Skip to content

Commit

Permalink
fix: only apply vite-specific global replacements to non-dep code
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 22, 2020
1 parent 1be6121 commit b96ed68
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/node/build/buildPluginReplace.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>,
sourcemap: boolean
): Plugin => {
Expand All @@ -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
Expand Down
35 changes: 27 additions & 8 deletions src/node/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -204,11 +218,7 @@ export async function build(options: BuildConfig = {}): Promise<BuildResult> {
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,
Expand All @@ -219,10 +229,19 @@ export async function build(options: BuildConfig = {}): Promise<BuildResult> {
// - 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
Expand Down
9 changes: 2 additions & 7 deletions src/node/depOptimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -214,16 +214,11 @@ export async function optimizeDeps(
}, {} as Record<string, string>)

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)),
Expand Down

0 comments on commit b96ed68

Please sign in to comment.