More improvements for ?module
This commit is contained in:
parent
c5092f4b68
commit
02b0dc91e2
|
@ -9,7 +9,7 @@
|
||||||
"test": "node scripts/test.js --env=jsdom"
|
"test": "node scripts/test.js --env=jsdom"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"babel-plugin-unpkg-rewrite": "^3.0.0",
|
"babel-plugin-unpkg-rewrite": "^3.1.0",
|
||||||
"cors": "^2.8.1",
|
"cors": "^2.8.1",
|
||||||
"countries-list": "^1.3.2",
|
"countries-list": "^1.3.2",
|
||||||
"csso": "^3.1.1",
|
"csso": "^3.1.1",
|
||||||
|
|
|
@ -72,50 +72,23 @@ function fetchFile(req, res, next) {
|
||||||
} else {
|
} else {
|
||||||
req.packageDir = outputDir
|
req.packageDir = outputDir
|
||||||
|
|
||||||
if (req.filename) {
|
let filename = req.filename
|
||||||
// Based on the URL, figure out which file they want.
|
let useIndex = true
|
||||||
const useIndex = req.filename[req.filename.length - 1] !== '/'
|
|
||||||
|
|
||||||
findFile(path.join(req.packageDir, req.filename), useIndex, function (error, file, stats) {
|
if (req.query.module != null) {
|
||||||
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) {
|
|
||||||
// They want an ES module. Try "module", "jsnext:main", and "/"
|
// They want an ES module. Try "module", "jsnext:main", and "/"
|
||||||
// https://github.com/rollup/rollup/wiki/pkg.module
|
// 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'] || '/'
|
||||||
findFile(path.join(req.packageDir, filename), true, function (error, file, stats) {
|
} else if (filename) {
|
||||||
if (error)
|
// They are requesting an explicit filename. Only try to find an
|
||||||
console.error(error)
|
// index file if they are NOT requesting an HTML directory listing.
|
||||||
|
useIndex = filename[filename.length - 1] !== '/'
|
||||||
if (file == null) {
|
} else if (req.query.main) {
|
||||||
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))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} 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))
|
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`)
|
return res.status(404).type('text').send(`Cannot find field "${req.query.main}" in ${req.packageSpec} package config`)
|
||||||
|
|
||||||
|
// They specified a custom ?main field.
|
||||||
filename = req.packageConfig[req.query.main]
|
filename = req.packageConfig[req.query.main]
|
||||||
} else if (typeof req.packageConfig.unpkg === 'string') {
|
} else if (typeof req.packageConfig.unpkg === 'string') {
|
||||||
// The "unpkg" field allows packages to explicitly declare the
|
// The "unpkg" field allows packages to explicitly declare the
|
||||||
|
@ -125,24 +98,36 @@ function fetchFile(req, res, next) {
|
||||||
// Fall back to the "browser" field if declared (only support strings).
|
// Fall back to the "browser" field if declared (only support strings).
|
||||||
filename = req.packageConfig.browser
|
filename = req.packageConfig.browser
|
||||||
} else {
|
} else {
|
||||||
// Use "main" or fallback to "/" (same as npm).
|
// Fall back to "main" or / (same as npm).
|
||||||
filename = req.packageConfig.main || '/'
|
filename = req.packageConfig.main || '/'
|
||||||
}
|
}
|
||||||
|
|
||||||
findFile(path.join(req.packageDir, filename), true, function (error, file, stats) {
|
findFile(path.join(req.packageDir, filename), useIndex, function (error, file, stats) {
|
||||||
if (error)
|
if (error)
|
||||||
console.error(error)
|
console.error(error)
|
||||||
|
|
||||||
if (file == null) {
|
if (file == null)
|
||||||
res.status(404).type('text').send(`Cannot find main file "${filename}" in package ${req.packageSpec}`)
|
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 {
|
} else {
|
||||||
req.file = file.replace(req.packageDir, '')
|
req.file = filename
|
||||||
req.stats = stats
|
req.stats = stats
|
||||||
next()
|
next()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
})
|
})
|
||||||
} else if (req.packageVersion in req.packageInfo['dist-tags']) {
|
} else if (req.packageVersion in req.packageInfo['dist-tags']) {
|
||||||
// Cache tag redirects for 1 minute.
|
// Cache tag redirects for 1 minute.
|
||||||
|
|
11
yarn.lock
11
yarn.lock
|
@ -463,6 +463,10 @@ babel-plugin-syntax-exponentiation-operator@^6.8.0:
|
||||||
version "6.13.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"
|
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:
|
babel-plugin-syntax-flow@^6.18.0, babel-plugin-syntax-flow@^6.3.13:
|
||||||
version "6.18.0"
|
version "6.18.0"
|
||||||
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-flow/-/babel-plugin-syntax-flow-6.18.0.tgz#4c3ab20a2af26aa20cd25995c398c4eb70310c8d"
|
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-runtime "^6.22.0"
|
||||||
babel-types "^6.24.1"
|
babel-types "^6.24.1"
|
||||||
|
|
||||||
babel-plugin-unpkg-rewrite@^3.0.0:
|
babel-plugin-unpkg-rewrite@^3.1.0:
|
||||||
version "3.0.0"
|
version "3.1.0"
|
||||||
resolved "https://registry.yarnpkg.com/babel-plugin-unpkg-rewrite/-/babel-plugin-unpkg-rewrite-3.0.0.tgz#3485621d79da04cd910ca21cdc6b3cacc1a3d1d1"
|
resolved "https://registry.yarnpkg.com/babel-plugin-unpkg-rewrite/-/babel-plugin-unpkg-rewrite-3.1.0.tgz#0e60ecfaac74689346636d85c9fea29beea46c38"
|
||||||
dependencies:
|
dependencies:
|
||||||
|
babel-plugin-syntax-export-extensions "^6.13.0"
|
||||||
warning "^3.0.0"
|
warning "^3.0.0"
|
||||||
whatwg-url "^4.3.0"
|
whatwg-url "^4.3.0"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue