Skip to content

Commit 5d211ff

Browse files
committed
refactor(core): use oxc-resolver
1 parent 9644be5 commit 5d211ff

File tree

11 files changed

+1713
-2483
lines changed

11 files changed

+1713
-2483
lines changed

package.json

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,51 +28,47 @@
2828
"bench": "node -r @swc-node/register ./bench/index.ts",
2929
"build": "tsc -b tsconfig.json",
3030
"format": "prettier --config ./package.json . -w",
31-
"lint": "eslint -c ./.eslintrc.yml .",
31+
"lint": "oxlint .",
3232
"test": "ava",
3333
"test:jest": "jest --config jest.config.js",
3434
"test:module": "cross-env SWC_NODE_PROJECT=packages/integrate-module/tsconfig.json node --import=@swc-node/register/esm-register packages/integrate-module/src/index.ts",
3535
"version": "pnpm install && git add .",
3636
"postinstall": "husky install"
3737
},
3838
"devDependencies": {
39-
"@babel/core": "^7.24.5",
40-
"@babel/plugin-transform-typescript": "^7.24.5",
41-
"@babel/preset-env": "^7.24.5",
42-
"@babel/preset-typescript": "^7.24.1",
39+
"@babel/core": "^7.24.7",
40+
"@babel/plugin-transform-typescript": "^7.24.7",
41+
"@babel/preset-env": "^7.24.7",
42+
"@babel/preset-typescript": "^7.24.7",
4343
"@swc-node/core": "workspace:*",
4444
"@swc-node/jest": "workspace:*",
4545
"@swc-node/register": "workspace:*",
4646
"@types/babel__core": "^7.20.5",
4747
"@types/benchmark": "^2.1.5",
48-
"@types/lodash": "^4.17.0",
49-
"@types/node": "^20.12.7",
48+
"@types/lodash": "^4.17.6",
49+
"@types/node": "^20.14.9",
5050
"@types/sinon": "^17.0.3",
51-
"@typescript-eslint/eslint-plugin": "^7.8.0",
52-
"@typescript-eslint/parser": "^7.8.0",
53-
"ava": "^6.1.2",
51+
"ava": "^6.1.3",
5452
"benchmark": "^2.1.4",
5553
"colorette": "^2.0.20",
5654
"cross-env": "^7.0.3",
57-
"esbuild": "^0.20.2",
58-
"eslint": "8",
59-
"eslint-config-prettier": "^9.1.0",
60-
"eslint-plugin-import": "^2.29.1",
55+
"esbuild": "^0.23.0",
6156
"husky": "^9.0.11",
6257
"jest": "^29.7.0",
63-
"lerna": "^8.1.2",
64-
"lint-staged": "^15.2.2",
58+
"lerna": "8.1.4",
59+
"lint-staged": "^15.2.7",
6560
"lodash": "^4.17.21",
66-
"prettier": "^3.2.5",
61+
"oxlint": "^0.5.2",
62+
"prettier": "^3.3.2",
6763
"react": "^18.3.1",
6864
"rxjs": "^7.8.1",
69-
"sinon": "^17.0.1",
70-
"tslib": "^2.6.2",
71-
"typescript": "^5.4.5"
65+
"sinon": "^18.0.0",
66+
"tslib": "^2.6.3",
67+
"typescript": "^5.5.3"
7268
},
7369
"lint-staged": {
7470
"*.@(js|ts|tsx|mts)": [
75-
"eslint --fix"
71+
"oxlint --fix"
7672
],
7773
"*.@(js|ts|tsx|mts|yml|yaml|md|json)": [
7874
"prettier --write"

packages/core/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function transformOption(path: string, options?: Options, jest = false): SwcOpti
8282
: opts.sourcemap,
8383
inlineSourcesContent: true,
8484
swcrc: false,
85-
...(options?.swc ?? {}),
85+
...options?.swc,
8686
}
8787
}
8888

packages/core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
"@swc/types": ">= 0.1"
4646
},
4747
"devDependencies": {
48-
"@swc/core": "^1.4.17",
49-
"@swc/types": "^0.1.6"
48+
"@swc/core": "^1.6.6",
49+
"@swc/types": "^0.1.9"
5050
},
5151
"funding": {
5252
"type": "github",

packages/integrate-module/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"p-timeout": "^6.1.2"
99
},
1010
"devDependencies": {
11-
"@swc/core": "^1.4.17",
11+
"@swc/core": "^1.6.6",
1212
"@swc-node/register": "workspace:*",
13-
"typescript": "^5.4.5"
13+
"typescript": "^5.5.3"
1414
}
1515
}

packages/integrate/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
},
2121
"devDependencies": {
2222
"@swc/helpers": "^0.5.11",
23-
"@swc-node/core": "^1.13.0",
23+
"@swc-node/core": "^1.13.1",
2424
"@swc-node/register": "workspace:*",
2525
"@types/jest": "^29.5.12",
26-
"@types/react": "^18.3.1",
26+
"@types/react": "^18.3.3",
2727
"@types/react-dom": "^18.3.0",
2828
"jest": "^29.7.0",
2929
"react": "^18.3.1",
3030
"react-dom": "^18.3.1",
31-
"sinon": "^17.0.1",
32-
"typescript": "^5.4.5"
31+
"sinon": "^18.0.0",
32+
"typescript": "^5.5.3"
3333
}
3434
}

