Skip to content

Commit

Permalink
fix(register): remove file extension tests in compile (#825)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn authored Jul 16, 2024
1 parent 1f7ceaf commit 7c686f1
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 58 deletions.
1 change: 1 addition & 0 deletions packages/integrate-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@swc-node/register": "workspace:*",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"esmock": "^2.6.6",
"ipaddr.js": "^2.2.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
11 changes: 11 additions & 0 deletions packages/integrate-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import test from 'node:test'

import { RepositoryState } from '@napi-rs/simple-git'
import { bar as subBar } from '@subdirectory/bar.mjs'
import esmock from 'esmock'
import { supportedExtensions } from 'file-type'
import { renderToString } from 'react-dom/server'
import { simpleGit } from 'simple-git'
Expand Down Expand Up @@ -79,3 +80,13 @@ await test('resolve json file', () => {
await test('resolve ipaddr.js', () => {
assert.ok(ipaddr.isValid('192.168.1.1'))
})

await test('esmock should work', async () => {
const main = await esmock('./mocked.ts', {
path: {
basename: () => 'hello',
},
})

assert.strictEqual(main.pathbasenamewrap(), 'hello')
})
5 changes: 5 additions & 0 deletions packages/integrate-module/src/mocked.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import path from 'path'

export default {
pathbasenamewrap: (n: string) => path.basename(n),
}
38 changes: 0 additions & 38 deletions packages/integrate/__tests__/extention/extention.spec.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/register/esm.mts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ts from 'typescript'
// @ts-expect-error
import { readDefaultTsConfig } from '../lib/read-default-tsconfig.js'
// @ts-expect-error
import { compile } from '../lib/register.js'
import { compile, DEFAULT_EXTENSIONS } from '../lib/register.js'

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

Expand Down
24 changes: 5 additions & 19 deletions packages/register/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as ts from 'typescript'

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

const DEFAULT_EXTENSIONS = [
const DEFAULT_EXTENSIONS = new Set([
ts.Extension.Js,
ts.Extension.Ts,
ts.Extension.Jsx,
Expand All @@ -16,17 +16,7 @@ const DEFAULT_EXTENSIONS = [
ts.Extension.Cts,
'.es6',
'.es',
]

export const AVAILABLE_TS_EXTENSION_PATTERN = new RegExp(
`((?<!\\.d)(${[ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Mts, ts.Extension.Cts].map((ext) => ext.replace(/^\./, '\\.')).join('|')}))$`,
'i',
)

export const AVAILABLE_EXTENSION_PATTERN = new RegExp(
`((?<!\\.d)(${DEFAULT_EXTENSIONS.map((ext) => ext.replace(/^\./, '\\.')).join('|')}))$`,
'i',
)
])

const injectInlineSourceMap = ({
filename,
Expand Down Expand Up @@ -89,12 +79,8 @@ export function compile(
},
async = false,
) {
if (
typeof sourcecode === 'undefined' ||
(filename.includes('node_modules') && !AVAILABLE_TS_EXTENSION_PATTERN.test(filename)) ||
!AVAILABLE_EXTENSION_PATTERN.test(filename)
) {
return sourcecode
if (sourcecode == null) {
return
}
if (options && typeof options.fallbackToTs === 'function' && options.fallbackToTs(filename)) {
delete options.fallbackToTs
Expand Down Expand Up @@ -134,7 +120,7 @@ export function register(options: Partial<ts.CompilerOptions> = {}, hookOpts = {
options.module = ts.ModuleKind.CommonJS
installSourceMapSupport()
return addHook((code, filename) => compile(code, filename, options), {
exts: DEFAULT_EXTENSIONS,
exts: Array.from(DEFAULT_EXTENSIONS),
...hookOpts,
})
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7c686f1

Please sign in to comment.