Skip to content

Commit

Permalink
refactor(core): use oxc-resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Jul 3, 2024
1 parent 9644be5 commit 5d211ff
Show file tree
Hide file tree
Showing 11 changed files with 1,713 additions and 2,483 deletions.
38 changes: 17 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,51 +28,47 @@
"bench": "node -r @swc-node/register ./bench/index.ts",
"build": "tsc -b tsconfig.json",
"format": "prettier --config ./package.json . -w",
"lint": "eslint -c ./.eslintrc.yml .",
"lint": "oxlint .",
"test": "ava",
"test:jest": "jest --config jest.config.js",
"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",
"version": "pnpm install && git add .",
"postinstall": "husky install"
},
"devDependencies": {
"@babel/core": "^7.24.5",
"@babel/plugin-transform-typescript": "^7.24.5",
"@babel/preset-env": "^7.24.5",
"@babel/preset-typescript": "^7.24.1",
"@babel/core": "^7.24.7",
"@babel/plugin-transform-typescript": "^7.24.7",
"@babel/preset-env": "^7.24.7",
"@babel/preset-typescript": "^7.24.7",
"@swc-node/core": "workspace:*",
"@swc-node/jest": "workspace:*",
"@swc-node/register": "workspace:*",
"@types/babel__core": "^7.20.5",
"@types/benchmark": "^2.1.5",
"@types/lodash": "^4.17.0",
"@types/node": "^20.12.7",
"@types/lodash": "^4.17.6",
"@types/node": "^20.14.9",
"@types/sinon": "^17.0.3",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"ava": "^6.1.2",
"ava": "^6.1.3",
"benchmark": "^2.1.4",
"colorette": "^2.0.20",
"cross-env": "^7.0.3",
"esbuild": "^0.20.2",
"eslint": "8",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"esbuild": "^0.23.0",
"husky": "^9.0.11",
"jest": "^29.7.0",
"lerna": "^8.1.2",
"lint-staged": "^15.2.2",
"lerna": "8.1.4",
"lint-staged": "^15.2.7",
"lodash": "^4.17.21",
"prettier": "^3.2.5",
"oxlint": "^0.5.2",
"prettier": "^3.3.2",
"react": "^18.3.1",
"rxjs": "^7.8.1",
"sinon": "^17.0.1",
"tslib": "^2.6.2",
"typescript": "^5.4.5"
"sinon": "^18.0.0",
"tslib": "^2.6.3",
"typescript": "^5.5.3"
},
"lint-staged": {
"*.@(js|ts|tsx|mts)": [
"eslint --fix"
"oxlint --fix"
],
"*.@(js|ts|tsx|mts|yml|yaml|md|json)": [
"prettier --write"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function transformOption(path: string, options?: Options, jest = false): SwcOpti
: opts.sourcemap,
inlineSourcesContent: true,
swcrc: false,
...(options?.swc ?? {}),
...options?.swc,
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"@swc/types": ">= 0.1"
},
"devDependencies": {
"@swc/core": "^1.4.17",
"@swc/types": "^0.1.6"
"@swc/core": "^1.6.6",
"@swc/types": "^0.1.9"
},
"funding": {
"type": "github",
Expand Down
4 changes: 2 additions & 2 deletions packages/integrate-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"p-timeout": "^6.1.2"
},
"devDependencies": {
"@swc/core": "^1.4.17",
"@swc/core": "^1.6.6",
"@swc-node/register": "workspace:*",
"typescript": "^5.4.5"
"typescript": "^5.5.3"
}
}
8 changes: 4 additions & 4 deletions packages/integrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
},
"devDependencies": {
"@swc/helpers": "^0.5.11",
"@swc-node/core": "^1.13.0",
"@swc-node/core": "^1.13.1",
"@swc-node/register": "workspace:*",
"@types/jest": "^29.5.12",
"@types/react": "^18.3.1",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"jest": "^29.7.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"sinon": "^17.0.1",
"typescript": "^5.4.5"
"sinon": "^18.0.0",
"typescript": "^5.5.3"
}
}
4 changes: 2 additions & 2 deletions packages/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"typescript": ">= 5.0"
},
"devDependencies": {
"@swc/core": "^1.4.17",
"@swc/types": "^0.1.6"
"@swc/core": "^1.6.6",
"@swc/types": "^0.1.9"
},
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions packages/loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"url": "https://github.com/sponsors/Brooooooklyn"
},
"devDependencies": {
"typescript": "^5.4.5",
"webpack": "^5.91.0"
"typescript": "^5.5.3",
"webpack": "^5.92.1"
}
}
50 changes: 32 additions & 18 deletions packages/register/esm.mts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { readFile } from 'fs/promises'
import { createRequire, type LoadFnOutput, type LoadHook, type ResolveFnOutput, type ResolveHook } from 'node:module'
import { extname } from 'path'
import { extname, join } from 'path'
import { fileURLToPath, parse as parseUrl, pathToFileURL } from 'url'

