Skip to content

Commit

Permalink
fix: only allow built-ins as externals if building for ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 4, 2021
1 parent b5b4e0f commit 804c9a3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
23 changes: 17 additions & 6 deletions packages/vite/src/node/build.ts
@@ -1,5 +1,6 @@
import fs from 'fs'
import path from 'path'
import chalk from 'chalk'
import { resolveConfig, UserConfig, ResolvedConfig } from './config'
import Rollup, {
Plugin,
Expand All @@ -13,7 +14,6 @@ import Rollup, {
} from 'rollup'
import { buildReporterPlugin } from './plugins/reporter'
import { buildDefinePlugin } from './plugins/define'
import chalk from 'chalk'
import { buildHtmlPlugin } from './plugins/html'
import { buildEsbuildPlugin } from './plugins/esbuild'
import { terserPlugin } from './plugins/terser'
Expand Down Expand Up @@ -110,6 +110,10 @@ export interface BuildOptions {
* configurations that are suitable for distributing libraries.
*/
lib?: LibraryOptions | false
/**
* @internal for now
*/
ssr?: boolean
}

export interface LibraryOptions {
Expand Down Expand Up @@ -137,6 +141,7 @@ export function resolveBuildOptions(
write: true,
manifest: false,
lib: false,
ssr: false,
...raw
}

Expand Down Expand Up @@ -344,10 +349,19 @@ export function onRollupWarning(
allowNodeBuiltins: string[] = [],
userOnWarn?: WarningHandlerWithDefault
) {
function doWarn() {
if (userOnWarn) {
userOnWarn(warning, warn)
} else {
warn(warning)
}
}

if (warning.code === 'UNRESOLVED_IMPORT') {
let message: string
const id = warning.source
const importer = warning.importer

if (id && isBuiltin(id)) {
let importingDep
if (importer) {
Expand Down Expand Up @@ -377,6 +391,7 @@ export function onRollupWarning(
}
throw new Error(message)
}

if (
warning.plugin === 'rollup-plugin-dynamic-import-variables' &&
dynamicImportWarningIgnoreList.some((msg) => warning.message.includes(msg))
Expand All @@ -385,10 +400,6 @@ export function onRollupWarning(
}

if (!warningIgnoreList.includes(warning.code!)) {
if (userOnWarn) {
userOnWarn(warning, warn)
} else {
warn(warning)
}
doWarn()
}
}
17 changes: 14 additions & 3 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -15,7 +15,7 @@ import {
fsPathFromId,
resolveFrom
} from '../utils'
import { ViteDevServer } from '..'
import { ResolvedConfig, ViteDevServer } from '..'
import slash from 'slash'
import { createFilter } from '@rollup/pluginutils'
import { PartialResolvedId } from 'rollup'
Expand Down Expand Up @@ -47,7 +47,8 @@ export function resolvePlugin({
asSrc,
dedupe
}: ResolveOptions): Plugin {
let server: ViteDevServer
let config: ResolvedConfig | undefined
let server: ViteDevServer | undefined

return {
name: 'vite:resolve',
Expand All @@ -56,9 +57,18 @@ export function resolvePlugin({
server = _server
},

configResolved(_config) {
config = _config
},

resolveId(id, importer) {
let res

// fast path for commonjs proxy modules
if (/\?commonjs/.test(id) || id === 'commonjsHelpers.js') {
return
}

// explicit fs paths that starts with /@fs/*
if (asSrc && id.startsWith(FS_PREFIX)) {
const fsPath = fsPathFromId(id)
Expand Down Expand Up @@ -117,7 +127,8 @@ export function resolvePlugin({

// bare package imports, perform node resolve
if (bareImportRE.test(id)) {
if (isBuild && isBuiltin(id)) {
// externalize node built-ins only when building for ssr
if (isBuild && config && config.build.ssr && isBuiltin(id)) {
return {
id,
external: true
Expand Down

0 comments on commit 804c9a3

Please sign in to comment.