Skip to content

Commit

Permalink
fix(optimizer): externalize cross-package imported css
Browse files Browse the repository at this point in the history
fix #1722
  • Loading branch information
yyx990803 committed Jan 26, 2021
1 parent 1a9b321 commit 0599908
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,41 @@ export function esbuildDepPlugin(
return {
name: 'vite:dep-pre-bundle',
setup(build) {
async function useViteResolve(id: string, importer: string) {
id = (await aliasResolver.resolveId(id))?.id || id
const resolved = tryNodeResolve(
id,
path.dirname(importer),
false,
true,
config.dedupe,
config.root
)
if (resolved) {
return path.resolve(resolved.id)
}
}

// externalize assets and commonly known non-js file types
build.onResolve(
{
filter: new RegExp(`\\.(` + externalTypes.join('|') + `)(\\?.*)?$`)
},
({ path: _path, importer }) => {
if (_path.startsWith('.')) {
async ({ path: id, importer }) => {
if (id.startsWith('.')) {
const dir = path.dirname(importer)
return {
path: path.resolve(dir, _path),
path: path.resolve(dir, id),
external: true
}
} else {
const resolved = await useViteResolve(id, importer)
if (resolved) {
return {
path: resolved,
external: true
}
}
}
}
)
Expand All @@ -58,19 +81,11 @@ export function esbuildDepPlugin(
const deepMatch = id.match(deepImportRE)
const pkgId = deepMatch ? deepMatch[1] || deepMatch[2] : id
transitiveOptimized[pkgId] = true

id = (await aliasResolver.resolveId(id))?.id || id
const resolved = tryNodeResolve(
id,
path.dirname(importer),
false,
true,
config.dedupe,
config.root
)
// use vite resolver
const resolved = await useViteResolve(id, importer)
if (resolved) {
return {
path: path.resolve(resolved.id)
path: resolved
}
}
} else {
Expand Down

0 comments on commit 0599908

Please sign in to comment.