This commit is contained in:
Iliyan Angelov
2025-09-14 23:24:25 +03:00
commit c67067a2a4
71311 changed files with 6800714 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
import type { Asset } from 'webpack';
/**
* @param {Asset} asset
* @return {string} The MD5 hash of the asset's source.
*
* @private
*/
export declare function getAssetHash(asset: Asset): string | null;

View File

@@ -0,0 +1,32 @@
"use strict";
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAssetHash = void 0;
const crypto_1 = __importDefault(require("crypto"));
/**
* @param {Asset} asset
* @return {string} The MD5 hash of the asset's source.
*
* @private
*/
function getAssetHash(asset) {
// If webpack has the asset marked as immutable, then we don't need to
// use an out-of-band revision for it.
// See https://github.com/webpack/webpack/issues/9038
if (asset.info && asset.info.immutable) {
return null;
}
return crypto_1.default.createHash('md5')
.update(Buffer.from(asset.source.source()))
.digest('hex');
}
exports.getAssetHash = getAssetHash;

View File

@@ -0,0 +1,6 @@
import { Compilation } from 'webpack';
import { WebpackGenerateSWOptions, WebpackInjectManifestOptions, ManifestEntry } from 'workbox-build';
export declare function getManifestEntriesFromCompilation(compilation: Compilation, config: WebpackGenerateSWOptions | WebpackInjectManifestOptions): Promise<{
size: number;
sortedEntries: ManifestEntry[];
}>;

View File

@@ -0,0 +1,188 @@
"use strict";
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getManifestEntriesFromCompilation = void 0;
const webpack_1 = require("webpack");
const transform_manifest_1 = require("workbox-build/build/lib/transform-manifest");
const get_asset_hash_1 = require("./get-asset-hash");
const resolve_webpack_url_1 = require("./resolve-webpack-url");
/**
* For a given asset, checks whether at least one of the conditions matches.
*
* @param {Asset} asset The webpack asset in question. This will be passed
* to any functions that are listed as conditions.
* @param {Compilation} compilation The webpack compilation. This will be passed
* to any functions that are listed as conditions.
* @param {Array<string|RegExp|Function>} conditions
* @return {boolean} Whether or not at least one condition matches.
* @private
*/
function checkConditions(asset, compilation, conditions = []) {
for (const condition of conditions) {
if (typeof condition === 'function') {
return condition({ asset, compilation });
//return compilation !== null;
}
else {
if (webpack_1.ModuleFilenameHelpers.matchPart(asset.name, condition)) {
return true;
}
}
}
// We'll only get here if none of the conditions applied.
return false;
}
/**
* Returns the names of all the assets in all the chunks in a chunk group,
* if provided a chunk group name.
* Otherwise, if provided a chunk name, return all the assets in that chunk.
* Otherwise, if there isn't a chunk group or chunk with that name, return null.
*
* @param {Compilation} compilation
* @param {string} chunkOrGroup
* @return {Array<string>|null}
* @private
*/
function getNamesOfAssetsInChunkOrGroup(compilation, chunkOrGroup) {
const chunkGroup = compilation.namedChunkGroups &&
compilation.namedChunkGroups.get(chunkOrGroup);
if (chunkGroup) {
const assetNames = [];
for (const chunk of chunkGroup.chunks) {
assetNames.push(...getNamesOfAssetsInChunk(chunk));
}
return assetNames;
}
else {
const chunk = compilation.namedChunks && compilation.namedChunks.get(chunkOrGroup);
if (chunk) {
return getNamesOfAssetsInChunk(chunk);
}
}
// If we get here, there's no chunkGroup or chunk with that name.
return null;
}
/**
* Returns the names of all the assets in a chunk.
*
* @param {Chunk} chunk
* @return {Array<string>}
* @private
*/
function getNamesOfAssetsInChunk(chunk) {
const assetNames = [];
assetNames.push(...chunk.files);
// This only appears to be set in webpack v5.
if (chunk.auxiliaryFiles) {
assetNames.push(...chunk.auxiliaryFiles);
}
return assetNames;
}
/**
* Filters the set of assets out, based on the configuration options provided:
* - chunks and excludeChunks, for chunkName-based criteria.
* - include and exclude, for more general criteria.
*
* @param {Compilation} compilation The webpack compilation.
* @param {Object} config The validated configuration, obtained from the plugin.
* @return {Set<Asset>} The assets that should be included in the manifest,
* based on the criteria provided.
* @private
*/
function filterAssets(compilation, config) {
const filteredAssets = new Set();
const assets = compilation.getAssets();
const allowedAssetNames = new Set();
// See https://github.com/GoogleChrome/workbox/issues/1287
if (Array.isArray(config.chunks)) {
for (const name of config.chunks) {
// See https://github.com/GoogleChrome/workbox/issues/2717
const assetsInChunkOrGroup = getNamesOfAssetsInChunkOrGroup(compilation, name);
if (assetsInChunkOrGroup) {
for (const assetName of assetsInChunkOrGroup) {
allowedAssetNames.add(assetName);
}
}
else {
compilation.warnings.push(new Error(`The chunk '${name}' was ` +
`provided in your Workbox chunks config, but was not found in the ` +
`compilation.`));
}
}
}
const deniedAssetNames = new Set();
if (Array.isArray(config.excludeChunks)) {
for (const name of config.excludeChunks) {
// See https://github.com/GoogleChrome/workbox/issues/2717
const assetsInChunkOrGroup = getNamesOfAssetsInChunkOrGroup(compilation, name);
if (assetsInChunkOrGroup) {
for (const assetName of assetsInChunkOrGroup) {
deniedAssetNames.add(assetName);
}
} // Don't warn if the chunk group isn't found.
}
}
for (const asset of assets) {
// chunk based filtering is funky because:
// - Each asset might belong to one or more chunks.
// - If *any* of those chunk names match our config.excludeChunks,
// then we skip that asset.
// - If the config.chunks is defined *and* there's no match
// between at least one of the chunkNames and one entry, then
// we skip that assets as well.
if (deniedAssetNames.has(asset.name)) {
continue;
}
if (Array.isArray(config.chunks) && !allowedAssetNames.has(asset.name)) {
continue;
}
// Next, check asset-level checks via includes/excludes:
const isExcluded = checkConditions(asset, compilation, config.exclude);
if (isExcluded) {
continue;
}
// Treat an empty config.includes as an implicit inclusion.
const isIncluded = !Array.isArray(config.include) ||
checkConditions(asset, compilation, config.include);
if (!isIncluded) {
continue;
}
// If we've gotten this far, then add the asset.
filteredAssets.add(asset);
}
return filteredAssets;
}
async function getManifestEntriesFromCompilation(compilation, config) {
const filteredAssets = filterAssets(compilation, config);
const { publicPath } = compilation.options.output;
const fileDetails = Array.from(filteredAssets).map((asset) => {
return {
file: (0, resolve_webpack_url_1.resolveWebpackURL)(publicPath, asset.name),
hash: (0, get_asset_hash_1.getAssetHash)(asset),
size: asset.source.size() || 0,
};
});
const { manifestEntries, size, warnings } = await (0, transform_manifest_1.transformManifest)({
fileDetails,
additionalManifestEntries: config.additionalManifestEntries,
dontCacheBustURLsMatching: config.dontCacheBustURLsMatching,
manifestTransforms: config.manifestTransforms,
maximumFileSizeToCacheInBytes: config.maximumFileSizeToCacheInBytes,
modifyURLPrefix: config.modifyURLPrefix,
transformParam: compilation,
});
// See https://github.com/GoogleChrome/workbox/issues/2790
for (const warning of warnings) {
compilation.warnings.push(new Error(warning));
}
// Ensure that the entries are properly sorted by URL.
const sortedEntries = manifestEntries.sort((a, b) => a.url === b.url ? 0 : a.url > b.url ? 1 : -1);
return { size, sortedEntries };
}
exports.getManifestEntriesFromCompilation = getManifestEntriesFromCompilation;

