varan
Version:
A webpack starter kit for offline-first bring-your-own-code apps with server side rendering
82 lines • 3.52 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const crypto_1 = require("crypto");
const webpack_assets_manifest_1 = __importDefault(require("webpack-assets-manifest"));
const webpack_sources_1 = require("webpack-sources");
// Helpers
function getSRI(hashes, content) {
return hashes
.map((hash) => {
const integrity = crypto_1.createHash(hash).update(content, 'utf8').digest('base64');
return `${hash}-${integrity}`;
})
.join(' ')
.trim();
}
// Exports
class WebpackVaranAssetsManifest extends webpack_assets_manifest_1.default {
/**
* Overrides
*/
apply(compiler) {
// Add hook for missing assets
compiler.hooks.emit.tapAsync({
context: false,
name: 'WebpackVaranAssetsManifest',
stage: Infinity,
}, this.varanExtendedEmit.bind(this));
return super.apply(compiler);
}
varanExtendedEmit(compilerCompilation, callback) {
const ignoreExtension = ['.br', '.gz', '.license', '.map'];
// Fetch new stats as previous stats is not sufficient
const stats = compilerCompilation.getStats().toJson({
all: false,
assets: true,
cachedAssets: true,
});
// Add missing assets
if (stats.assets) {
stats.assets
.filter((asset) => {
// Ignore self
if (asset.name === this.options.output)
return false;
// Ignore HMR files
if (!/^(?!.*(\.hot-update\.)).*/.test(asset.name))
return false;
// Ignore already processed files
if (this.assetNames.has(asset.name))
return false;
// Ignore based on file extension
return !ignoreExtension.includes(path_1.default.extname(asset.name).toLocaleLowerCase());
})
// Add to manifest
.forEach((asset) => {
this.currentAsset = compilerCompilation.assets[asset.name];
// `integrity` may have already been set by another plugin, like `webpack-subresource-integrity`.
// Only generate the SRI hash if `integrity` is not found.
if (this.options.integrity &&
this.currentAsset &&
this.options.integrityPropertyName &&
!this.currentAsset[this.options.integrityPropertyName]) {
this.currentAsset[this.options.integrityPropertyName] = getSRI(this.options.integrityHashes || [], this.currentAsset.source());
}
this.set(asset.name, asset.name);
this.currentAsset = null;
});
}
const output = this.getManifestPath(compilerCompilation, this.inDevServer()
? path_1.default.basename(this.getOutputPath())
: path_1.default.relative(compilerCompilation.compiler.outputPath, this.getOutputPath()));
// eslint-disable-next-line no-param-reassign
compilerCompilation.assets[output] = new webpack_sources_1.RawSource(this.toString());
callback();
}
}
exports.default = WebpackVaranAssetsManifest;
//# sourceMappingURL=WebpackVaranAssetsManifest.js.map