Skip to content

Commit b1dd232

Browse files
lazueesxzz
andauthored
fix(node-protocol): respect alias and tsconfig paths when stripping node: prefix (#773)
* fix(node-protocol): respect alias and tsconfig paths when stripping node: prefix * refactor * fix(tests): update assertions to use regex for path and crypto imports * refactor: reorder test cases --------- Co-authored-by: Kevin Deng <[email protected]>
1 parent afaabb9 commit b1dd232

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

src/features/node-protocol.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,18 @@ export function NodeProtocolPlugin(nodeProtocolOption: 'strip' | true): Plugin {
2222
},
2323
handler:
2424
nodeProtocolOption === 'strip'
25-
? (id) => {
25+
? async function (id, ...args) {
26+
// strip the `node:` prefix
27+
const strippedId = id.slice(5 /* "node:".length */)
28+
29+
// check if another resolver (e.g., tsconfig paths, alias) handles the stripped id
30+
const resolved = await this.resolve(strippedId, ...args)
31+
if (resolved && !resolved.external) {
32+
return resolved
33+
}
34+
2635
return {
27-
// strip the `node:` prefix
28-
id: id.slice(5),
36+
id: strippedId,
2937
external: true,
3038
moduleSideEffects: false,
3139
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## index.mjs
2+
3+
```mjs
4+
//#region crypto-polyfill.ts
5+
function randomUUID() {
6+
return "polyfill-uuid";
7+
}
8+
9+
//#endregion
10+
//#region path-polyfill.ts
11+
function resolve(...args) {
12+
return args.join("/");
13+
}
14+
15+
//#endregion
16+
//#region index.ts
17+
const id = randomUUID();
18+
const dir = resolve(".");
19+
20+
//#endregion
21+
export { dir, id };
22+
```

tests/issues.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,30 @@ describe('issues', () => {
193193
expect(fileMap['entry1.css']).toContain('class-shared')
194194
expect(fileMap['entry2.css']).toContain('class-shared')
195195
})
196+
197+
test('#772', async (context) => {
198+
const { fileMap, outputFiles } = await testBuild({
199+
context,
200+
files: {
201+
'index.ts': `import { randomUUID } from 'node:crypto'\nimport { resolve } from 'node:path'\nexport const id = randomUUID()\nexport const dir = resolve('.')`,
202+
'crypto-polyfill.ts': `export function randomUUID() { return 'polyfill-uuid' }`,
203+
'path-polyfill.ts': `export function resolve(...args: string[]) { return args.join('/') }`,
204+
'tsconfig.json': JSON.stringify({
205+
compilerOptions: {
206+
paths: { crypto: ['./crypto-polyfill'] },
207+
},
208+
}),
209+
},
210+
options: {
211+
nodeProtocol: 'strip',
212+
alias: { path: './path-polyfill' },
213+
tsconfig: 'tsconfig.json',
214+
},
215+
})
216+
expect(outputFiles).toContain('index.mjs')
217+
expect(fileMap['index.mjs']).toContain('args.join')
218+
expect(fileMap['index.mjs']).not.toMatch(/from ['"]path['"]/)
219+
expect(fileMap['index.mjs']).toContain('polyfill-uuid')
220+
expect(fileMap['index.mjs']).not.toMatch(/from ['"]crypto['"]/)
221+
})
196222
})

0 commit comments

Comments
 (0)