Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process named chunks instead of entry points #1774

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ function hookIntoCompiler (compiler, options, plugin) {
* @param {(err?: Error) => void} callback
*/
(compilationAssets, callback) => {
// Get all entry point names for this html file
const entryNames = Array.from(compilation.entrypoints.keys());
const filteredEntryNames = filterChunks(entryNames, options.chunks, options.excludeChunks);
const sortedEntryNames = sortEntryChunks(filteredEntryNames, options.chunksSortMode, compilation);
// Get all chunk names for this html file
const chunkNames = Array.from(compilation.namedChunks.keys());
const filteredChunkNames = filterChunks(chunkNames, options.chunks, options.excludeChunks);
const sortedChunkNames = sortChunks(filteredChunkNames, options.chunksSortMode, compilation);

const templateResult = options.templateContent
? { mainCompilationHash: compilation.hash }
Expand All @@ -248,7 +248,7 @@ function hookIntoCompiler (compiler, options, plugin) {
const htmlPublicPath = getPublicPath(compilation, options.filename, options.publicPath);

/** Generated file paths from the entry point names */
const assets = htmlWebpackPluginAssets(compilation, sortedEntryNames, htmlPublicPath);
const assets = htmlWebpackPluginAssets(compilation, sortedChunkNames, htmlPublicPath);

// If the template and the assets did not change we don't have to emit the html
const newAssetJson = JSON.stringify(getAssetFiles(assets));
Expand Down Expand Up @@ -534,18 +534,18 @@ function hookIntoCompiler (compiler, options, plugin) {

/**
* Helper to sort chunks
* @param {string[]} entryNames
* @param {string|((entryNameA: string, entryNameB: string) => number)} sortMode
* @param {string[]} chunkNames
* @param {string|((chunkNameA: string, chunkNameB: string) => number)} sortMode
* @param {WebpackCompilation} compilation
*/
function sortEntryChunks (entryNames, sortMode, compilation) {
function sortChunks (chunkNames, sortMode, compilation) {
// Custom function
if (typeof sortMode === 'function') {
return entryNames.sort(sortMode);
return chunkNames.sort(sortMode);
}
// Check if the given sort mode is a valid chunkSorter sort mode
if (typeof chunkSorter[sortMode] !== 'undefined') {
return chunkSorter[sortMode](entryNames, compilation, options);
return chunkSorter[sortMode](chunkNames, compilation, options);
}
throw new Error('"' + sortMode + '" is not a valid chunk sort mode');
}
Expand Down Expand Up @@ -616,7 +616,7 @@ function hookIntoCompiler (compiler, options, plugin) {
* The htmlWebpackPluginAssets extracts the asset information of a webpack compilation
* for all given entry names
* @param {WebpackCompilation} compilation
* @param {string[]} entryNames
* @param {string[]} chunkNames
* @param {string | 'auto'} publicPath
* @returns {{
publicPath: string,
Expand All @@ -626,7 +626,7 @@ function hookIntoCompiler (compiler, options, plugin) {
favicon?: string
}}
*/
function htmlWebpackPluginAssets (compilation, entryNames, publicPath) {
function htmlWebpackPluginAssets (compilation, chunkNames, publicPath) {
const compilationHash = compilation.hash;
/**
* @type {{
Expand Down Expand Up @@ -656,14 +656,14 @@ function hookIntoCompiler (compiler, options, plugin) {
}

// Extract paths to .js, .mjs and .css files from the current compilation
const entryPointPublicPathMap = {};
const chunkPublicPathMap = {};
const extensionRegexp = /\.(css|js|mjs)(\?|$)/;
for (let i = 0; i < entryNames.length; i++) {
const entryName = entryNames[i];
/** entryPointUnfilteredFiles - also includes hot module update files */
const entryPointUnfilteredFiles = compilation.entrypoints.get(entryName).getFiles();
for (let i = 0; i < chunkNames.length; i++) {
const chunkName = chunkNames[i];
/** chunkUnfilteredFiles - also includes hot module update files */
const chunkUnfilteredFiles = compilation.namedChunks.get(chunkName).files;

const entryPointFiles = entryPointUnfilteredFiles.filter((chunkFile) => {
const chunkFiles = chunkUnfilteredFiles.filter((chunkFile) => {
// compilation.getAsset was introduced in webpack 4.4.0
// once the support pre webpack 4.4.0 is dropped please
// remove the following guard:
Expand All @@ -679,29 +679,29 @@ function hookIntoCompiler (compiler, options, plugin) {
// Prepend the publicPath and append the hash depending on the
// webpack.output.publicPath and hashOptions
// E.g. bundle.js -> /bundle.js?hash
const entryPointPublicPaths = entryPointFiles
const chunkPublishPaths = chunkFiles
.map(chunkFile => {
const entryPointPublicPath = publicPath + urlencodePath(chunkFile);
const chunkPublicPath = publicPath + urlencodePath(chunkFile);
return options.hash
? appendHash(entryPointPublicPath, compilationHash)
: entryPointPublicPath;
? appendHash(chunkPublicPath, compilationHash)
: chunkPublicPath;
});

entryPointPublicPaths.forEach((entryPointPublicPath) => {
const extMatch = extensionRegexp.exec(entryPointPublicPath);
chunkPublishPaths.forEach((chunkPublicPath) => {
const extMatch = extensionRegexp.exec(chunkPublicPath);
// Skip if the public path is not a .css, .mjs or .js file
if (!extMatch) {
return;
}
// Skip if this file is already known
// (e.g. because of common chunk optimizations)
if (entryPointPublicPathMap[entryPointPublicPath]) {
if (chunkPublicPathMap[chunkPublicPath]) {
return;
}
entryPointPublicPathMap[entryPointPublicPath] = true;
chunkPublicPathMap[chunkPublicPath] = true;
// ext will contain .js or .css, because .mjs recognizes as .js
const ext = extMatch[1] === 'mjs' ? 'js' : extMatch[1];
assets[ext].push(entryPointPublicPath);
assets[ext].push(chunkPublicPath);
});
}
return assets;
Expand Down
12 changes: 6 additions & 6 deletions lib/chunksorter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict';

/**
* @type {{[sortmode: string] : (entryPointNames: Array<string>, compilation, htmlWebpackPluginOptions) => Array<string> }}
* @type {{[sortmode: string] : (chunkNames: Array<string>, compilation, htmlWebpackPluginOptions) => Array<string> }}
* This file contains different sort methods for the entry chunks names
*/
module.exports = {};
Expand All @@ -17,20 +17,20 @@ module.exports.none = chunks => chunks;

/**
* Sort manually by the chunks
* @param {string[]} entryPointNames the chunks to sort
* @param {string[]} chunkNames the chunks to sort
* @param {WebpackCompilation} compilation the webpack compilation
* @param htmlWebpackPluginOptions the plugin options
* @return {string[]} The sorted chunks
*/
module.exports.manual = (entryPointNames, compilation, htmlWebpackPluginOptions) => {
module.exports.manual = (chunkNames, compilation, htmlWebpackPluginOptions) => {
const chunks = htmlWebpackPluginOptions.chunks;
if (!Array.isArray(chunks)) {
return entryPointNames;
return chunkNames;
}
// Remove none existing entries from
// htmlWebpackPluginOptions.chunks
return chunks.filter((entryPointName) => {
return compilation.entrypoints.has(entryPointName);
return chunks.filter((chunkName) => {
return compilation.namedChunks.has(chunkName);
});
};

Expand Down