Skip to content

Commit 526cf23

Browse files
authored
fix(ssr): mark builtin modules as side effect free (#15658)
1 parent 110e2e1 commit 526cf23

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

packages/vite/src/node/plugins/resolve.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,9 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
407407
this.error(message)
408408
}
409409

410-
return options.idOnly ? id : { id, external: true }
410+
return options.idOnly
411+
? id
412+
: { id, external: true, moduleSideEffects: false }
411413
} else {
412414
if (!asSrc) {
413415
debug?.(

playground/ssr-resolve/__tests__/ssr-resolve.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ test.runIf(isBuild)('correctly resolve entrypoints', async () => {
2525

2626
await expect(import(`${testDir}/dist/main.mjs`)).resolves.toBeTruthy()
2727
})
28+
29+
test.runIf(isBuild)(
30+
'node builtins should not be bundled if not used',
31+
async () => {
32+
const contents = readFile('dist/main.mjs')
33+
expect(contents).not.include(`node:url`)
34+
},
35+
)

playground/ssr-resolve/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import fileEntry from '@vitejs/test-entries/file'
66
import pkgExportsEntry from '@vitejs/test-resolve-pkg-exports/entry'
77
import deepFoo from '@vitejs/test-deep-import/foo'
88
import deepBar from '@vitejs/test-deep-import/bar'
9+
import { used } from './util'
910

1011
export default `
1112
entries/dir: ${dirEntry}
1213
entries/file: ${fileEntry}
1314
pkg-exports/entry: ${pkgExportsEntry}
1415
deep-import/foo: ${deepFoo}
1516
deep-import/bar: ${deepBar}
17+
util: ${used(['[success]'])}
1618
`

playground/ssr-resolve/util.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { pathToFileURL } from 'node:url'
2+
3+
export function used(s) {
4+
return s
5+
}
6+
7+
// This is not used, so `node:url` should not be bundled
8+
export function treeshaken(s) {
9+
return pathToFileURL(s)
10+
}

0 commit comments

Comments
 (0)