Skip to content

Commit

Permalink
fix(optimizer): handle alias to optimized entries
Browse files Browse the repository at this point in the history
fix #1780
  • Loading branch information
yyx990803 committed Jan 29, 2021
1 parent e7dd4f8 commit 81eb7a0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
31 changes: 26 additions & 5 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Expand Up @@ -3,7 +3,7 @@ import path from 'path'
import { Loader, Plugin } from 'esbuild'
import { knownAssetTypes } from '../constants'
import { ResolvedConfig } from '..'
import { isRunningWithYarnPnp } from '../utils'
import { bareImportRE, isRunningWithYarnPnp, flattenId } from '../utils'
import { browserExternalId } from '../plugins/resolve'

const externalTypes = [
Expand All @@ -26,9 +26,15 @@ export function esbuildDepPlugin(
config: ResolvedConfig
): Plugin {
const _resolve = config.createResolver({ asSrc: false })
const resolve = (id: string, importer: string) =>

const resolve = (
id: string,
importer: string
): Promise<string | undefined> => {
// map importer ids to file paths for correct resolution
_resolve(id, importer in qualified ? qualified[importer] : importer)
importer = importer in qualified ? qualified[importer] : importer
return _resolve(id, importer)
}

return {
name: 'vite:dep-pre-bundle',
Expand Down Expand Up @@ -58,9 +64,24 @@ export function esbuildDepPlugin(
// if is optimized entry, redirect to entry namespace
return {
path: id,
namespace: 'entry'
namespace: 'dep'
}
} else {
// check alias fist
const aliased = await _resolve(id, undefined, true)
if (aliased && bareImportRE.test(aliased)) {
const id = flattenId(aliased)
if (id in qualified) {
// #1780
// id was aliased to a qualified entry, use the entry to
// avoid duplicated copies of the module
return {
path: id,
namespace: 'dep'
}
}
}

// use vite resolver
const resolved = await resolve(id, importer)
if (resolved) {
Expand All @@ -81,7 +102,7 @@ export function esbuildDepPlugin(
// for entry files, we'll read it ourselves to retain the entry's raw id
// instead of file path
// so that esbuild outputs desired output file structure.
build.onLoad({ filter: /.*/, namespace: 'entry' }, ({ path: id }) => {
build.onLoad({ filter: /.*/, namespace: 'dep' }, ({ path: id }) => {
const entryFile = qualified[id]
let ext = path.extname(entryFile).slice(1)
if (ext === 'mjs') ext = 'js'
Expand Down
5 changes: 2 additions & 3 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -8,7 +8,8 @@ import {
emptyDir,
lookupFile,
normalizePath,
writeFile
writeFile,
flattenId
} from '../utils'
import { build } from 'esbuild'
import { esbuildDepPlugin } from './esbuildDepPlugin'
Expand Down Expand Up @@ -63,8 +64,6 @@ export interface DepOptimizationMetadata {
>
}

const flattenId = (id: string) => id.replace(/[\/\.]/g, '_')

export async function optimizeDeps(
config: ResolvedConfig,
force = config.server.force,
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/node/utils.ts
Expand Up @@ -10,6 +10,8 @@ import resolve from 'resolve'
import builtins from 'builtin-modules'
import { FSWatcher } from 'chokidar'

export const flattenId = (id: string) => id.replace(/[\/\.]/g, '_')

export function isBuiltin(id: string): boolean {
return builtins.includes(id)
}
Expand Down

0 comments on commit 81eb7a0

Please sign in to comment.