Skip to content

Commit

Permalink
fix: fix cjs external
Browse files Browse the repository at this point in the history
  • Loading branch information
haoqunjiang committed Jun 22, 2022
1 parent 639d363 commit fd88ff9
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import colors from 'picocolors'
import type { Alias, AliasOptions } from 'types/alias'
import aliasPlugin from '@rollup/plugin-alias'
import { build } from 'esbuild'
import type { Plugin as ESBuildPlugin } from 'esbuild'
import type { RollupOptions } from 'rollup'
import type { Plugin } from './plugin'
import type { BuildOptions, ResolvedBuildOptions } from './build'
Expand Down Expand Up @@ -968,6 +969,33 @@ export function isDepsOptimizerEnabled(config: ResolvedConfig): boolean {
)
}

// esbuild doesn't transpile `require('foo')` into `import` statements if 'foo' is externalized
// https://github.com/evanw/esbuild/issues/566#issuecomment-735551834
function esbuildCjsExternalPlugin(externals: string[]): ESBuildPlugin {
return {
name: 'cjs-external',
setup(build) {
const escape = (text: string) =>
`^${text.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}$`
const filter = new RegExp(externals.map(escape).join('|'))

build.onResolve({ filter: /.*/, namespace: 'external' }, (args) => ({
path: args.path,
external: true
}))

build.onResolve({ filter }, (args) => ({
path: args.path,
namespace: 'external'
}))

build.onLoad({ filter: /.*/, namespace: 'external' }, (args) => ({
contents: `export * from ${JSON.stringify(args.path)}`
}))
}
}
}

// Support `rollupOptions.external` when `legacy.buildRollupPluginCommonjs` is disabled
function externalConfigCompat(config: UserConfig, { command }: ConfigEnv) {
// Only affects the build command
Expand Down Expand Up @@ -1011,8 +1039,8 @@ function externalConfigCompat(config: UserConfig, { command }: ConfigEnv) {
exclude: normalizedExternal as string[],
esbuildOptions: {
plugins: [
// TODO: configure `optimizeDeps.esbuildOptions.plugins`
// TODO: maybe it can be added unconditionally?
// TODO: maybe it can be added globally/unconditionally?
esbuildCjsExternalPlugin(normalizedExternal as string[])
]
}
}
Expand Down

0 comments on commit fd88ff9

Please sign in to comment.