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

fix(node): #1180 - Enable ESM support #1455

Merged
merged 1 commit into from
Jun 19, 2024
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
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