From 7c686f13aa644bc280143f399ab60fc7fd8a9da3 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 16 Jul 2024 17:04:53 +0800 Subject: [PATCH] fix(register): remove file extension tests in compile (#825) --- packages/integrate-module/package.json | 1 + packages/integrate-module/src/index.ts | 11 ++++++ packages/integrate-module/src/mocked.ts | 5 +++ .../__tests__/extention/extention.spec.ts | 38 ------------------- packages/register/esm.mts | 2 +- packages/register/register.ts | 24 +++--------- pnpm-lock.yaml | 9 +++++ 7 files changed, 32 insertions(+), 58 deletions(-) create mode 100644 packages/integrate-module/src/mocked.ts delete mode 100644 packages/integrate/__tests__/extention/extention.spec.ts diff --git a/packages/integrate-module/package.json b/packages/integrate-module/package.json index f667dd85c..4f1cacda7 100644 --- a/packages/integrate-module/package.json +++ b/packages/integrate-module/package.json @@ -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", diff --git a/packages/integrate-module/src/index.ts b/packages/integrate-module/src/index.ts index 4b22ad1d4..bedd63e64 100644 --- a/packages/integrate-module/src/index.ts +++ b/packages/integrate-module/src/index.ts @@ -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' @@ -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') +}) diff --git a/packages/integrate-module/src/mocked.ts b/packages/integrate-module/src/mocked.ts new file mode 100644 index 000000000..64342b364 --- /dev/null +++ b/packages/integrate-module/src/mocked.ts @@ -0,0 +1,5 @@ +import path from 'path' + +export default { + pathbasenamewrap: (n: string) => path.basename(n), +} diff --git a/packages/integrate/__tests__/extention/extention.spec.ts b/packages/integrate/__tests__/extention/extention.spec.ts deleted file mode 100644 index 0d078b8ec..000000000 --- a/packages/integrate/__tests__/extention/extention.spec.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { AVAILABLE_EXTENSION_PATTERN, AVAILABLE_TS_EXTENSION_PATTERN } from '@swc-node/register/register.ts' -import test from 'ava' -import * as ts from 'typescript' - -const tsExtensions = [ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Mts, ts.Extension.Cts] -const nonTsExtensions = [ts.Extension.Js, ts.Extension.Jsx, ts.Extension.Mjs, ts.Extension.Cjs, '.es6', '.es'] -const defaultExtensions = [...tsExtensions, ...nonTsExtensions] -const ignoreExtensions = ['.txt', '.json', '.xml'] - -test(`AVAILABLE_TS_EXTENSION_PATTERN matches TypeScript extensions`, (t) => { - tsExtensions.forEach((ext) => { - t.true(AVAILABLE_TS_EXTENSION_PATTERN.test(`file${ext}`)) - }) -}) - -test(`AVAILABLE_TS_EXTENSION_PATTERN does not match d.ts`, (t) => { - tsExtensions.forEach((ext) => { - t.false(AVAILABLE_TS_EXTENSION_PATTERN.test(`file.d${ext}`)) - }) -}) - -test(`AVAILABLE_TS_EXTENSION_PATTERN does not match non-ts extensions`, (t) => { - ;[...nonTsExtensions, ...ignoreExtensions].forEach((ext) => { - t.false(AVAILABLE_TS_EXTENSION_PATTERN.test(`file${ext}`)) - }) -}) - -test(`AVAILABLE_EXTENSION_PATTERN matches default extensions`, (t) => { - defaultExtensions.forEach((ext) => { - t.true(AVAILABLE_EXTENSION_PATTERN.test(`file${ext}`)) - }) -}) - -test(`AVAILABLE_EXTENSION_PATTERN does not match non-default extensions`, (t) => { - ignoreExtensions.forEach((ext) => { - t.false(AVAILABLE_EXTENSION_PATTERN.test(`file${ext}`)) - }) -}) diff --git a/packages/register/esm.mts b/packages/register/esm.mts index ee23664c6..d1527c28e 100644 --- a/packages/register/esm.mts +++ b/packages/register/esm.mts @@ -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') diff --git a/packages/register/register.ts b/packages/register/register.ts index c80ed79ef..76ed8284d 100644 --- a/packages/register/register.ts +++ b/packages/register/register.ts @@ -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, @@ -16,17 +16,7 @@ const DEFAULT_EXTENSIONS = [ ts.Extension.Cts, '.es6', '.es', -] - -export const AVAILABLE_TS_EXTENSION_PATTERN = new RegExp( - `((? ext.replace(/^\./, '\\.')).join('|')}))$`, - 'i', -) - -export const AVAILABLE_EXTENSION_PATTERN = new RegExp( - `((? ext.replace(/^\./, '\\.')).join('|')}))$`, - 'i', -) +]) const injectInlineSourceMap = ({ filename, @@ -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 @@ -134,7 +120,7 @@ export function register(options: Partial = {}, hookOpts = { options.module = ts.ModuleKind.CommonJS installSourceMapSupport() return addHook((code, filename) => compile(code, filename, options), { - exts: DEFAULT_EXTENSIONS, + exts: Array.from(DEFAULT_EXTENSIONS), ...hookOpts, }) } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 552c99623..cf9174f49 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -165,6 +165,9 @@ importers: '@types/react-dom': specifier: ^18.3.0 version: 18.3.0 + esmock: + specifier: ^2.6.6 + version: 2.6.6 ipaddr.js: specifier: ^2.2.0 version: 2.2.0 @@ -2710,6 +2713,10 @@ packages: resolution: {integrity: sha512-2NxwbF/hZ0KpepYN0cNbo+FN6XoK7GaHlQhgx/hIZl6Va0bF45RQOOwhLIy8lQDbuCiadSLCBnH2CFYquit5bw==} engines: {node: '>=8.0.0'} + esmock@2.6.6: + resolution: {integrity: sha512-Ay1IQ/qbZV1j8IqRY5o4GYNyX8Y6qDJVVYRZtQ0WFOqUPf26++qW/LyNaHrxz79QGB4F1xuDTCPRYA5XRNuqZg==} + engines: {node: '>=14.16.0'} + esprima@4.0.1: resolution: {integrity: sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==} engines: {node: '>=4'} @@ -7751,6 +7758,8 @@ snapshots: esrecurse: 4.3.0 estraverse: 4.3.0 + esmock@2.6.6: {} + esprima@4.0.1: {} esrecurse@4.3.0: