Skip to content

Commit

Permalink
Merge pull request #440 from danger/tsc_module
Browse files Browse the repository at this point in the history
Adds an overwrite for the TS module system when transpiling our TS code
  • Loading branch information
orta authored Dec 14, 2017
2 parents f23ace9 + 46cc98c commit fed3534
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
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

0 comments on commit fed3534

Please sign in to comment.