diff --git a/src/node/build/buildPluginReplace.ts b/src/node/build/buildPluginReplace.ts index 64df2fb9738029..b727050af7a143 100644 --- a/src/node/build/buildPluginReplace.ts +++ b/src/node/build/buildPluginReplace.ts @@ -1,9 +1,8 @@ import { Plugin, TransformResult } from 'rollup' import MagicString from 'magic-string' -const filter = /\.(j|t)sx?$/ - export const createReplacePlugin = ( + test: (id: string) => boolean, replacements: Record, sourcemap: boolean ): Plugin => { @@ -21,7 +20,7 @@ export const createReplacePlugin = ( return { name: 'vite:replace', transform(code, id) { - if (filter.test(id)) { + if (test(id)) { const s = new MagicString(code) let hasReplaced = false let match diff --git a/src/node/build/index.ts b/src/node/build/index.ts index dc91779bf5e2ae..283b0545e95886 100644 --- a/src/node/build/index.ts +++ b/src/node/build/index.ts @@ -3,7 +3,13 @@ import fs from 'fs-extra' import chalk from 'chalk' import { Ora } from 'ora' import { resolveFrom } from '../utils' -import { rollup as Rollup, RollupOutput, ExternalOption, Plugin } from 'rollup' +import { + rollup as Rollup, + RollupOutput, + ExternalOption, + Plugin, + InputOptions +} from 'rollup' import { createResolver, supportedExts, InternalResolver } from '../resolver' import { createBuildResolvePlugin } from './buildPluginResolve' import { createBuildHtmlPlugin } from './buildPluginHtml' @@ -37,6 +43,14 @@ const writeColors = { [WriteType.SOURCE_MAP]: chalk.gray } +const warningIgnoreList = [`CIRCULAR_DEPENDENCY`, `THIS_IS_UNDEFINED`] + +export const onRollupWarning: InputOptions['onwarn'] = (warning, warn) => { + if (!warningIgnoreList.includes(warning.code!)) { + warn(warning) + } +} + /** * Named exports detection logic from Snowpack * MIT License @@ -204,11 +218,7 @@ export async function build(options: BuildConfig = {}): Promise { input: path.resolve(root, 'index.html'), preserveEntrySignatures: false, treeshake: { moduleSideEffects: 'no-external' }, - onwarn(warning, warn) { - if (warning.code !== 'CIRCULAR_DEPENDENCY') { - warn(warning) - } - }, + onwarn: onRollupWarning, ...rollupInputOptions, plugins: [ ...basePlugins, @@ -219,10 +229,19 @@ export async function build(options: BuildConfig = {}): Promise { // - which makes it impossible to exclude Vue templates from it since // Vue templates are compiled into js and included in chunks. createReplacePlugin( + (id) => /\.(j|t)sx?$/.test(id), { ...envReplacements, - 'process.env.': `({}).`, - __DEV__: 'false', + 'process.env.': `({}).` + }, + sourcemap + ), + // for vite spcific replacements, make sure to only apply them to + // non-dependency code to avoid collision (e.g. #224 antd has __DEV__) + createReplacePlugin( + (id) => !id.includes('node_modules') && /\.(j|t)sx?$/.test(id), + { + __DEV__: `false`, __BASE__: JSON.stringify(publicBasePath) }, sourcemap diff --git a/src/node/depOptimizer.ts b/src/node/depOptimizer.ts index 6594c5dcefc25f..7307284aefb7d0 100644 --- a/src/node/depOptimizer.ts +++ b/src/node/depOptimizer.ts @@ -8,7 +8,7 @@ import { supportedExts, resolveNodeModuleEntry } from './resolver' -import { createBaseRollupPlugins } from './build' +import { createBaseRollupPlugins, onRollupWarning } from './build' import { resolveFrom, lookupFile } from './utils' import { init, parse } from 'es-module-lexer' import chalk from 'chalk' @@ -214,16 +214,11 @@ export async function optimizeDeps( }, {} as Record) const rollup = require('rollup') as typeof Rollup - const warningIgnoreList = [`CIRCULAR_DEPENDENCY`, `THIS_IS_UNDEFINED`] const bundle = await rollup.rollup({ input, external: preservedDeps, treeshake: { moduleSideEffects: 'no-external' }, - onwarn(warning, warn) { - if (!warningIgnoreList.includes(warning.code!)) { - warn(warning) - } - }, + onwarn: onRollupWarning, ...config.rollupInputOptions, plugins: [ ...(await createBaseRollupPlugins(root, resolver, config)),