Skip to content

Commit 20abe92

Browse files
authored
feat(register): respect conditions from Node.js (#955)
* feat(register): respect conditions from Node.js * Update readme
1 parent ca6e80c commit 20abe92

File tree

10 files changed

+57
-19
lines changed

10 files changed

+57
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Run TypeScript with node, without compilation or typechecking:
1717
```bash
1818
npm i -D @swc-node/register
1919
node -r @swc-node/register script.ts
20-
node --import @swc-node/register/esm-register script.ts # for esm project with node>=20.6
20+
node --import @swc-node/register/esm-register --enable-source-maps script.ts # for esm project with node>=20.6
2121
node --loader @swc-node/register/esm script.ts # for esm project with node<=20.5, deprecated
2222
```
2323

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"lint": "oxlint .",
3232
"test": "ava",
3333
"test:jest": "jest --config jest.config.js",
34-
"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",
34+
"test:module": "cross-env SWC_NODE_PROJECT=packages/integrate-module/tsconfig.json node --enable-source-maps --conditions=dev --import=@swc-node/register/esm-register packages/integrate-module/src/index.ts",
3535
"version": "pnpm install && git add .",
3636
"postinstall": "husky"
3737
},

packages/conditions/dev.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const name = 'from-dev'

packages/conditions/import.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const name = 'from-import'

packages/conditions/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "module",
3+
"name": "@swc-node/test-conditions",
4+
"private": true,
5+
"types": "./types.d.ts",
6+
"exports": {
7+
".": {
8+
"dev": "./dev.js",
9+
"import": "./index.js"
10+
}
11+
}
12+
}

packages/conditions/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const name: string

packages/integrate-module/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"@napi-rs/simple-git": "^0.1.22",
1212
"@swc/core": "^1.13.3",
1313
"@swc-node/register": "workspace:*",
14+
"@swc-node/test-conditions": "workspace:*",
1415
"@types/react": "^19.1.10",
1516
"@types/react-dom": "^19.1.7",
1617
"esmock": "^2.7.1",

packages/integrate-module/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import test from 'node:test'
44

55
import { RepositoryState } from '@napi-rs/simple-git'
66
import { bar as subBar } from '@subdirectory/bar.mjs'
7+
import { name } from '@swc-node/test-conditions'
78
import esmock from 'esmock'
89
import { supportedExtensions } from 'file-type'
910
import { renderToString } from 'react-dom/server'
@@ -97,3 +98,7 @@ await test('postgres should work', async () => {
9798
host: 'postgres://localhost',
9899
})
99100
})
101+
102+
await test('resolve conditions', () => {
103+
assert.equal(name, 'from-dev')
104+
})

packages/register/esm.mts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { extname, isAbsolute, join } from 'node:path'
1111
import { fileURLToPath, URL, pathToFileURL } from 'node:url'
1212

1313
import debugFactory from 'debug'
14-
import { EnforceExtension, ResolverFactory } from 'oxc-resolver'
14+
import { EnforceExtension, ResolverFactory, type NapiResolveOptions } from 'oxc-resolver'
1515
import ts from 'typescript'
1616

1717
// @ts-expect-error
@@ -35,22 +35,6 @@ const TSCONFIG_PATH = (function () {
3535
return pathFromEnv
3636
})()
3737

38-
const resolver = new ResolverFactory({
39-
tsconfig: {
40-
configFile: TSCONFIG_PATH,
41-
references: 'auto',
42-
},
43-
conditionNames: ['node', 'import'],
44-
enforceExtension: EnforceExtension.Auto,
45-
extensions: ['.js', '.mjs', '.cjs', '.ts', '.tsx', '.mts', '.cts', '.json', '.wasm', '.node'],
46-
extensionAlias: {
47-
'.js': ['.ts', '.tsx', '.js'],
48-
'.mjs': ['.mts', '.mjs'],
49-
'.cjs': ['.cts', '.cjs'],
50-
},
51-
moduleType: true,
52-
})
53-
5438
async function getModuleType(path: string): Promise<'module' | 'commonjs' | undefined> {
5539
const pkgJsonReadContent = await readPackageJSON(path)
5640
return pkgJsonReadContent?.type
@@ -152,9 +136,37 @@ const EXTENSION_MODULE_MAP = {
152136
'.node': 'commonjs',
153137
} as const
154138

139+
let conditions: string[] | undefined = undefined
140+
141+
const resolverOptions: NapiResolveOptions = {
142+
tsconfig: {
143+
configFile: TSCONFIG_PATH,
144+
references: 'auto',
145+
},
146+
conditionNames: ['node', 'import'],
147+
enforceExtension: EnforceExtension.Auto,
148+
extensions: ['.js', '.mjs', '.cjs', '.ts', '.tsx', '.mts', '.cts', '.json', '.wasm', '.node'],
149+
extensionAlias: {
150+
'.js': ['.ts', '.tsx', '.js'],
151+
'.mjs': ['.mts', '.mjs'],
152+
'.cjs': ['.cts', '.cjs'],
153+
},
154+
moduleType: true,
155+
}
156+
157+
let resolver = new ResolverFactory(resolverOptions)
158+
155159
export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
156160
debug('resolve', specifier, JSON.stringify(context))
157161

162+
if (!conditions) {
163+
conditions = context.conditions
164+
resolver = resolver.cloneWithOptions({
165+
...resolverOptions,
166+
conditionNames: conditions,
167+
})
168+
}
169+
158170
if (specifier.startsWith('node:') || specifier.startsWith('nodejs:')) {
159171
debug('skip resolve: internal format', specifier)
160172

pnpm-lock.yaml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)