packages/jest/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
"typescript": ">= 5.0"
3838
},
3939
"devDependencies": {
40-
"@swc/core": "^1.4.17",
41-
"@swc/types": "^0.1.6"
40+
"@swc/core": "^1.6.6",
41+
"@swc/types": "^0.1.9"
4242
},
4343
"repository": {
4444
"type": "git",

packages/loader/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"url": "https://github.com/sponsors/Brooooooklyn"
4848
},
4949
"devDependencies": {
50-
"typescript": "^5.4.5",
51-
"webpack": "^5.91.0"
50+
"typescript": "^5.5.3",
51+
"webpack": "^5.92.1"
5252
}
5353
}

packages/register/esm.mts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { readFile } from 'fs/promises'
22
import { createRequire, type LoadFnOutput, type LoadHook, type ResolveFnOutput, type ResolveHook } from 'node:module'
3-
import { extname } from 'path'
3+
import { extname, join } from 'path'
44
import { fileURLToPath, parse as parseUrl, pathToFileURL } from 'url'
55

66
import debugFactory from 'debug'
7+
import { ResolverFactory } from 'oxc-resolver'
78
import ts from 'typescript'
89

910
// @ts-expect-error
@@ -16,11 +17,26 @@ const debug = debugFactory('@swc-node')
1617
const tsconfig: ts.CompilerOptions = readDefaultTsConfig()
1718
tsconfig.module = ts.ModuleKind.ESNext
1819

19-
const moduleResolutionCache = ts.createModuleResolutionCache(ts.sys.getCurrentDirectory(), (x) => x, tsconfig)
20-
const host: ts.ModuleResolutionHost = {
21-
fileExists: ts.sys.fileExists,
22-
readFile: ts.sys.readFile,
23-
}
20+
const TSCONFIG_PATH = (function () {
21+
const pathFromEnv =
22+
process.env.SWC_NODE_PROJECT ?? process.env.TS_NODE_PROJECT ?? join(process.cwd(), 'tsconfig.json')
23+
if (!pathFromEnv.startsWith('/')) {
24+
return join(process.cwd(), pathFromEnv)
25+
}
26+
return pathFromEnv
27+
})()
28+
29+
const resolver = new ResolverFactory({
30+
tsconfig: {
31+
configFile: TSCONFIG_PATH,
32+
references: 'auto',
33+
},
34+
extensionAlias: {
35+
'.js': ['.ts', '.js'],
36+
'.mjs': ['.mts', '.mjs'],
37+
'.cjs': ['.cts', '.cjs'],
38+
},
39+
})
2440

2541
const addShortCircuitSignal = <T extends ResolveFnOutput | LoadFnOutput>(input: T): T => {
2642
return {
@@ -169,25 +185,23 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
169185
return addShortCircuitSignal(await nextResolve(specifier))
170186
}
171187

172-
const { resolvedModule } = ts.resolveModuleName(
188+
const { error, path } = await resolver.async(
189+
join(fileURLToPath(context.parentURL), '..'),
173190
specifier.startsWith('file:') ? fileURLToPath(specifier) : specifier,
174-
fileURLToPath(context.parentURL),
175-
tsconfig,
176-
host,
177-
moduleResolutionCache,
178191
)
179192

193+
if (error) {
194+
console.error(error)
195+
throw error
196+
}
197+
180198
// local project file
181-
if (
182-
resolvedModule &&
183-
!resolvedModule.resolvedFileName.includes('/node_modules/') &&
184-
AVAILABLE_TS_EXTENSION_PATTERN.test(resolvedModule.resolvedFileName)
185-
) {
186-
debug('resolved: typescript', specifier, resolvedModule.resolvedFileName)
199+
if (path && !path.includes('/node_modules/')) {
200+
debug('resolved: typescript', specifier, path)
187201

188202
return addShortCircuitSignal({
189203
...context,
190-
url: pathToFileURL(resolvedModule.resolvedFileName).href,
204+
url: pathToFileURL(path).href,
191205
format: 'module',
192206
})
193207
}

packages/register/package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,22 @@
4343
"@swc-node/core": "^1.13.1",
4444
"@swc-node/sourcemap-support": "^0.5.0",
4545
"colorette": "^2.0.20",
46-
"debug": "^4.3.4",
46+
"debug": "^4.3.5",
47+
"oxc-resolver": "^1.9.2",
4748
"pirates": "^4.0.6",
48-
"tslib": "^2.6.2"
49+
"tslib": "^2.6.3"
4950
},
5051
"peerDependencies": {
5152
"@swc/core": ">= 1.4.13",
5253
"typescript": ">= 4.3"
5354
},
5455
"devDependencies": {
55-
"@swc/core": "^1.4.17",
56+
"@swc/core": "^1.6.6",
5657
"@swc/helpers": "^0.5.11",
5758
"@types/debug": "^4.1.12",
5859
"lodash": "^4.17.21",
59-
"sinon": "^17.0.1",
60-
"typescript": "^5.4.5"
60+
"sinon": "^18.0.0",
61+
"typescript": "^5.5.3"
6162
},
6263
"funding": {
6364
"type": "github",

packages/sourcemap-support/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
},
3434
"dependencies": {
3535
"source-map-support": "^0.5.21",
36-
"tslib": "^2.6.2"
36+
"tslib": "^2.6.3"
3737
},
3838
"bugs": {
3939
"url": "https://github.com/swc-project/swc-node/issues"

0 commit comments

Comments
 (0)