Skip to content

Commit 4260c75

Browse files
authored
fix(bundled-dev): allow named imports from Node built-ins (#23858)
1 parent 96d1578 commit 4260c75

4 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎packages/vite/src/node/plugins/resolve.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ export function oxcResolvePlugin(
260260
tsconfig: partialEnv.config.tsconfig,
261261
resolveOptions: {
262262
isBuild: options.isBuild,
263+
isBundled: partialEnv.config.isBundled,
263264
isProduction: options.isProduction,
264265
asSrc: options.asSrc ?? false,
265266
preferRelative: options.preferRelative ?? false,

‎playground/resolve/__tests__/resolve.spec.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'node:path'
33
import { fileURLToPath } from 'node:url'
44
import { describe, expect, test } from 'vitest'
55
import {
6+
browserErrors,
67
isBuild,
78
isBundled,
89
isBundledDev,
@@ -145,6 +146,28 @@ test('access property of browser:false module', async () => {
145146
)
146147
})
147148

149+
test('named import from a Node built-in does not break the build', async () => {
150+
if (isBundled) {
151+
await expect
152+
.poll(() => page.textContent('.node-builtin-named-import'))
153+
.toMatch('[success]')
154+
} else {
155+
// plain dev reads the named import at once, and the stub throws
156+
expect(browserErrors.map((e) => e.message)).toContainEqual(
157+
expect.stringContaining('Cannot access "node:fs.readFileSync"'),
158+
)
159+
}
160+
})
161+
162+
test.runIf(isBundled)(
163+
'reading a named import from a Node built-in',
164+
async () => {
165+
await expect
166+
.poll(() => page.textContent('.node-builtin-named-import-read'))
167+
.toMatch(isBuild ? 'undefined' : 'Cannot access "node:fs.readFileSync"')
168+
},
169+
)
170+
148171
test('Resolve browser field even if module field exists', async () => {
149172
expect(await page.textContent('.browser-module1')).toMatch('[success]')
150173
})

‎playground/resolve/index.html‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ <h2>Browser Field</h2>
162162
<p class="browser">fail</p>
163163
<p class="browser-field-false-access">fail</p>
164164

165+
<h2>Named import from a Node built-in</h2>
166+
<p class="node-builtin-named-import">fail</p>
167+
<p class="node-builtin-named-import-read"></p>
168+
<script type="module" src="./node-builtin-named-import.js"></script>
169+
165170
<h2>Resolve browser field even if module field exists</h2>
166171
<p class="browser-module1">fail</p>
167172

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { readFileSync } from 'node:fs'
2+
3+
// Plain dev turns the import above into
4+
// `const readFileSync = stub['readFileSync']`, which throws right away.
5+
// Bundled dev and build read it only here, when `read()` is called.
6+
export function read() {
7+
return readFileSync
8+
}
9+
10+
document.querySelector('.node-builtin-named-import').textContent = '[success]'
11+
12+
let readResult
13+
try {
14+
readResult = String(read())
15+
} catch (e) {
16+
readResult = e.message
17+
}
18+
document.querySelector('.node-builtin-named-import-read').textContent =
19+
readResult

0 commit comments

Comments
 (0)