Skip to content
Closed
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
12 changes: 12 additions & 0 deletions packages/vite/src/node/optimizer/rolldownDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const externalTypes = [

const optionalPeerDepNamespace = 'optional-peer-dep:'
const browserExternalNamespace = 'browser-external:'
const browserExternalEmptyNamespace = `${browserExternalNamespace}empty:`

export function rolldownDepPlugin(
environment: Environment,
Expand Down Expand Up @@ -121,6 +122,11 @@ export function rolldownDepPlugin(
}

const resolveResult = (id: string, resolved: string, kind: ImportKind) => {
if (resolved === browserExternalId) {
return {
id: browserExternalEmptyNamespace + id,
}
}
if (resolved.startsWith(browserExternalId)) {
return {
id: browserExternalNamespace + id,
Expand Down Expand Up @@ -260,6 +266,12 @@ export function rolldownDepPlugin(
],
},
handler(id) {
if (id.startsWith(browserExternalEmptyNamespace)) {
return {
code: 'module.exports = {}',
}
}

if (id.startsWith(browserExternalNamespace)) {
const path = id.slice(browserExternalNamespace.length)
if (isProduction) {
Expand Down
13 changes: 13 additions & 0 deletions playground/optimize-deps/__tests__/optimize-deps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ test('cjs browser field bare', async () => {
.toBe('pong')
})

test.runIf(isServe)(
'cjs browser field false mapping does not warn when a property is accessed',
() => {
expect(
browserLogs.some((log) =>
log.includes(
'Cannot access "fs.__explicitBrowserFieldTest" in client code.',
),
),
).toBe(false)
},
)

test('dep from linked dep (lodash-es)', async () => {
await expect.poll(() => page.textContent('.deps-linked')).toBe('fooBarBaz')
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

// eslint-disable-next-line n/prefer-node-protocol
const events = require('events')
// eslint-disable-next-line n/prefer-node-protocol
const fs = require('fs')

module.exports = 'foo' in events ? 'pong' : ''
module.exports =
'foo' in events && fs.__explicitBrowserFieldTest === undefined ? 'pong' : ''
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "0.0.0",
"main": "index.js",
"browser": {
"events": "./events-shim.js"
"events": "./events-shim.js",
"fs": false
}
}
Loading