Skip to content

Commit

Permalink
fix: dependency scan with esbuild when using non-HTML entrypoints (close
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo committed Jan 28, 2021
1 parent 7e726a6 commit 2770d60
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path'
import glob from 'fast-glob'
import { ResolvedConfig } from '..'
import { build, Loader, Plugin } from 'esbuild'
import { knownAssetTypes } from '../constants'
import { knownAssetTypes, DEFAULT_ASSETS_RE } from '../constants'
import {
createDebugger,
emptyDir,
Expand Down Expand Up @@ -54,6 +54,9 @@ export async function scanImports(
entries = await globEntries('**/*.html', config)
}

// Asset entrypoints should not be scanned for dependencies.
entries = entries.filter((entry) => !DEFAULT_ASSETS_RE.test(entry))

if (!entries.length) {
debug(`No entry HTML files detected`)
return { deps: {}, missing: {} }
Expand All @@ -64,7 +67,7 @@ export async function scanImports(
const tempDir = path.join(config.optimizeCacheDir!, 'temp')
const deps: Record<string, string> = {}
const missing: Record<string, string> = {}
const plugin = esbuildScanPlugin(config, deps, missing)
const plugin = esbuildScanPlugin(config, deps, missing, entries)

await Promise.all(
entries.map((entry) =>
Expand Down Expand Up @@ -110,7 +113,8 @@ const langRE = /\blang\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/im
function esbuildScanPlugin(
config: ResolvedConfig,
depImports: Record<string, string>,
missing: Record<string, string>
missing: Record<string, string>,
entries: string[]
): Plugin {
let container: PluginContainer

Expand All @@ -134,6 +138,11 @@ function esbuildScanPlugin(
const include = config.optimizeDeps?.include
const exclude = config.optimizeDeps?.exclude

const externalUnlessEntry = ({ path }) => ({
path,
external: !entries.includes(path)
})

return {
name: 'vite:dep-scan',
setup(build) {
Expand Down Expand Up @@ -191,15 +200,15 @@ function esbuildScanPlugin(
{
filter: /\.(css|less|sass|scss|styl|stylus|postcss)$/
},
({ path }) => ({ path, external: true })
externalUnlessEntry
)

// known asset types: externalize
build.onResolve(
{
filter: new RegExp(`\\.(${knownAssetTypes.join('|')})$`)
},
({ path }) => ({ path, external: true })
externalUnlessEntry
)

// known vite query types: ?worker, ?raw
Expand All @@ -218,38 +227,29 @@ function esbuildScanPlugin(
},
async ({ path: id, importer }) => {
if (depImports[id]) {
return {
path: id,
external: true
}
return externalUnlessEntry({ path: id })
}

if (isExternalUrl(id) || isDataUrl(id)) {
return {
path: id,
external: true
}
return externalUnlessEntry({ path: id })
}

const resolved = await resolve(id, importer)
if (resolved) {
// browser external
if (resolved.startsWith(browserExternalId)) {
return { path: id, external: true }
return externalUnlessEntry({ path: id })
}
// virtual id
if (id === resolved) {
return { path: id, external: true }
return externalUnlessEntry({ path: id })
}
// dep or force included, externalize and stop crawling
if (resolved.includes('node_modules') || include?.includes(id)) {
if (!exclude?.includes(id)) {
depImports[id] = resolved
}
return {
path: id,
external: true
}
return externalUnlessEntry({ path: id })
} else {
// linked package, keep crawling
return {
Expand All @@ -276,10 +276,8 @@ function esbuildScanPlugin(
}
} else {
// resolve failed... probably usupported type
return {
path: id,
external: true
}
// or file is already resolved because it's an entry
return externalUnlessEntry({ path: id })
}
}
)
Expand Down

0 comments on commit 2770d60

Please sign in to comment.