mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-09-19 02:48:30 +08:00
* add sentry webpack plugin * allow override of webpack config * work around cheerio inability to parse jinga templates * webpack can not minify jinja templates * allow script injection to specify other attributes * allow script injection to specify other attributes * Adjustments to make plugin systems work * Add sourcemaps for javascript in prod webpack * Update .gitignore * Fix spelling errors Co-authored-by: Matt Weiden <538456+mweiden@users.noreply.github.com>
62 lines
1.8 KiB
JavaScript
62 lines
1.8 KiB
JavaScript
const cheerio = require("cheerio");
|
|
const crypto = require("crypto");
|
|
HtmlWebpackPlugin = require("html-webpack-plugin");
|
|
|
|
class CspHashPlugin {
|
|
constructor(opts) {
|
|
this.opts = { ...opts };
|
|
}
|
|
|
|
apply(compiler) {
|
|
compiler.hooks.compilation.tap("CspHashPlugin", (compilation) => {
|
|
HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
|
|
"CspHashPlugin",
|
|
(data, cb) => {
|
|
const { filename } = this.opts;
|
|
|
|
const $ = cheerio.load(data.html, { decodeEntities: false });
|
|
|
|
if (filename) {
|
|
const results = {};
|
|
results["script-hashes"] = $("script:not([src]):not([no-csp-hash])")
|
|
.map((i, elmt) => this.digest($(elmt).html()))
|
|
.get();
|
|
results["style-hashes"] = $("style:not([href]):not([no-csp-hash])")
|
|
.map((i, elmt) => this.digest($(elmt).html()))
|
|
.get();
|
|
|
|
const json = JSON.stringify(results);
|
|
compilation.assets[filename] = {
|
|
source: () => json,
|
|
size: () => json.length,
|
|
};
|
|
}
|
|
|
|
// Remove no-csp-hash attributes. Cheerio does not parse Jinja templates
|
|
// correctly, so we brute force this with a regular expression.
|
|
data.html = data.html
|
|
.replace(/(<script .*)no-csp-hash(.*>)/, (match, p1, p2) =>
|
|
[p1, p2].join("")
|
|
)
|
|
.replace(/(<style .*)no-csp-hash(.*>)/, (match, p1, p2) =>
|
|
[p1, p2].join("")
|
|
);
|
|
|
|
// Tell webpack to move on
|
|
cb(null, data);
|
|
}
|
|
);
|
|
});
|
|
}
|
|
|
|
digest(str) {
|
|
const hash = crypto
|
|
.createHash("sha256")
|
|
.update(str, "utf8")
|
|
.digest("base64");
|
|
return "sha256-" + hash;
|
|
}
|
|
}
|
|
|
|
module.exports = CspHashPlugin;
|