webpack-subresource-integrity
Version:
Webpack plugin for enabling Subresource Integrity
93 lines • 4.12 kB
JavaScript
/**
* Copyright (c) 2015-present, Waysact Pty Ltd
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.install = void 0;
const globals_1 = require("./globals");
const util_1 = require("./util");
function sriStatsFactory(statsFactory) {
statsFactory.hooks.extract
.for("asset")
.tap(globals_1.thisPluginName, (object, asset) => {
var _a;
const contenthash = (_a = asset.info) === null || _a === void 0 ? void 0 : _a.contenthash;
if (contenthash) {
const shaHashes = (Array.isArray(contenthash) ? contenthash : [contenthash]).filter((hash) => String(hash).match(/^sha[0-9]+-/));
if (shaHashes.length > 0) {
object.integrity =
shaHashes.join(" ");
}
}
});
}
function installStatsFactoryPlugin(compiler) {
compiler.hooks.compilation.tap(globals_1.thisPluginName, (compilation) => {
compilation.hooks.statsFactory.tap(globals_1.thisPluginName, sriStatsFactory);
});
}
function isErrorWithCode(obj) {
return (obj instanceof Error &&
(0, util_1.hasOwnProperty)(obj, "code") &&
["string", "undefined"].includes(typeof obj.code));
}
function install(compiler, callback) {
let getHtmlWebpackPluginHooks = null;
compiler.hooks.beforeCompile.tapPromise(globals_1.thisPluginName, () => __awaiter(this, void 0, void 0, function* () {
try {
getHtmlWebpackPluginHooks = (yield Promise.resolve().then(() => __importStar(require("html-webpack-plugin")))).default
.getHooks;
}
catch (e) {
if (!isErrorWithCode(e) || e.code !== "MODULE_NOT_FOUND") {
throw e;
}
}
}));
compiler.hooks.afterPlugins.tap(globals_1.thisPluginName, (compiler) => {
compiler.hooks.thisCompilation.tap({
name: globals_1.thisPluginName,
stage: -10000,
}, (compilation) => {
callback(compilation, (getHtmlWebpackPluginHooks === null || getHtmlWebpackPluginHooks === void 0 ? void 0 : getHtmlWebpackPluginHooks(compilation)) || null);
});
installStatsFactoryPlugin(compiler);
});
}
exports.install = install;
//# sourceMappingURL=hooks.js.map
;