Skip to content
Open
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
14 changes: 11 additions & 3 deletions src/features/node-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ export function NodeProtocolPlugin(nodeProtocolOption: 'strip' | true): Plugin {
},
handler:
nodeProtocolOption === 'strip'
? (id) => {
? async function (id, ...args) {
// strip the `node:` prefix
const strippedId = id.slice(5 /* "node:".length */)

// check if another resolver (e.g., tsconfig paths, alias) handles the stripped id
const resolved = await this.resolve(strippedId, ...args)
if (resolved && !resolved.external) {
return resolved
}

return {
// strip the `node:` prefix
id: id.slice(5),
id: strippedId,
external: true,
moduleSideEffects: false,
}
Expand Down
22 changes: 22 additions & 0 deletions tests/__snapshots__/issues/772.snap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## index.mjs

```mjs
//#region crypto-polyfill.ts
function randomUUID() {
return "polyfill-uuid";
}

//#endregion
//#region path-polyfill.ts
function resolve(...args) {
return args.join("/");
}

//#endregion
//#region index.ts
const id = randomUUID();
const dir = resolve(".");

//#endregion
export { dir, id };
```
26 changes: 26 additions & 0 deletions tests/issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,32 @@ describe('issues', () => {
})
})

test('#772', async (context) => {
const { fileMap, outputFiles } = await testBuild({
context,
files: {
'index.ts': `import { randomUUID } from 'node:crypto'\nimport { resolve } from 'node:path'\nexport const id = randomUUID()\nexport const dir = resolve('.')`,
'crypto-polyfill.ts': `export function randomUUID() { return 'polyfill-uuid' }`,
'path-polyfill.ts': `export function resolve(...args: string[]) { return args.join('/') }`,
'tsconfig.json': JSON.stringify({
compilerOptions: {
paths: { crypto: ['./crypto-polyfill'] },
},
}),
},
options: {
nodeProtocol: 'strip',
alias: { path: './path-polyfill' },
tsconfig: 'tsconfig.json',
},
})
expect(outputFiles).toContain('index.mjs')
expect(fileMap['index.mjs']).toContain('args.join')
expect(fileMap['index.mjs']).not.toMatch(/from ['"]path['"]/)
expect(fileMap['index.mjs']).toContain('polyfill-uuid')
expect(fileMap['index.mjs']).not.toMatch(/from ['"]crypto['"]/)
})

test.fails('#668', async (context) => {
const { outputFiles, fileMap } = await testBuild({
context,
Expand Down
Loading