View File

@@ -0,0 +1,2 @@
import { Compilation } from 'webpack';
export declare function getScriptFilesForChunks(compilation: Compilation, chunkNames: Array<string>): Array<string>;

View File

@@ -0,0 +1,42 @@
"use strict";
/*
Copyright 2019 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getScriptFilesForChunks = void 0;
const upath_1 = __importDefault(require("upath"));
const resolve_webpack_url_1 = require("./resolve-webpack-url");
function getScriptFilesForChunks(compilation, chunkNames) {
var _a;
const { chunks } = compilation.getStats().toJson({ chunks: true });
const { publicPath } = compilation.options.output;
const scriptFiles = new Set();
for (const chunkName of chunkNames) {
const chunk = chunks.find((chunk) => { var _a; return (_a = chunk.names) === null || _a === void 0 ? void 0 : _a.includes(chunkName); });
if (chunk) {
for (const file of (_a = chunk === null || chunk === void 0 ? void 0 : chunk.files) !== null && _a !== void 0 ? _a : []) {
// See https://github.com/GoogleChrome/workbox/issues/2161
if (upath_1.default.extname(file) === '.js') {
scriptFiles.add((0, resolve_webpack_url_1.resolveWebpackURL)(publicPath, file));
}
}
}
else {
compilation.warnings.push(new Error(`${chunkName} was provided to ` +
`importScriptsViaChunks, but didn't match any named chunks.`));
}
}
if (scriptFiles.size === 0) {
compilation.warnings.push(new Error(`There were no assets matching ` +
`importScriptsViaChunks: [${chunkNames.join(' ')}].`));
}
return Array.from(scriptFiles);
}
exports.getScriptFilesForChunks = getScriptFilesForChunks;

View File

@@ -0,0 +1,20 @@
import type { Compilation } from 'webpack';
/**
* If our bundled swDest file contains a sourcemap, we would invalidate that
* mapping if we just replaced injectionPoint with the stringified manifest.
* Instead, we need to update the swDest contents as well as the sourcemap
* at the same time.
*
* See https://github.com/GoogleChrome/workbox/issues/2235
*
* @param {Object} compilation The current webpack compilation.
* @param {string} swContents The contents of the swSrc file, which may or
* may not include a valid sourcemap comment.
* @param {string} swDest The configured swDest value.
* @return {string|undefined} If the swContents contains a valid sourcemap
* comment pointing to an asset present in the compilation, this will return the
* name of that asset. Otherwise, it will return undefined.
*
* @private
*/
export declare function getSourcemapAssetName(compilation: Compilation, swContents: string, swDest: string): string | undefined;

