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

refactor(core): use oxc-resolver #798

Merged
merged 4 commits into from
Jul 3, 2024
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
8 changes: 6 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on:
branches: [master, develop]
pull_request:

env:
OXC_LOG: 'debug'
DEBUG: '@swc-node'

jobs:
test:
strategy:
Expand All @@ -18,7 +22,7 @@ jobs:
steps:
- uses: actions/checkout@v4

- uses: pnpm/action-setup@v2
- uses: pnpm/action-setup@v4

- name: Setup node
uses: actions/setup-node@v4
Expand Down Expand Up @@ -54,7 +58,7 @@ jobs:
node-version: 20

- name: Cache NPM dependencies
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.pnpm-store
key: npm-cache-ubuntu-latest-node@20
Expand Down
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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Generated by [AVA](https://avajs.dev).
`"use strict";␊
const Button = ({ text })=>/*#__PURE__*/ h("div", null, text);␊
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICBjb25zdCBCdXR0b24gPSAoeyB0ZXh0IH0pID0+IChcbiAgICA8ZGl2PlxuICAgICAge3RleHR9XG4gICAgPC9kaXY+XG4gIClcbiJdLCJuYW1lcyI6WyJCdXR0b24iLCJ0ZXh0IiwiZGl2Il0sInJhbmdlTWFwcGluZ3MiOiI7IiwibWFwcGluZ3MiOiI7QUFDRSxNQUFNQSxTQUFTLENBQUMsRUFBRUMsSUFBSSxFQUFFLGlCQUN0QixFQUFDQyxhQUNFRCJ9`
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICBjb25zdCBCdXR0b24gPSAoeyB0ZXh0IH0pID0+IChcbiAgICA8ZGl2PlxuICAgICAge3RleHR9XG4gICAgPC9kaXY+XG4gIClcbiJdLCJuYW1lcyI6WyJCdXR0b24iLCJ0ZXh0IiwiZGl2Il0sIm1hcHBpbmdzIjoiO0FBQ0UsTUFBTUEsU0FBUyxDQUFDLEVBQUVDLElBQUksRUFBRSxpQkFDdEIsRUFBQ0MsYUFDRUQifQ==`

## should transform jsx into new jsx runtime

Expand All @@ -26,4 +26,4 @@ Generated by [AVA](https://avajs.dev).
children: text␊
});␊
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICBjb25zdCBCdXR0b24gPSAoeyB0ZXh0IH0pID0+IChcbiAgICA8ZGl2PlxuICAgICAge3RleHR9XG4gICAgPC9kaXY+XG4gIClcbiJdLCJuYW1lcyI6WyJCdXR0b24iLCJ0ZXh0IiwiZGl2Il0sInJhbmdlTWFwcGluZ3MiOiI7Ozs7OzsiLCJtYXBwaW5ncyI6Ijs7Ozs7QUFDRSxNQUFNQSxTQUFTLENBQUMsRUFBRUMsSUFBSSxFQUFFLGlCQUN0QixxQkFBQ0M7a0JBQ0VEIn0=`
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICBjb25zdCBCdXR0b24gPSAoeyB0ZXh0IH0pID0+IChcbiAgICA8ZGl2PlxuICAgICAge3RleHR9XG4gICAgPC9kaXY+XG4gIClcbiJdLCJuYW1lcyI6WyJCdXR0b24iLCJ0ZXh0IiwiZGl2Il0sIm1hcHBpbmdzIjoiOzs7OztBQUNFLE1BQU1BLFNBQVMsQ0FBQyxFQUFFQyxJQUFJLEVBQUUsaUJBQ3RCLHFCQUFDQztrQkFDRUQifQ==`
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ Generated by [AVA](https://avajs.dev).

`Error: ␊
at /packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts:27:5␊
at Test.callFn (file:///node_modules/.pnpm/[email protected].2_encoding@0.1.13/node_modules/ava/lib/test.js:525:26)␊
at Test.run (file:///node_modules/.pnpm/[email protected].2_encoding@0.1.13/node_modules/ava/lib/test.js:534:33)␊
at Runner.runSingle (file:///node_modules/.pnpm/[email protected].2_encoding@0.1.13/node_modules/ava/lib/runner.js:281:33)␊
at Runner.runTest (file:///node_modules/.pnpm/[email protected].2_encoding@0.1.13/node_modules/ava/lib/runner.js:363:30)␊
at Test.callFn (file:///node_modules/.pnpm/[email protected].3_encoding@0.1.13/node_modules/ava/lib/test.js:525:26)␊
at Test.run (file:///node_modules/.pnpm/[email protected].3_encoding@0.1.13/node_modules/ava/lib/test.js:534:33)␊
at Runner.runSingle (file:///node_modules/.pnpm/[email protected].3_encoding@0.1.13/node_modules/ava/lib/runner.js:281:33)␊
at Runner.runTest (file:///node_modules/.pnpm/[email protected].3_encoding@0.1.13/node_modules/ava/lib/runner.js:363:30)␊
at processTicksAndRejections (node:internal/process/task_queues:95:5)␊
at async Promise.all (index 0)␊
at file:///node_modules/.pnpm/[email protected].2_encoding@0.1.13/node_modules/ava/lib/runner.js:528:21␊
at Runner.start (file:///node_modules/.pnpm/[email protected].2_encoding@0.1.13/node_modules/ava/lib/runner.js:536:15)`
at file:///node_modules/.pnpm/[email protected].3_encoding@0.1.13/node_modules/ava/lib/runner.js:528:21␊
at Runner.start (file:///node_modules/.pnpm/[email protected].3_encoding@0.1.13/node_modules/ava/lib/runner.js:536:15)`
Binary file not shown.
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"
}
}
2 changes: 1 addition & 1 deletion packages/jest/__test__/hoist-top-level.spec.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ Generated by [AVA](https://avajs.dev).
jest.unmock('./bar/foo').dontMock('./bar/bar');␊
};␊
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImplc3Quc3BlYy50cyJdLCJzb3VyY2VzQ29udGVudCI6WyJcbmNvbnN0IGZvbyA9ICdmb28nXG5jb25zb2xlLmxvZyhmb28pXG5qZXN0LmVuYWJsZUF1dG9tb2NrKClcbmplc3QuZGlzYWJsZUF1dG9tb2NrKClcbmplc3QubW9jaygnLi9mb28nKVxuamVzdC5tb2NrKCcuL2Zvby9iYXInLCAoKSA9PiAnYmFyJylcbmplc3QudW5tb2NrKCcuL2Jhci9mb28nKS5kb250TW9jaygnLi9iYXIvYmFyJylcbmplc3QuZGVlcFVubW9jaygnLi9mb28nKVxuamVzdC5tb2NrKCcuL2ZvbycpLm1vY2soJy4vYmFyJylcbmNvbnN0IGZ1bmMgPSAoKSA9PiB7XG4gIGNvbnN0IGJhciA9ICdiYXInXG4gIGNvbnNvbGUubG9nKGJhcilcbiAgamVzdC51bm1vY2soJy4vZm9vJylcbiAgamVzdC5tb2NrKCcuL2JhcicpXG4gIGplc3QubW9jaygnLi9iYXIvZm9vJywgKCkgPT4gJ2ZvbycpXG4gIGplc3QudW5tb2NrKCcuL2Zvby9iYXInKVxuICBqZXN0LnVubW9jaygnLi9iYXIvZm9vJykuZG9udE1vY2soJy4vYmFyL2JhcicpXG4gIGplc3QuZGVlcFVubW9jaygnLi9iYXInKVxuICBqZXN0Lm1vY2soJy4vZm9vJykubW9jaygnLi9iYXInKVxufVxuY29uc3QgZnVuYzIgPSAoKSA9PiB7XG4gIGNvbnN0IGJhciA9ICdiYXInXG4gIGNvbnNvbGUubG9nKGJhcilcbiAgamVzdC5tb2NrKCcuL2JhcicpXG4gIGplc3QudW5tb2NrKCcuL2Zvby9iYXInKVxuICBqZXN0Lm1vY2soJy4vYmFyL2ZvbycsICgpID0+ICdmb28nKVxuICBqZXN0LnVubW9jaygnLi9mb28nKVxuICBqZXN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UtHLElBQUksQ0FBQyxhQUFhLElBQU07SUFDN0JILEtBQUtRLE1BQU0sQ0FBQztJQUVaUixLQUFLSSxVQUFVLENBQUM7SUFDaEJKLEtBQUtHLElBQUksQ0FBQyxTQUFTQSxJQUFJLENBQUM7SUFSeEIsTUFBTVEsTUFBTTtJQUNaTCxRQUFRQyxHQUFHLENBQUNJO0lBS1pYLEtBQUtRLE1BQU0sQ0FBQyxhQUFhQyxRQUFRLENBQUM7QUFHcEM7QUFDQSxNQUFNRyxRQUFRO0lBR1paLEtBQUtHLElBQUksQ0FBQztJQUNWSCxLQUFLUSxNQUFNLENBQUM7SUFDWlIsS0FBS0csSUFBSSxDQUFDLGFBQWEsSUFBTTtJQUM3QkgsS0FBS1EsTUFBTSxDQUFDO0lBRVpSLEtBQUtJLFVBQVUsQ0FBQztJQUNoQkosS0FBS0csSUFBSSxDQUFDLFNBQVNBLElBQUksQ0FBQztJQVJ4QixNQUFNUSxNQUFNO0lBQ1pMLFFBQVFDLEdBQUcsQ0FBQ0k7SUFLWlgsS0FBS1EsTUFBTSxDQUFDLGFBQWFDLFFBQVEsQ0FBQztBQUdwQyJ9`
Binary file modified packages/jest/__test__/hoist-top-level.spec.ts.snap
Binary file not shown.
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"
}
}
51 changes: 35 additions & 16 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,27 @@ 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',
},
conditionNames: ['node', 'import'],
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 +186,27 @@ 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)
path &&
((process.platform !== 'win32' && !path.includes('/node_modules/')) ||
(process.platform === 'win32' && !path.includes('\\node_modules\\')))
) {
debug('resolved: typescript', specifier, resolvedModule.resolvedFileName)
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