diff --git a/CHANGELOG.md b/CHANGELOG.md index 30c61e120..cccf7cc48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`, diff --git a/package.json b/package.json index 804da7b31..165443424 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/source/runner/runners/utils/_tests/_transpiler.test.ts b/source/runner/runners/utils/_tests/_transpiler.test.ts new file mode 100644 index 000000000..cea1a439f --- /dev/null +++ b/source/runner/runners/utils/_tests/_transpiler.test.ts @@ -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") + }) +}) diff --git a/source/runner/runners/utils/transpiler.ts b/source/runner/runners/utils/transpiler.ts index a14741115..8c82dda83 100644 --- a/source/runner/runners/utils/transpiler.ts +++ b/source/runner/runners/utils/transpiler.ts @@ -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) {