Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds an overwrite for the TS module system when transpiling our TS code #440

Merged
merged 1 commit into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ developers, so please limit technical // terminology to in here.

// ### Master

### 2.1.5

* The TS compiler will force a module type of commonjs when transpiling the Dangerfile - [@orta][]

### 2.1.4

* Adds a CLI option for a unique Danger ID per run to `danger` and `danger process`,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "danger",
"version": "2.1.4",
"version": "2.1.5",
"description": "Unit tests for Team Culture",
"main": "distribution/danger.js",
"typings": "distribution/danger.d.ts",
Expand Down
22 changes: 22 additions & 0 deletions source/runner/runners/utils/_tests/_transpiler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
jest.mock("fs", () => ({
readFileSync: jest.fn(),
}))

import { typescriptify } from "../transpiler"
import * as fs from "fs"

describe("typescriptify", () => {
it("removes the module option in a tsconfig ", () => {
const dangerfile = `import {a} from 'lodash'; a()`
const fakeTSConfig = {
compilerOptions: {
target: "es5",
module: "es2015",
},
}
const fsMock = fs.readFileSync as any
fsMock.mockImplementationOnce(() => JSON.stringify(fakeTSConfig))

expect(typescriptify(dangerfile)).not.toContain("import")
})
})
23 changes: 22 additions & 1 deletion source/runner/runners/utils/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,31 @@ try {
export const typescriptify = (content: string): string => {
const ts = require("typescript") // tslint:disable-line
const compilerOptions = JSON.parse(fs.readFileSync("tsconfig.json", "utf8"))
let result = ts.transpileModule(content, compilerOptions)
let result = ts.transpileModule(content, sanitizeTSConfig(compilerOptions))
return result.outputText
}

const sanitizeTSConfig = (config: any) => {
if (!config.compilerOptions) {
return config
}

const safeConfig = config

// It can make sense to ship TS code with modules
// for `import`/`export` syntax, but as we're running
// the transpiled code on vanilla node - it'll need to
// be used with plain old commonjs
//
// @see https://github.com/apollographql/react-apollo/pull/1402#issuecomment-351810274
//
if (safeConfig.compilerOptions.module) {
safeConfig.compilerOptions.module = "commonjs"
}

return safeConfig
}

export const babelify = (content: string, filename: string, extraPlugins: string[]): string => {
const babel = require("babel-core") // tslint:disable-line
if (!babel.transform) {
Expand Down