Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 19, 2024
Merged
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
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,9 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
this.error(message)
}

return options.idOnly ? id : { id, external: true }
return options.idOnly
? id
: { id, external: true, moduleSideEffects: false }
} else {
if (!asSrc) {
debug?.(
Expand Down
8 changes: 8 additions & 0 deletions playground/ssr-resolve/__tests__/ssr-resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ test.runIf(isBuild)('correctly resolve entrypoints', async () => {

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`)
},
)
2 changes: 2 additions & 0 deletions playground/ssr-resolve/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import fileEntry from '@vitejs/test-entries/file'
import pkgExportsEntry from '@vitejs/test-resolve-pkg-exports/entry'
import deepFoo from '@vitejs/test-deep-import/foo'
import deepBar from '@vitejs/test-deep-import/bar'
import { used } from './util'

export default `
entries/dir: ${dirEntry}
entries/file: ${fileEntry}
pkg-exports/entry: ${pkgExportsEntry}
deep-import/foo: ${deepFoo}
deep-import/bar: ${deepBar}
util: ${used(['[success]'])}
`
10 changes: 10 additions & 0 deletions playground/ssr-resolve/util.js
Original file line number Diff line number Diff line change
@@ -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)
}