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(register): remove file extension tests in compile #825

Merged
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
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.