Skip to content

Commit 0cd156c

Browse files
committed
feat: add full support for public paths inside templates
1 parent 2c0123d commit 0cd156c

File tree

5 files changed

+63
-6
lines changed

5 files changed

+63
-6
lines changed

Diff for: index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class HtmlWebpackPlugin {
112112
* @param {string} templateFilename
113113
* @returns {Promise<string | (() => string | Promise<string>)>}
114114
*/
115-
evaluateCompilationResult (source, templateFilename) {
115+
evaluateCompilationResult (source, publicPath, templateFilename) {
116116
if (!source) {
117117
return Promise.reject(new Error('The child compilation didn\'t provide a result'));
118118
}
@@ -122,7 +122,7 @@ class HtmlWebpackPlugin {
122122
source += ';\nHTML_WEBPACK_PLUGIN_RESULT';
123123
}
124124
const templateWithoutLoaders = templateFilename.replace(/^.+!/, '').replace(/\?.+$/, '');
125-
const vmContext = vm.createContext({ HTML_WEBPACK_PLUGIN: true, require: require, ...global });
125+
const vmContext = vm.createContext({ HTML_WEBPACK_PLUGIN: true, require: require, htmlWebpackPluginPublicPath: publicPath, ...global });
126126
const vmScript = new vm.Script(source, { filename: templateWithoutLoaders });
127127
// Evaluate code and cast to string
128128
let newSource;
@@ -322,7 +322,7 @@ function hookIntoCompiler (compiler, options, plugin) {
322322
// Once everything is compiled evaluate the html factory
323323
// and replace it with its content
324324
return ('compiledEntry' in templateResult)
325-
? plugin.evaluateCompilationResult(templateResult.compiledEntry.content, options.template)
325+
? plugin.evaluateCompilationResult(templateResult.compiledEntry.content, htmlPublicPath, options.template)
326326
: Promise.reject(new Error('Child compilation contained no compiledEntry'));
327327
});
328328
const templateExectutionPromise = Promise.all([assetsPromise, assetTagGroupsPromise, templateEvaluationPromise])

Diff for: lib/child-compiler.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ class HtmlWebpackChildCompiler {
8888

8989
const outputOptions = {
9090
filename: '__child-[name]',
91-
publicPath: mainCompilation.outputOptions.publicPath === 'auto'
92-
? ''
93-
: mainCompilation.outputOptions.publicPath,
91+
publicPath: '',
9492
library: {
9593
type: 'var',
9694
name: 'HTML_WEBPACK_PLUGIN_RESULT'
@@ -116,6 +114,7 @@ class HtmlWebpackChildCompiler {
116114

117115
// Add all templates
118116
this.templates.forEach((template, index) => {
117+
new EntryPlugin(childCompiler.context, 'data:text/javascript,__webpack_public_path__ = htmlWebpackPluginPublicPath;', `HtmlWebpackPlugin_${index}-${this.id}`).apply(childCompiler);
119118
new EntryPlugin(childCompiler.context, template, `HtmlWebpackPlugin_${index}-${this.id}`).apply(childCompiler);
120119
});
121120

Diff for: spec/basic.spec.js

+47
Original file line numberDiff line numberDiff line change
@@ -2559,4 +2559,51 @@ describe('HtmlWebpackPlugin', () => {
25592559
]
25602560
}, ['<script defer="defer" src="index_bundle.js"></script>'], null, done);
25612561
});
2562+
2563+
it('generates relative path for asset/resource', done => {
2564+
testHtmlPlugin({
2565+
mode: 'development',
2566+
entry: path.join(__dirname, 'fixtures/index.js'),
2567+
output: {
2568+
path: OUTPUT_DIR,
2569+
filename: 'index_bundle.js',
2570+
assetModuleFilename: 'assets/demo[ext]'
2571+
},
2572+
module: {
2573+
rules: [
2574+
{ test: /\.png$/, type: 'asset/resource' }
2575+
]
2576+
},
2577+
plugins: [
2578+
new HtmlWebpackPlugin({
2579+
template: 'html-loader!' + path.join(__dirname, 'fixtures/logo.html'),
2580+
filename: 'demo/index.js'
2581+
})
2582+
]
2583+
}, ['<img src="../assets/demo.png'], 'demo/index.js', done);
2584+
});
2585+
2586+
it('uses the absolute path for asset/resource', done => {
2587+
testHtmlPlugin({
2588+
mode: 'development',
2589+
entry: path.join(__dirname, 'fixtures/index.js'),
2590+
output: {
2591+
path: OUTPUT_DIR,
2592+
filename: 'index_bundle.js',
2593+
assetModuleFilename: 'assets/demo[ext]'
2594+
},
2595+
module: {
2596+
rules: [
2597+
{ test: /\.png$/, type: 'asset/resource' }
2598+
]
2599+
},
2600+
plugins: [
2601+
new HtmlWebpackPlugin({
2602+
template: 'html-loader!' + path.join(__dirname, 'fixtures/logo.html'),
2603+
filename: 'demo/index.js',
2604+
publicPath: '/foo/'
2605+
})
2606+
]
2607+
}, ['<img src="/foo/assets/demo.png'], 'demo/index.js', done);
2608+
});
25622609
});

Diff for: spec/fixtures/logo.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!doctype html>
2+
<html lang="en" manifest="foo.appcache">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Example Plain file</title>
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
</head>
8+
<body>
9+
<img src="./logo.png" />
10+
</body>
11+
</html>

Diff for: spec/fixtures/logo.png

52.8 KB
Loading

0 commit comments

Comments
 (0)