View File

@@ -0,0 +1,51 @@
"use strict";
/*
Copyright 2019 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSourcemapAssetName = void 0;
const get_source_map_url_1 = require("workbox-build/build/lib/get-source-map-url");
const upath_1 = __importDefault(require("upath"));
/**
* If our bundled swDest file contains a sourcemap, we would invalidate that
* mapping if we just replaced injectionPoint with the stringified manifest.
* Instead, we need to update the swDest contents as well as the sourcemap
* at the same time.
*
* See https://github.com/GoogleChrome/workbox/issues/2235
*
* @param {Object} compilation The current webpack compilation.
* @param {string} swContents The contents of the swSrc file, which may or
* may not include a valid sourcemap comment.
* @param {string} swDest The configured swDest value.
* @return {string|undefined} If the swContents contains a valid sourcemap
* comment pointing to an asset present in the compilation, this will return the
* name of that asset. Otherwise, it will return undefined.
*
* @private
*/
function getSourcemapAssetName(compilation, swContents, swDest) {
const url = (0, get_source_map_url_1.getSourceMapURL)(swContents);
if (url) {
// Translate the relative URL to what the presumed name for the webpack
// asset should be.
// This *might* not be a valid asset if the sourcemap URL that was found
// was added by another module incidentally.
// See https://github.com/GoogleChrome/workbox/issues/2250
const swAssetDirname = upath_1.default.dirname(swDest);
const sourcemapURLAssetName = upath_1.default.normalize(upath_1.default.join(swAssetDirname, url));
// Not sure if there's a better way to check for asset existence?
if (compilation.getAsset(sourcemapURLAssetName)) {
return sourcemapURLAssetName;
}
}
return undefined;
}
exports.getSourcemapAssetName = getSourcemapAssetName;

View File

@@ -0,0 +1,11 @@
import type { Compilation } from 'webpack';
/**
* @param {Object} compilation The webpack compilation.
* @param {string} swDest The original swDest value.
*
* @return {string} If swDest was not absolute, the returns swDest as-is.
* Otherwise, returns swDest relative to the compilation's output path.
*
* @private
*/
export declare function relativeToOutputPath(compilation: Compilation, swDest: string): string;

View File

@@ -0,0 +1,32 @@
"use strict";
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.relativeToOutputPath = void 0;
const upath_1 = __importDefault(require("upath"));
/**
* @param {Object} compilation The webpack compilation.
* @param {string} swDest The original swDest value.
*
* @return {string} If swDest was not absolute, the returns swDest as-is.
* Otherwise, returns swDest relative to the compilation's output path.
*
* @private
*/
function relativeToOutputPath(compilation, swDest) {
// See https://github.com/jantimon/html-webpack-plugin/pull/266/files#diff-168726dbe96b3ce427e7fedce31bb0bcR38
if (upath_1.default.resolve(swDest) === upath_1.default.normalize(swDest)) {
return upath_1.default.relative(compilation.options.output.path, swDest);
}
// Otherwise, return swDest as-is.
return swDest;
}
exports.relativeToOutputPath = relativeToOutputPath;

View File

@@ -0,0 +1,14 @@
/**
* Resolves a url in the way that webpack would (with string concatenation)
*
* Use publicPath + filePath instead of url.resolve(publicPath, filePath) see:
* https://webpack.js.org/configuration/output/#output-publicpath
*
* @function resolveWebpackURL
* @param {string} publicPath The publicPath value from webpack's compilation.
* @param {Array<string>} paths File paths to join
* @return {string} Joined file path
*
* @private
*/
export declare function resolveWebpackURL(publicPath: string, ...paths: Array<string>): string;

View File

@@ -0,0 +1,34 @@
"use strict";
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveWebpackURL = void 0;
/**
* Resolves a url in the way that webpack would (with string concatenation)
*
* Use publicPath + filePath instead of url.resolve(publicPath, filePath) see:
* https://webpack.js.org/configuration/output/#output-publicpath
*
* @function resolveWebpackURL
* @param {string} publicPath The publicPath value from webpack's compilation.
* @param {Array<string>} paths File paths to join
* @return {string} Joined file path
*
* @private
*/
function resolveWebpackURL(publicPath, ...paths) {
// This is a change in webpack v5.
// See https://github.com/jantimon/html-webpack-plugin/pull/1516
if (publicPath === 'auto') {
return paths.join('');
}
else {
return [publicPath, ...paths].join('');
}
}
exports.resolveWebpackURL = resolveWebpackURL;