Skip to content

Commit fd5fe58

Browse files
committed
refactor: move the publicPath generation into a seperate function
1 parent 60a6ef8 commit fd5fe58

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

index.js

+31-15
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,11 @@ function hookIntoCompiler (compiler, options, plugin) {
252252
// it is a cached result
253253
const isCompilationCached = templateResult.mainCompilationHash !== compilation.hash;
254254

255-
// Turn the entry point names into file paths
256-
const assets = htmlWebpackPluginAssets(compilation, childCompilationOutputName, sortedEntryNames, options.publicPath);
255+
/** The public path used inside the html file */
256+
const htmlPublicPath = getPublicPath(compilation, childCompilationOutputName, options.publicPath);
257+
258+
/** Generated file paths from the entry point names */
259+
const assets = htmlWebpackPluginAssets(compilation, sortedEntryNames, htmlPublicPath);
257260

258261
// If the template and the assets did not change we don't have to emit the html
259262
const newAssetJson = JSON.stringify(getAssetFiles(assets));
@@ -327,7 +330,6 @@ function hookIntoCompiler (compiler, options, plugin) {
327330
? plugin.evaluateCompilationResult(templateResult.compiledEntry.content, options.template)
328331
: Promise.reject(new Error('Child compilation contained no compiledEntry'));
329332
});
330-
331333
const templateExectutionPromise = Promise.all([assetsPromise, assetTagGroupsPromise, templateEvaluationPromise])
332334
// Execute the template
333335
.then(([assetsHookResult, assetTags, compilationResult]) => typeof compilationResult !== 'function'
@@ -545,20 +547,15 @@ function hookIntoCompiler (compiler, options, plugin) {
545547
}
546548

547549
/**
548-
* The htmlWebpackPluginAssets extracts the asset information of a webpack compilation
549-
* for all given entry names
550+
* Generate the relative or absolute base url to reference images, css, and javascript files
551+
* from within the html file - the publicPath
552+
*
550553
* @param {WebpackCompilation} compilation
551-
* @param {string[]} entryNames
554+
* @param {string} childCompilationOutputName
552555
* @param {string | 'auto'} customPublicPath
553-
* @returns {{
554-
publicPath: string,
555-
js: Array<string>,
556-
css: Array<string>,
557-
manifest?: string,
558-
favicon?: string
559-
}}
556+
* @returns {string}
560557
*/
561-
function htmlWebpackPluginAssets (compilation, childCompilationOutputName, entryNames, customPublicPath) {
558+
function getPublicPath (compilation, childCompilationOutputName, customPublicPath) {
562559
const compilationHash = compilation.hash;
563560

564561
/**
@@ -587,6 +584,25 @@ function hookIntoCompiler (compiler, options, plugin) {
587584
publicPath += '/';
588585
}
589586

587+
return publicPath;
588+
}
589+
590+
/**
591+
* The htmlWebpackPluginAssets extracts the asset information of a webpack compilation
592+
* for all given entry names
593+
* @param {WebpackCompilation} compilation
594+
* @param {string[]} entryNames
595+
* @param {string | 'auto'} publicPath
596+
* @returns {{
597+
publicPath: string,
598+
js: Array<string>,
599+
css: Array<string>,
600+
manifest?: string,
601+
favicon?: string
602+
}}
603+
*/
604+
function htmlWebpackPluginAssets (compilation, entryNames, publicPath) {
605+
const compilationHash = compilation.hash;
590606
/**
591607
* @type {{
592608
publicPath: string,
@@ -598,7 +614,7 @@ function hookIntoCompiler (compiler, options, plugin) {
598614
*/
599615
const assets = {
600616
// The public path
601-
publicPath: publicPath,
617+
publicPath,
602618
// Will contain all js and mjs files
603619
js: [],
604620
// Will contain all css files

0 commit comments

Comments
 (0)