fix(ssr): mark builtin modules as side effect free (#15658)

This commit is contained in:
Bjorn Lu 2024-01-19 23:19:35 +08:00 committed by GitHub
parent 110e2e1651
commit 526cf23410
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 1 deletions

View File

@ -407,7 +407,9 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
this.error(message) this.error(message)
} }
return options.idOnly ? id : { id, external: true } return options.idOnly
? id
: { id, external: true, moduleSideEffects: false }
} else { } else {
if (!asSrc) { if (!asSrc) {
debug?.( debug?.(

View File

@ -25,3 +25,11 @@ test.runIf(isBuild)('correctly resolve entrypoints', async () => {
await expect(import(`${testDir}/dist/main.mjs`)).resolves.toBeTruthy() await expect(import(`${testDir}/dist/main.mjs`)).resolves.toBeTruthy()
}) })
test.runIf(isBuild)(
'node builtins should not be bundled if not used',
async () => {
const contents = readFile('dist/main.mjs')
expect(contents).not.include(`node:url`)
},
)

View File

@ -6,6 +6,7 @@ import fileEntry from '@vitejs/test-entries/file'
import pkgExportsEntry from '@vitejs/test-resolve-pkg-exports/entry' import pkgExportsEntry from '@vitejs/test-resolve-pkg-exports/entry'
import deepFoo from '@vitejs/test-deep-import/foo' import deepFoo from '@vitejs/test-deep-import/foo'
import deepBar from '@vitejs/test-deep-import/bar' import deepBar from '@vitejs/test-deep-import/bar'
import { used } from './util'
export default ` export default `
entries/dir: ${dirEntry} entries/dir: ${dirEntry}
@ -13,4 +14,5 @@ export default `
pkg-exports/entry: ${pkgExportsEntry} pkg-exports/entry: ${pkgExportsEntry}
deep-import/foo: ${deepFoo} deep-import/foo: ${deepFoo}
deep-import/bar: ${deepBar} deep-import/bar: ${deepBar}
util: ${used(['[success]'])}
` `

View File

@ -0,0 +1,10 @@
import { pathToFileURL } from 'node:url'
export function used(s) {
return s
}
// This is not used, so `node:url` should not be bundled
export function treeshaken(s) {
return pathToFileURL(s)
}