Skip to content

Commit

Permalink
Merge pull request #1455 from matthewh/danger-js-1180-esm_support
Browse files Browse the repository at this point in the history
fix(node): #1180 - Enable ESM support
  • Loading branch information
orta committed Jun 19, 2024
2 parents fff9545 + 7fb5f74 commit d99a404
Show file tree
Hide file tree
Showing 7 changed files with 1,983 additions and 1,939 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
## Main

<!-- Your comment below this -->

- [#1180] Fix for MTS danger files [@matthewh]
<!-- Your comment above this -->

## 12.3.1
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
"ts-jest": "^28.0.0",
"ts-node": "^10.9.2",
"typedoc": "0.9.0",
"typescript": "^4.5.5",
"typescript": "^4.9.5",
"typescript-json-schema": "^0.53.0"
},
"dependencies": {
Expand Down
17 changes: 17 additions & 0 deletions source/commands/utils/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ export function dangerfilePath(program: any): string {
return program.dangerfile
}


if (existsSync("dangerfile.mts")) {
return "dangerfile.mts"
}

if (existsSync("dangerfile.mjs")) {
return "dangerfile.mjs"
}

if (existsSync("dangerfile.ts")) {
return "dangerfile.ts"
}
Expand All @@ -20,6 +29,14 @@ export function dangerfilePath(program: any): string {
return "dangerfile.js"
}

if (existsSync("Dangerfile.mts")) {
return "Dangerfile.mts"
}

if (existsSync("Dangerfile.mjs")) {
return "Dangerfile.mjs"
}

if (existsSync("Dangerfile.ts")) {
return "Dangerfile.ts"
}
Expand Down
17 changes: 14 additions & 3 deletions source/runner/runners/inline.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from "fs"

import * as path from "path"
import { debug } from "../../debug"
import _require from "require-from-string"

Expand Down Expand Up @@ -75,7 +75,7 @@ export const runDangerfileEnvironment = async (

const customRequire = moduleHandler || customModuleHandler

// Tell all these filetypes to ge the custom compilation
// Tell all these filetypes to get the custom compilation
require.extensions[".ts"] = customRequire
require.extensions[".tsx"] = customRequire
require.extensions[".js"] = customRequire
Expand Down Expand Up @@ -105,7 +105,18 @@ export const runDangerfileEnvironment = async (
}

d("Started parsing Dangerfile: ", filename)
const optionalExport = _require(compiled, filename, {})
let optionalExport
if (filename.endsWith(".mts")) {
const tmpFileName = path.join(process.cwd(), `._dangerfile.mjs`)
fs.writeFileSync(tmpFileName, compiled)
// tried but data urls have trouble with imports and I don't know how to fix
// optionalExport = (await import(`data:text/javascript;base64,${btoa(compiled)}`));
optionalExport = await import(tmpFileName)
} else if (filename.endsWith(".mjs")) {
optionalExport = await import(path.join(process.cwd(), filename))
} else {
optionalExport = _require(compiled, filename, {})
}

if (typeof optionalExport.default === "function") {
d("Running default export from Dangerfile", filename)
Expand Down
14 changes: 8 additions & 6 deletions source/runner/runners/utils/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const lookupTSConfig = (dir: string): string | null => {
return null
}

export const typescriptify = (content: string, dir: string): string => {
export const typescriptify = (content: string, dir: string, esm: boolean = false): string => {
const ts = require("typescript")

// Support custom TSC options, but also fallback to defaults
Expand All @@ -131,11 +131,11 @@ export const typescriptify = (content: string, dir: string): string => {
compilerOptions = ts.getDefaultCompilerOptions()
}

let result = ts.transpileModule(content, sanitizeTSConfig(compilerOptions))
let result = ts.transpileModule(content, sanitizeTSConfig(compilerOptions, esm))
return result.outputText
}

const sanitizeTSConfig = (config: any) => {
const sanitizeTSConfig = (config: any, esm: boolean = false) => {
if (!config.compilerOptions) {
return config
}
Expand All @@ -149,8 +149,10 @@ const sanitizeTSConfig = (config: any) => {
//
// @see https://github.com/apollographql/react-apollo/pull/1402#issuecomment-351810274
//
if (safeConfig.compilerOptions.module) {
if (!esm && safeConfig.compilerOptions.module) {
safeConfig.compilerOptions.module = "commonjs"
} else {
safeConfig.compilerOptions.module = "es6"
}

return safeConfig
Expand Down Expand Up @@ -202,9 +204,9 @@ export default (code: string, filename: string, remoteFile: boolean = false) =>
}

let result = code
if (hasNativeTypeScript && filetype.startsWith(".ts")) {
if (hasNativeTypeScript && (filetype.startsWith(".ts") || filetype.startsWith(".mts"))) {
d("compiling with typescript")
result = typescriptify(code, path.dirname(filename))
result = typescriptify(code, path.dirname(filename), filename.endsWith(".mts"))
} else if (hasBabel && hasBabelTypeScript && filetype.startsWith(".ts")) {
d("compiling as typescript with babel")
result = babelify(code, filename, [`${babelPackagePrefix}plugin-transform-typescript`])
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"noUnusedParameters": true,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "node",
"moduleResolution": "node16",
"pretty": true,
"target": "es5",
"outDir": "distribution",
Expand Down
Loading

0 comments on commit d99a404

Please sign in to comment.