2018-07-31 17:07:27 +00:00
|
|
|
const etag = require("etag");
|
|
|
|
const babel = require("babel-core");
|
|
|
|
|
|
|
|
const getContentTypeHeader = require("../utils/getContentTypeHeader");
|
|
|
|
const unpkgRewrite = require("../plugins/unpkgRewrite");
|
|
|
|
|
|
|
|
function rewriteBareModuleIdentifiers(code, packageConfig) {
|
|
|
|
const dependencies = Object.assign(
|
|
|
|
{},
|
|
|
|
packageConfig.peerDependencies,
|
|
|
|
packageConfig.dependencies
|
|
|
|
);
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
// Ignore .babelrc and package.json babel config
|
|
|
|
// because we haven't installed dependencies so
|
|
|
|
// we can't load plugins; see #84
|
|
|
|
babelrc: false,
|
|
|
|
plugins: [unpkgRewrite(dependencies)]
|
|
|
|
};
|
|
|
|
|
|
|
|
return babel.transform(code, options).code;
|
|
|
|
}
|
|
|
|
|
|
|
|
function serveJavaScriptModule(req, res) {
|
|
|
|
if (req.entry.contentType !== "application/javascript") {
|
|
|
|
return res
|
|
|
|
.status(403)
|
|
|
|
.type("text")
|
|
|
|
.send("?module mode is available only for JavaScript files");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const code = rewriteBareModuleIdentifiers(
|
|
|
|
req.entry.content.toString("utf8"),
|
|
|
|
req.packageConfig
|
|
|
|
);
|
|
|
|
|
|
|
|
res
|
|
|
|
.set({
|
|
|
|
"Content-Length": Buffer.byteLength(code),
|
|
|
|
"Content-Type": getContentTypeHeader(req.entry.contentType),
|
2018-09-01 18:37:42 +00:00
|
|
|
"Cache-Control": "public, max-age=31536000, immutable", // 1 year
|
2018-07-31 17:07:27 +00:00
|
|
|
ETag: etag(code),
|
|
|
|
"Cache-Tag": "file,js-file,js-module"
|
|
|
|
})
|
|
|
|
.send(code);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
const errorName = error.constructor.name;
|
|
|
|
const errorMessage = error.message.replace(
|
|
|
|
/^.*?\/unpkg-.+?\//,
|
|
|
|
`/${req.packageSpec}/`
|
|
|
|
);
|
|
|
|
const codeFrame = error.codeFrame;
|
|
|
|
const debugInfo = `${errorName}: ${errorMessage}\n\n${codeFrame}`;
|
|
|
|
|
|
|
|
res
|
|
|
|
.status(500)
|
|
|
|
.type("text")
|
|
|
|
.send(
|
|
|
|
`Cannot generate module for ${req.packageSpec}${
|
|
|
|
req.filename
|
|
|
|
}\n\n${debugInfo}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = serveJavaScriptModule;
|