Skip to content
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
11 changes: 10 additions & 1 deletion packages/vite/src/node/optimizer/rolldownDepPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path'
import MagicString from 'magic-string'
import type { ImportKind, Plugin, RolldownPlugin } from 'rolldown'
import { prefixRegex } from 'rolldown/filter'
import { exactRegex, prefixRegex } from 'rolldown/filter'
import { stripLiteral } from 'strip-literal'
import { isWindows } from '../../shared/utils'
import { JS_TYPES_RE, KNOWN_ASSET_TYPES } from '../constants'
Expand Down Expand Up @@ -121,6 +121,11 @@ export function rolldownDepPlugin(
}

const resolveResult = (id: string, resolved: string, kind: ImportKind) => {
// An exact browser-external id is an explicit browser:false mapping.
// Suffixed ids are unsupported Node builtins and still need the warning.
if (resolved === browserExternalId) {
return { id: browserExternalId }
}
if (resolved.startsWith(browserExternalId)) {
return {
id: browserExternalNamespace + id,
Expand Down Expand Up @@ -255,11 +260,15 @@ export function rolldownDepPlugin(
load: {
filter: {
id: [
exactRegex(browserExternalId),
prefixRegex(browserExternalNamespace),
prefixRegex(optionalPeerDepNamespace),
],
},
handler(id) {
if (id === browserExternalId) {
return { code: 'module.exports = {}' }
}
if (id.startsWith(browserExternalNamespace)) {
const path = id.slice(browserExternalNamespace.length)
if (isProduction) {
Expand Down
14 changes: 14 additions & 0 deletions playground/optimize-deps/__tests__/optimize-deps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ test('non optimized module is not duplicated', async () => {
.toBe('from-absolute-path, from-relative-path')
})

test.runIf(isServe)(
'optimized browser:false is empty without warning',
async () => {
expect(await page.textContent('.browser-false-optimized')).toBe('[success]')
expect(browserLogs).not.toEqual(
expect.arrayContaining([
expect.stringContaining(
'Module "browser-false-only" has been externalized for browser compatibility',
),
]),
)
},
)

test.runIf(isServe)('error on builtin modules usage', () => {
expect(browserLogs).toEqual(
expect.arrayContaining([
Expand Down
4 changes: 4 additions & 0 deletions playground/optimize-deps/dep-browser-false/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// An explicit browser:false mapping is an empty module, not a Node builtin warning.
// eslint-disable-next-line n/no-missing-require -- mapped to false by package browser field
const fs = require('browser-false-only')
module.exports = fs.readFileSync === undefined ? '[success]' : '[fail]'
9 changes: 9 additions & 0 deletions playground/optimize-deps/dep-browser-false/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@vitejs/test-dep-browser-false",
"private": true,
"version": "0.0.0",
"main": "index.js",
"browser": {
"browser-false-only": false
}
}
6 changes: 6 additions & 0 deletions playground/optimize-deps/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,12 @@ <h2>Import the CommonJS external package that omits the js suffix</h2>
}
</script>

<p class="browser-false-optimized">[pending]</p>
<script type="module">
import browserFalse from '@vitejs/test-dep-browser-false'
text('.browser-false-optimized', browserFalse)
</script>

<script type="module">
// should error on builtin modules from dep
import { read } from '@vitejs/test-dep-with-builtin-module-cjs'
Expand Down
1 change: 1 addition & 0 deletions playground/optimize-deps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@vitejs/test-dep-with-asset-ext1.pdf": "file:./dep-with-asset-ext/dep1",
"@vitejs/test-dep-with-asset-ext2.pdf": "file:./dep-with-asset-ext/dep2",
"@vitejs/test-dep-with-builtin-module-cjs": "file:./dep-with-builtin-module-cjs",
"@vitejs/test-dep-browser-false": "file:./dep-browser-false",
"@vitejs/test-dep-with-builtin-module-esm": "file:./dep-with-builtin-module-esm",
"@vitejs/test-dep-with-dynamic-import": "file:./dep-with-dynamic-import",
"@vitejs/test-dep-with-optional-peer-dep": "file:./dep-with-optional-peer-dep",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading