Skip to content

Commit 804c9a3

Browse files
committed
fix: only allow built-ins as externals if building for ssr
1 parent b5b4e0f commit 804c9a3

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

packages/vite/src/node/build.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'fs'
22
import path from 'path'
3+
import chalk from 'chalk'
34
import { resolveConfig, UserConfig, ResolvedConfig } from './config'
45
import Rollup, {
56
Plugin,
@@ -13,7 +14,6 @@ import Rollup, {
1314
} from 'rollup'
1415
import { buildReporterPlugin } from './plugins/reporter'
1516
import { buildDefinePlugin } from './plugins/define'
16-
import chalk from 'chalk'
1717
import { buildHtmlPlugin } from './plugins/html'
1818
import { buildEsbuildPlugin } from './plugins/esbuild'
1919
import { terserPlugin } from './plugins/terser'
@@ -110,6 +110,10 @@ export interface BuildOptions {
110110
* configurations that are suitable for distributing libraries.
111111
*/
112112
lib?: LibraryOptions | false
113+
/**
114+
* @internal for now
115+
*/
116+
ssr?: boolean
113117
}
114118

115119
export interface LibraryOptions {
@@ -137,6 +141,7 @@ export function resolveBuildOptions(
137141
write: true,
138142
manifest: false,
139143
lib: false,
144+
ssr: false,
140145
...raw
141146
}
142147

@@ -344,10 +349,19 @@ export function onRollupWarning(
344349
allowNodeBuiltins: string[] = [],
345350
userOnWarn?: WarningHandlerWithDefault
346351
) {
352+
function doWarn() {
353+
if (userOnWarn) {
354+
userOnWarn(warning, warn)
355+
} else {
356+
warn(warning)
357+
}
358+
}
359+
347360
if (warning.code === 'UNRESOLVED_IMPORT') {
348361
let message: string
349362
const id = warning.source
350363
const importer = warning.importer
364+
351365
if (id && isBuiltin(id)) {
352366
let importingDep
353367
if (importer) {
@@ -377,6 +391,7 @@ export function onRollupWarning(
377391
}
378392
throw new Error(message)
379393
}
394+
380395
if (
381396
warning.plugin === 'rollup-plugin-dynamic-import-variables' &&
382397
dynamicImportWarningIgnoreList.some((msg) => warning.message.includes(msg))
@@ -385,10 +400,6 @@ export function onRollupWarning(
385400
}
386401

387402
if (!warningIgnoreList.includes(warning.code!)) {
388-
if (userOnWarn) {
389-
userOnWarn(warning, warn)
390-
} else {
391-
warn(warning)
392-
}
403+
doWarn()
393404
}
394405
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
fsPathFromId,
1616
resolveFrom
1717
} from '../utils'
18-
import { ViteDevServer } from '..'
18+
import { ResolvedConfig, ViteDevServer } from '..'
1919
import slash from 'slash'
2020
import { createFilter } from '@rollup/pluginutils'
2121
import { PartialResolvedId } from 'rollup'
@@ -47,7 +47,8 @@ export function resolvePlugin({
4747
asSrc,
4848
dedupe
4949
}: ResolveOptions): Plugin {
50-
let server: ViteDevServer
50+
let config: ResolvedConfig | undefined
51+
let server: ViteDevServer | undefined
5152

5253
return {
5354
name: 'vite:resolve',
@@ -56,9 +57,18 @@ export function resolvePlugin({
5657
server = _server
5758
},
5859

60+
configResolved(_config) {
61+
config = _config
62+
},
63+
5964
resolveId(id, importer) {
6065
let res
6166

67+
// fast path for commonjs proxy modules
68+
if (/\?commonjs/.test(id) || id === 'commonjsHelpers.js') {
69+
return
70+
}
71+
6272
// explicit fs paths that starts with /@fs/*
6373
if (asSrc && id.startsWith(FS_PREFIX)) {
6474
const fsPath = fsPathFromId(id)
@@ -117,7 +127,8 @@ export function resolvePlugin({
117127

118128
// bare package imports, perform node resolve
119129
if (bareImportRE.test(id)) {
120-
if (isBuild && isBuiltin(id)) {
130+
// externalize node built-ins only when building for ssr
131+
if (isBuild && config && config.build.ssr && isBuiltin(id)) {
121132
return {
122133
id,
123134
external: true

0 commit comments

Comments
 (0)