Skip to content

Commit

Permalink
fix(node): danger#1180 - Enable ESM support
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewh committed Jun 17, 2024
1 parent fff9545 commit b516cc3
Show file tree
Hide file tree
Showing 5 changed files with 1,965 additions and 1,938 deletions.
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: 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 b516cc3

Please sign in to comment.