Skip to content

Commit

Permalink
fix: scan on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 27, 2021
1 parent fd5e7c0 commit 5f7698b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 61 deletions.
129 changes: 69 additions & 60 deletions packages/vite/src/node/optimizer/scan.ts
Expand Up @@ -4,9 +4,14 @@ import glob from 'fast-glob'
import { ResolvedConfig } from '..'
import { build, Loader, Plugin } from 'esbuild'
import { knownAssetTypes } from '../constants'
import { createDebugger, emptyDir, isDataUrl, isExternalUrl } from '../utils'
import {
createDebugger,
emptyDir,
isDataUrl,
isExternalUrl,
normalizePath
} from '../utils'
import { browserExternalId } from '../plugins/resolve'
import { isCSSRequest } from '../plugins/css'
import chalk from 'chalk'
import {
createPluginContainer,
Expand Down Expand Up @@ -84,7 +89,10 @@ function esbuildScanPlugin(
return seen.get(key)
}
container = container || (container = await createPluginContainer(config))
const resolved = await container.resolveId(id, importer)
const resolved = await container.resolveId(
id,
importer && normalizePath(importer)
)
const res = resolved?.id
seen.set(key, res)
return res
Expand All @@ -96,63 +104,6 @@ function esbuildScanPlugin(
return {
name: 'vite:dep-scan',
setup(build) {
// bare imports: record and externalize
build.onResolve(
{
filter: /^[\w@]/
},
async ({ path: id, importer }) => {
if (depImports[id]) {
return {
path: id,
external: true
}
}

if (
isExternalUrl(id) ||
isDataUrl(id) ||
isCSSRequest(id) ||
config.assetsInclude(id)
) {
return {
path: id,
external: true
}
}

const resolved = await resolve(id, importer)
if (resolved) {
// virtual id or browser external
if (id === resolved || resolved.startsWith(browserExternalId)) {
return { path: id, external: true }
}
// 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
}
} else {
// linked package, keep crawling
return {
path: path.resolve(resolved)
}
}
} else {
config.logger.error(
chalk.red(
`Dependency ${id} not found. Is it installed? (imported by ${importer})`
)
)
missing.add(id)
}
}
)

const htmlTypesRe = /\.(html|vue|svelte)$/
// html types: extract script contents
build.onResolve({ filter: htmlTypesRe }, async ({ path, importer }) => {
Expand All @@ -161,6 +112,7 @@ function esbuildScanPlugin(
namespace: 'html'
}
})

build.onLoad(
{ filter: htmlTypesRe, namespace: 'html' },
async ({ path }) => {
Expand Down Expand Up @@ -210,6 +162,63 @@ function esbuildScanPlugin(
({ path }) => ({ path, external: true })
)

// bare imports: record and externalize
build.onResolve(
{
// avoid matching windows volume
filter: /^[\w@][^:]/
},
async ({ path: id, importer }) => {
if (depImports[id]) {
return {
path: id,
external: true
}
}

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

const resolved = await resolve(id, importer)
if (resolved) {
// browser external
if (resolved.startsWith(browserExternalId)) {
return { path: id, external: true }
}
// virtual id
if (id === resolved) {
return { path: id, external: true }
}
// 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
}
} else {
// linked package, keep crawling
return {
path: path.resolve(resolved)
}
}
} else {
config.logger.error(
chalk.red(
`Dependency ${id} not found. Is it installed? (imported by ${importer})`
)
)
missing.add(id)
}
}
)

// catch all
build.onResolve(
{
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/ssr/ssrTransform.ts
Expand Up @@ -94,7 +94,7 @@ export async function ssrTransform(
} else {
// export const foo = 1, bar = 2
for (const decl of node.declaration.declarations) {
const names = extractNames(decl.id)
const names = extractNames(decl.id as any)
for (const name of names) {
defineExport(name)
}
Expand Down

0 comments on commit 5f7698b

Please sign in to comment.