import debugFactory from 'debug'
import { ResolverFactory } from 'oxc-resolver'
import ts from 'typescript'

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

const moduleResolutionCache = ts.createModuleResolutionCache(ts.sys.getCurrentDirectory(), (x) => x, tsconfig)
const host: ts.ModuleResolutionHost = {
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
}
const TSCONFIG_PATH = (function () {
const pathFromEnv =
process.env.SWC_NODE_PROJECT ?? process.env.TS_NODE_PROJECT ?? join(process.cwd(), 'tsconfig.json')
if (!pathFromEnv.startsWith('/')) {
return join(process.cwd(), pathFromEnv)
}
return pathFromEnv
})()

const resolver = new ResolverFactory({
tsconfig: {
configFile: TSCONFIG_PATH,
references: 'auto',
},
extensionAlias: {
'.js': ['.ts', '.js'],
'.mjs': ['.mts', '.mjs'],
'.cjs': ['.cts', '.cjs'],
},
})

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

const { resolvedModule } = ts.resolveModuleName(
const { error, path } = await resolver.async(
join(fileURLToPath(context.parentURL), '..'),
specifier.startsWith('file:') ? fileURLToPath(specifier) : specifier,
fileURLToPath(context.parentURL),
tsconfig,
host,
moduleResolutionCache,
)

if (error) {
console.error(error)
throw error
}

// local project file
if (
resolvedModule &&
!resolvedModule.resolvedFileName.includes('/node_modules/') &&
AVAILABLE_TS_EXTENSION_PATTERN.test(resolvedModule.resolvedFileName)
) {
debug('resolved: typescript', specifier, resolvedModule.resolvedFileName)
if (path && !path.includes('/node_modules/')) {
debug('resolved: typescript', specifier, path)

return addShortCircuitSignal({
...context,
url: pathToFileURL(resolvedModule.resolvedFileName).href,
url: pathToFileURL(path).href,
format: 'module',
})
}
Expand Down
11 changes: 6 additions & 5 deletions packages/register/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,22 @@
"@swc-node/core": "^1.13.1",
"@swc-node/sourcemap-support": "^0.5.0",
"colorette": "^2.0.20",
"debug": "^4.3.4",
"debug": "^4.3.5",
"oxc-resolver": "^1.9.2",
"pirates": "^4.0.6",
"tslib": "^2.6.2"
"tslib": "^2.6.3"
},
"peerDependencies": {
"@swc/core": ">= 1.4.13",
"typescript": ">= 4.3"
},
"devDependencies": {
"@swc/core": "^1.4.17",
"@swc/core": "^1.6.6",
"@swc/helpers": "^0.5.11",
"@types/debug": "^4.1.12",
"lodash": "^4.17.21",
"sinon": "^17.0.1",
"typescript": "^5.4.5"
"sinon": "^18.0.0",
"typescript": "^5.5.3"
},
"funding": {
"type": "github",
Expand Down
2 changes: 1 addition & 1 deletion packages/sourcemap-support/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"dependencies": {
"source-map-support": "^0.5.21",
"tslib": "^2.6.2"
"tslib": "^2.6.3"
},
"bugs": {
"url": "https://github.com/swc-project/swc-node/issues"
Expand Down
Loading

0 comments on commit 5d211ff

Please sign in to comment.