Skip to content

Commit 7c686f1

Browse files
authored
fix(register): remove file extension tests in compile (#825)
1 parent 1f7ceaf commit 7c686f1

File tree

7 files changed

+32
-58
lines changed

7 files changed

+32
-58
lines changed

packages/integrate-module/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@swc-node/register": "workspace:*",
1414
"@types/react": "^18.3.3",
1515
"@types/react-dom": "^18.3.0",
16+
"esmock": "^2.6.6",
1617
"ipaddr.js": "^2.2.0",
1718
"react": "^18.3.1",
1819
"react-dom": "^18.3.1",

packages/integrate-module/src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import test from 'node:test'
44

55
import { RepositoryState } from '@napi-rs/simple-git'
66
import { bar as subBar } from '@subdirectory/bar.mjs'
7+
import esmock from 'esmock'
78
import { supportedExtensions } from 'file-type'
89
import { renderToString } from 'react-dom/server'
910
import { simpleGit } from 'simple-git'
@@ -79,3 +80,13 @@ await test('resolve json file', () => {
7980
await test('resolve ipaddr.js', () => {
8081
assert.ok(ipaddr.isValid('192.168.1.1'))
8182
})
83+
84+
await test('esmock should work', async () => {
85+
const main = await esmock('./mocked.ts', {
86+
path: {
87+
basename: () => 'hello',
88+
},
89+
})
90+
91+
assert.strictEqual(main.pathbasenamewrap(), 'hello')
92+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import path from 'path'
2+
3+
export default {
4+
pathbasenamewrap: (n: string) => path.basename(n),
5+
}

packages/integrate/__tests__/extention/extention.spec.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

packages/register/esm.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import ts from 'typescript'
1717
// @ts-expect-error
1818
import { readDefaultTsConfig } from '../lib/read-default-tsconfig.js'
1919
// @ts-expect-error
20-
import { compile } from '../lib/register.js'
20+
import { compile, DEFAULT_EXTENSIONS } from '../lib/register.js'
2121

2222
const debug = debugFactory('@swc-node')
2323

packages/register/register.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as ts from 'typescript'
55

66
import { readDefaultTsConfig, tsCompilerOptionsToSwcConfig } from './read-default-tsconfig'
77

8-
const DEFAULT_EXTENSIONS = [
8+
const DEFAULT_EXTENSIONS = new Set([
99
ts.Extension.Js,
1010
ts.Extension.Ts,
1111
ts.Extension.Jsx,
@@ -16,17 +16,7 @@ const DEFAULT_EXTENSIONS = [
1616
ts.Extension.Cts,
1717
'.es6',
1818
'.es',
19-
]
20-
21-
export const AVAILABLE_TS_EXTENSION_PATTERN = new RegExp(
22-
`((?<!\\.d)(${[ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Mts, ts.Extension.Cts].map((ext) => ext.replace(/^\./, '\\.')).join('|')}))$`,
23-
'i',
24-
)
25-
26-
export const AVAILABLE_EXTENSION_PATTERN = new RegExp(
27-
`((?<!\\.d)(${DEFAULT_EXTENSIONS.map((ext) => ext.replace(/^\./, '\\.')).join('|')}))$`,
28-
'i',
29-
)
19+
])
3020

3121
const injectInlineSourceMap = ({
3222
filename,
@@ -89,12 +79,8 @@ export function compile(
8979
},
9080
async = false,
9181
) {
92-
if (
93-
typeof sourcecode === 'undefined' ||
94-
(filename.includes('node_modules') && !AVAILABLE_TS_EXTENSION_PATTERN.test(filename)) ||
95-
!AVAILABLE_EXTENSION_PATTERN.test(filename)
96-
) {
97-
return sourcecode
82+
if (sourcecode == null) {
83+
return
9884
}
9985
if (options && typeof options.fallbackToTs === 'function' && options.fallbackToTs(filename)) {
10086
delete options.fallbackToTs
@@ -134,7 +120,7 @@ export function register(options: Partial<ts.CompilerOptions> = {}, hookOpts = {
134120
options.module = ts.ModuleKind.CommonJS
135121
installSourceMapSupport()
136122
return addHook((code, filename) => compile(code, filename, options), {
137-
exts: DEFAULT_EXTENSIONS,
123+
exts: Array.from(DEFAULT_EXTENSIONS),
138124
...hookOpts,
139125
})
140126
}

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)