Use dependencies for unpkg URLs in module mode

This commit is contained in:
MICHAEL JACKSON 2017-08-21 10:17:40 -07:00
parent 066729f17c
commit be14167718
4 changed files with 461 additions and 434 deletions

View File

@ -9,6 +9,7 @@
"test": "node scripts/test.js --env=jsdom" "test": "node scripts/test.js --env=jsdom"
}, },
"dependencies": { "dependencies": {
"babel-plugin-unpkg-rewrite": "^2.0.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",

View File

@ -2,9 +2,9 @@ const validateNPMPackageName = require('validate-npm-package-name')
const parsePackageURL = require('../utils/parsePackageURL') const parsePackageURL = require('../utils/parsePackageURL')
const KnownQueryParams = { const KnownQueryParams = {
expand: true,
main: true, main: true,
meta: true meta: true,
module: true
} }
function isKnownQueryParam(param) { function isKnownQueryParam(param) {

View File

@ -17,9 +17,11 @@ const AutoIndex = !process.env.DISABLE_INDEX
const MaximumDepth = 128 const MaximumDepth = 128
const FileTransforms = { const FileTransforms = {
expand: function (file, callback) { expand: function (file, dependencies, callback) {
const options = { const options = {
plugins: [ unpkgRewrite ] plugins: [
unpkgRewrite(dependencies)
]
} }
babel.transformFile(file, options, function (error, result) { babel.transformFile(file, options, function (error, result) {
@ -60,8 +62,13 @@ function serveFile(req, res, next) {
'Cache-Tag': 'file' 'Cache-Tag': 'file'
}) })
if (contentType === 'application/javascript' && req.query.expand != null) { if (contentType === 'application/javascript' && req.query.module != null) {
FileTransforms.expand(file, function (error, code) { const dependencies = Object.assign({},
req.packageConfig.peerDependencies,
req.packageConfig.dependencies
)
FileTransforms.expand(file, dependencies, function (error, code) {
if (error) { if (error) {
console.error(error) console.error(error)
res.status(500).type('text').send(`Cannot generate index page for ${req.packageSpec}${req.filename}`) res.status(500).type('text').send(`Cannot generate index page for ${req.packageSpec}${req.filename}`)

875
yarn.lock

File diff suppressed because it is too large Load Diff