Skip to content

Commit

Permalink
fix: dependency scan with esbuild when using non-HTML entrypoints (#1772
Browse files Browse the repository at this point in the history
)

fix #1763
  • Loading branch information
ElMassimo authored Jan 28, 2021
1 parent 7e726a6 commit ca862a2
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { init, parse } from 'es-module-lexer'
import MagicString from 'magic-string'
import { transformImportGlob } from '../importGlob'
import { isCSSRequest } from '../plugins/css'

const debug = createDebugger('vite:deps')

Expand Down Expand Up @@ -54,6 +55,11 @@ export async function scanImports(
entries = await globEntries('**/*.html', config)
}

// CSS/Asset entrypoints should not be scanned for dependencies.
entries = entries.filter(
(entry) => !(isCSSRequest(entry) || config.assetsInclude(entry))
)

if (!entries.length) {
debug(`No entry HTML files detected`)
return { deps: {}, missing: {} }
Expand All @@ -64,7 +70,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 +116,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 +141,11 @@ function esbuildScanPlugin(
const include = config.optimizeDeps?.include
const exclude = config.optimizeDeps?.exclude

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

return {
name: 'vite:dep-scan',
setup(build) {
Expand Down Expand Up @@ -191,23 +203,23 @@ 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
build.onResolve(
{
filter: /\?(worker|raw)\b/
},
({ path }) => ({ path, external: true })
externalUnlessEntry
)

// bare imports: record and externalize
Expand All @@ -218,17 +230,11 @@ 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 { path: id, external: true }
}

const resolved = await resolve(id, importer)
Expand All @@ -246,10 +252,7 @@ function esbuildScanPlugin(
if (!exclude?.includes(id)) {
depImports[id] = resolved
}
return {
path: id,
external: true
}
return externalUnlessEntry({ path: id })
} else {
// linked package, keep crawling
return {
Expand All @@ -271,15 +274,17 @@ function esbuildScanPlugin(
// use vite resolver to support urls and omitted extensions
const resolved = await resolve(id, importer)
if (resolved && resolved !== id) {
// in case user has configured to externalize additional assets
if (config.assetsInclude(id)) {
return { path: id, external: true }
}
return {
path: path.resolve(cleanUrl(resolved))
}
} 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 ca862a2

Please sign in to comment.