Eliminate unnecessary file read
This commit is contained in:
parent
a485858381
commit
f54b56b090
|
@ -64,53 +64,41 @@ function findFile(req, res, next) {
|
||||||
} else {
|
} else {
|
||||||
// No filename in the URL. Try to figure out which file they want by
|
// No filename in the URL. Try to figure out which file they want by
|
||||||
// checking package.json's "unpkg", "browser", and "main" fields.
|
// checking package.json's "unpkg", "browser", and "main" fields.
|
||||||
fs.readFile(path.join(req.packageDir, 'package.json'), 'utf8', function (error, data) {
|
let mainFilename
|
||||||
if (error) {
|
|
||||||
console.error(error)
|
|
||||||
return res.status(500).send(`Cannot read ${req.packageSpec}/package.json`)
|
|
||||||
}
|
|
||||||
|
|
||||||
let packageConfig
|
const packageConfig = req.packageConfig
|
||||||
try {
|
const queryMain = req.query.main
|
||||||
packageConfig = JSON.parse(data)
|
|
||||||
} catch (error) {
|
|
||||||
return res.status(500).send(`Cannot parse ${req.packageSpec}/package.json: ${error.message}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
let mainFilename
|
if (queryMain) {
|
||||||
const queryMain = query && query.main
|
if (!(queryMain in packageConfig))
|
||||||
|
return res.status(404).send(`Cannot find field "${queryMain}" in ${req.packageSpec}/package.json`)
|
||||||
|
|
||||||
if (queryMain) {
|
mainFilename = packageConfig[queryMain]
|
||||||
if (!(queryMain in packageConfig))
|
} else {
|
||||||
return res.status(404).send(`Cannot find field "${queryMain}" in ${req.packageSpec}/package.json`)
|
if (typeof packageConfig.unpkg === 'string') {
|
||||||
|
// The "unpkg" field allows packages to explicitly declare the
|
||||||
mainFilename = packageConfig[queryMain]
|
// file to serve at the bare URL (see #59).
|
||||||
|
mainFilename = packageConfig.unpkg
|
||||||
|
} else if (typeof packageConfig.browser === 'string') {
|
||||||
|
// Fall back to the "browser" field if declared (only support strings).
|
||||||
|
mainFilename = packageConfig.browser
|
||||||
} else {
|
} else {
|
||||||
if (typeof packageConfig.unpkg === 'string') {
|
// If there is no main, use "index" (same as npm).
|
||||||
// The "unpkg" field allows packages to explicitly declare the
|
mainFilename = packageConfig.main || 'index'
|
||||||
// file to serve at the bare URL (see #59).
|
|
||||||
mainFilename = packageConfig.unpkg
|
|
||||||
} else if (typeof packageConfig.browser === 'string') {
|
|
||||||
// Fall back to the "browser" field if declared (only support strings).
|
|
||||||
mainFilename = packageConfig.browser
|
|
||||||
} else {
|
|
||||||
// If there is no main, use "index" (same as npm).
|
|
||||||
mainFilename = packageConfig.main || 'index'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
resolveFile(path.join(req.packageDir, mainFilename), true, function (error, file, stats) {
|
resolveFile(path.join(req.packageDir, mainFilename), true, function (error, file, stats) {
|
||||||
if (error)
|
if (error)
|
||||||
console.error(error)
|
console.error(error)
|
||||||
|
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
res.status(404).send(`Cannot find main file "${mainFilename}" in package ${req.packageSpec}`)
|
res.status(404).send(`Cannot find main file "${mainFilename}" in package ${req.packageSpec}`)
|
||||||
} else {
|
} else {
|
||||||
req.file = file.replace(req.packageDir, '')
|
req.file = file.replace(req.packageDir, '')
|
||||||
req.stats = stats
|
req.stats = stats
|
||||||
next()
|
next()
|
||||||
}
|
}
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue