More improvements for ?module

This commit is contained in:
MICHAEL JACKSON 2017-08-24 00:33:58 -06:00
parent c5092f4b68
commit 02b0dc91e2
3 changed files with 58 additions and 68 deletions

View File

@ -9,7 +9,7 @@
"test": "node scripts/test.js --env=jsdom"
},
"dependencies": {
"babel-plugin-unpkg-rewrite": "^3.0.0",
"babel-plugin-unpkg-rewrite": "^3.1.0",
"cors": "^2.8.1",
"countries-list": "^1.3.2",
"csso": "^3.1.1",

View File

@ -72,76 +72,61 @@ function fetchFile(req, res, next) {
} else {
req.packageDir = outputDir
if (req.filename) {
// Based on the URL, figure out which file they want.
const useIndex = req.filename[req.filename.length - 1] !== '/'
let filename = req.filename
let useIndex = true
findFile(path.join(req.packageDir, req.filename), useIndex, function (error, file, stats) {
if (error)
console.error(error)
if (file == null) {
res.status(404).type('text').send(`Cannot find file "${req.filename}" in package ${req.packageSpec}`)
} else {
req.file = file.replace(req.packageDir, '')
req.stats = stats
next()
}
})
} else if (req.query.module != null) {
if (req.query.module != null) {
// They want an ES module. Try "module", "jsnext:main", and "/"
// https://github.com/rollup/rollup/wiki/pkg.module
const filename = req.packageConfig.module || req.packageConfig['jsnext:main'] || '/'
if (filename == null)
filename = req.packageConfig.module || req.packageConfig['jsnext:main'] || '/'
} else if (filename) {
// They are requesting an explicit filename. Only try to find an
// index file if they are NOT requesting an HTML directory listing.
useIndex = filename[filename.length - 1] !== '/'
} else if (req.query.main) {
if (!(req.query.main in req.packageConfig))
return res.status(404).type('text').send(`Cannot find field "${req.query.main}" in ${req.packageSpec} package config`)
findFile(path.join(req.packageDir, filename), true, function (error, file, stats) {
if (error)
console.error(error)
if (file == null) {
res.status(404).type('text').send(`Cannot find module "${filename}" in package ${req.packageSpec}`)
} else {
// Need to redirect to the module file so relative imports resolve
// correctly. Cache module redirects for 1 minute.
res.set({
'Cache-Control': 'public, max-age=60',
'Cache-Tag': 'redirect,module-redirect'
}).redirect(302, createPackageURL(req.packageName, req.packageVersion, file.replace(req.packageDir, ''), req.search))
}
})
// They specified a custom ?main field.
filename = req.packageConfig[req.query.main]
} else if (typeof req.packageConfig.unpkg === 'string') {
// The "unpkg" field allows packages to explicitly declare the
// file to serve at the bare URL (see #59).
filename = req.packageConfig.unpkg
} else if (typeof req.packageConfig.browser === 'string') {
// Fall back to the "browser" field if declared (only support strings).
filename = req.packageConfig.browser
} else {
// No filename in the URL. Try to figure out which file they want.
let filename
if (req.query.main) {
if (!(req.query.main in req.packageConfig))
return res.status(404).type('text').send(`Cannot find field "${req.query.main}" in ${req.packageSpec} package config`)
filename = req.packageConfig[req.query.main]
} else if (typeof req.packageConfig.unpkg === 'string') {
// The "unpkg" field allows packages to explicitly declare the
// file to serve at the bare URL (see #59).
filename = req.packageConfig.unpkg
} else if (typeof req.packageConfig.browser === 'string') {
// Fall back to the "browser" field if declared (only support strings).
filename = req.packageConfig.browser
} else {
// Use "main" or fallback to "/" (same as npm).
filename = req.packageConfig.main || '/'
}
findFile(path.join(req.packageDir, filename), true, function (error, file, stats) {
if (error)
console.error(error)
if (file == null) {
res.status(404).type('text').send(`Cannot find main file "${filename}" in package ${req.packageSpec}`)
} else {
req.file = file.replace(req.packageDir, '')
req.stats = stats
next()
}
})
// Fall back to "main" or / (same as npm).
filename = req.packageConfig.main || '/'
}
findFile(path.join(req.packageDir, filename), useIndex, function (error, file, stats) {
if (error)
console.error(error)
if (file == null)
return res.status(404).type('text').send(`Cannot find module "${filename}" in package ${req.packageSpec}`)
filename = file.replace(req.packageDir, '')
// TODO: We are able to serve files w/out the ".js" extension so
// don't bother redirecting when req.filename is "/area" and filename
// is "/area.js"
if (req.query.module != null && req.filename !== filename) {
// Need to redirect to the module file so relative imports resolve
// correctly. Cache module redirects for 1 minute.
res.set({
'Cache-Control': 'public, max-age=60',
'Cache-Tag': 'redirect,module-redirect'
}).redirect(302, createPackageURL(req.packageName, req.packageVersion, filename, req.search))
} else {
req.file = filename
req.stats = stats
next()
}
})
}
})
} else if (req.packageVersion in req.packageInfo['dist-tags']) {

View File

@ -463,6 +463,10 @@ babel-plugin-syntax-exponentiation-operator@^6.8.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz#9ee7e8337290da95288201a6a57f4170317830de"
babel-plugin-syntax-export-extensions@^6.13.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-export-extensions/-/babel-plugin-syntax-export-extensions-6.13.0.tgz#70a1484f0f9089a4e84ad44bac353c95b9b12721"
babel-plugin-syntax-flow@^6.18.0, babel-plugin-syntax-flow@^6.3.13:
version "6.18.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-flow/-/babel-plugin-syntax-flow-6.18.0.tgz#4c3ab20a2af26aa20cd25995c398c4eb70310c8d"
@ -739,10 +743,11 @@ babel-plugin-transform-strict-mode@^6.24.1:
babel-runtime "^6.22.0"
babel-types "^6.24.1"
babel-plugin-unpkg-rewrite@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/babel-plugin-unpkg-rewrite/-/babel-plugin-unpkg-rewrite-3.0.0.tgz#3485621d79da04cd910ca21cdc6b3cacc1a3d1d1"
babel-plugin-unpkg-rewrite@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/babel-plugin-unpkg-rewrite/-/babel-plugin-unpkg-rewrite-3.1.0.tgz#0e60ecfaac74689346636d85c9fea29beea46c38"
dependencies:
babel-plugin-syntax-export-extensions "^6.13.0"
warning "^3.0.0"
whatwg-url "^4.3.0"