Skip to content

Commit

Permalink
fix(ssr): mark builtin modules as side effect free (#15658)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Jan 19, 2024
1 parent 110e2e1 commit 526cf23
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
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)
}

0 comments on commit 526cf23

Please sign in to comment.