Skip to content

Commit

Permalink
fix: more graceful handling for packages with no main field
Browse files Browse the repository at this point in the history
close #247
  • Loading branch information
yyx990803 committed May 25, 2020
1 parent 3486d21 commit 8816d3b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
11 changes: 10 additions & 1 deletion src/node/depOptimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export async function optimizeDeps(
return false
}
const pkgInfo = resolveNodeModule(root, id)
if (!pkgInfo) {
if (!pkgInfo || !pkgInfo.entryFilePath) {
debug(`skipping ${id} (cannot resolve entry)`)
return false
}
Expand All @@ -126,6 +126,15 @@ export async function optimizeDeps(
debug(`skipping ${id} (entry is not js)`)
return false
}
if (!fs.existsSync(entryFilePath)) {
debug(`skipping ${id} (entry file does not exist)`)
console.error(
chalk.yellow(
`[vite] dependency ${id} declares non-existent entry file ${entryFilePath}.`
)
)
return false
}
const content = fs.readFileSync(entryFilePath, 'utf-8')
const [imports, exports] = parse(content)
if (!exports.length && !/export\s+\*\s+from/.test(content)) {
Expand Down
33 changes: 21 additions & 12 deletions src/node/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,22 @@ export function resolveBareModuleRequest(
root: string,
id: string,
importer: string
) {
): string {
const optimized = resolveOptimizedModule(root, id)
if (optimized) {
return id
}
const pkgInfo = resolveNodeModule(root, id)
if (pkgInfo) {
return pkgInfo.entry
if (!pkgInfo.entry) {
console.error(
chalk.yellow(
`[vite] dependency ${id} does not have default entry defined in ` +
`package.json.`
)
)
}
return pkgInfo.entry || id
}

// check and warn deep imports on optimized modules
Expand Down Expand Up @@ -200,8 +208,8 @@ export function resolveOptimizedModule(
}

interface NodeModuleInfo {
entry: string
entryFilePath: string
entry: string | null
entryFilePath: string | null
pkg: any
}
const nodeModulesInfoMap = new Map<string, NodeModuleInfo>()
Expand Down Expand Up @@ -242,22 +250,23 @@ export function resolveNodeModule(
}
}
if (!entryPoint) {
entryPoint = pkg.module || pkg.main || 'index.js'
entryPoint = pkg.module || pkg.main || null
}

debug(`(node_module entry) ${id} -> ${entryPoint}`)

const entryFilePath = path.join(path.dirname(pkgPath), entryPoint!)

// save resolved entry file path using the deep import path as key
// e.g. foo/dist/foo.js
// this is the path raw imports will be rewritten to, and is what will
// be passed to resolveNodeModuleFile().
entryPoint = path.posix.join(id, entryPoint!)

// save the resolved file path now so we don't need to do it again in
// resolveNodeModuleFile()
nodeModulesFileMap.set(entryPoint, entryFilePath)
let entryFilePath: string | null = null
if (entryPoint) {
entryFilePath = path.join(path.dirname(pkgPath), entryPoint!)
entryPoint = path.posix.join(id, entryPoint!)
// save the resolved file path now so we don't need to do it again in
// resolveNodeModuleFile()
nodeModulesFileMap.set(entryPoint, entryFilePath)
}

const result: NodeModuleInfo = {
entry: entryPoint!,
Expand Down

0 comments on commit 8816d3b

Please sign in to comment.