Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 860d1f6

Browse files
authored
fix(register): enforece module option in register/esm (#694)
1 parent ac2ed69 commit 860d1f6

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

packages/register/esm.mts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { promises as fs, constants as FSConstants } from 'fs'
22
import { join, parse, isAbsolute } from 'path'
33
import { fileURLToPath, pathToFileURL } from 'url'
44

5+
import ts from 'typescript'
6+
57
// @ts-expect-error
68
import { readDefaultTsConfig } from '../lib/read-default-tsconfig.js'
79
// @ts-expect-error
@@ -93,7 +95,8 @@ type LoadFn = (
9395
export const load: LoadFn = async (url, context, defaultLoad) => {
9496
const filePath = TRANSFORM_MAP.get(url)
9597
if (filePath) {
96-
const tsconfig = readDefaultTsConfig()
98+
const tsconfig: ts.CompilerOptions = readDefaultTsConfig()
99+
tsconfig.module = ts.ModuleKind.ESNext
97100
const code = await compile(await fs.readFile(filePath, 'utf8'), filePath, tsconfig, true)
98101
return {
99102
format: context.format,

packages/register/register.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { platform } from 'os'
22
import { resolve } from 'path'
33

4-
import { transform, transformSync } from '@swc-node/core'
4+
import { transform, transformSync, Options } from '@swc-node/core'
55
import { SourcemapMap, installSourceMapSupport } from '@swc-node/sourcemap-support'
66
import { addHook } from 'pirates'
77
import * as ts from 'typescript'
@@ -77,12 +77,16 @@ export function compile(
7777
return code
7878
})
7979
} else {
80-
const swcRegisterConfig = tsCompilerOptionsToSwcConfig(options, filename)
81-
if (process.env.SWCRC === 'true') {
80+
let swcRegisterConfig: Options
81+
if (process.env.SWCRC) {
8282
// when SWCRC environment variable is set to true it will use swcrc file
83-
swcRegisterConfig.swc = {
84-
swcrc: true,
83+
swcRegisterConfig = {
84+
swc: {
85+
swcrc: true,
86+
},
8587
}
88+
} else {
89+
swcRegisterConfig = tsCompilerOptionsToSwcConfig(options, filename)
8690
}
8791
const { code, map } = transformSync(sourcecode, filename, swcRegisterConfig)
8892
// in case of map is undefined
@@ -93,7 +97,11 @@ export function compile(
9397
}
9498
}
9599

96-
export function register(options = readDefaultTsConfig(), hookOpts = {}) {
100+
export function register(options: Partial<ts.CompilerOptions> = {}, hookOpts = {}) {
101+
if (!process.env.SWCRC) {
102+
options = readDefaultTsConfig()
103+
}
104+
options.module = ts.ModuleKind.CommonJS
97105
installSourceMapSupport()
98106
return addHook((code, filename) => compile(code, filename, options), {
99107
exts: DEFAULT_EXTENSIONS,

0 commit comments

Comments
 (0)