diff --git a/index.js b/index.js index a6c06bf0..aae8f6f6 100644 --- a/index.js +++ b/index.js @@ -117,7 +117,7 @@ class HtmlWebpackPlugin { * @param {string} templateFilename * @returns {Promise string | Promise)>} */ - evaluateCompilationResult (source, templateFilename) { + evaluateCompilationResult (source, publicPath, templateFilename) { if (!source) { return Promise.reject(new Error('The child compilation didn\'t provide a result')); } @@ -127,7 +127,7 @@ class HtmlWebpackPlugin { source += ';\nHTML_WEBPACK_PLUGIN_RESULT'; } const templateWithoutLoaders = templateFilename.replace(/^.+!/, '').replace(/\?.+$/, ''); - const vmContext = vm.createContext({ HTML_WEBPACK_PLUGIN: true, require: require, ...global }); + const vmContext = vm.createContext({ HTML_WEBPACK_PLUGIN: true, require: require, htmlWebpackPluginPublicPath: publicPath, ...global }); const vmScript = new vm.Script(source, { filename: templateWithoutLoaders }); // Evaluate code and cast to string let newSource; @@ -327,7 +327,7 @@ function hookIntoCompiler (compiler, options, plugin) { // Once everything is compiled evaluate the html factory // and replace it with its content return ('compiledEntry' in templateResult) - ? plugin.evaluateCompilationResult(templateResult.compiledEntry.content, options.template) + ? plugin.evaluateCompilationResult(templateResult.compiledEntry.content, htmlPublicPath, options.template) : Promise.reject(new Error('Child compilation contained no compiledEntry')); }); const templateExectutionPromise = Promise.all([assetsPromise, assetTagGroupsPromise, templateEvaluationPromise]) diff --git a/lib/child-compiler.js b/lib/child-compiler.js index 43ef989d..02d21de6 100644 --- a/lib/child-compiler.js +++ b/lib/child-compiler.js @@ -88,9 +88,7 @@ class HtmlWebpackChildCompiler { const outputOptions = { filename: '__child-[name]', - publicPath: mainCompilation.outputOptions.publicPath === 'auto' - ? '' - : mainCompilation.outputOptions.publicPath, + publicPath: '', library: { type: 'var', name: 'HTML_WEBPACK_PLUGIN_RESULT' @@ -116,6 +114,7 @@ class HtmlWebpackChildCompiler { // Add all templates this.templates.forEach((template, index) => { + new EntryPlugin(childCompiler.context, 'data:text/javascript,__webpack_public_path__ = htmlWebpackPluginPublicPath;', `HtmlWebpackPlugin_${index}-${this.id}`).apply(childCompiler); new EntryPlugin(childCompiler.context, template, `HtmlWebpackPlugin_${index}-${this.id}`).apply(childCompiler); }); diff --git a/spec/basic.spec.js b/spec/basic.spec.js index d31ba5e2..eeae2fe0 100644 --- a/spec/basic.spec.js +++ b/spec/basic.spec.js @@ -2584,4 +2584,51 @@ describe('HtmlWebpackPlugin', () => { ] }, [''], null, done); }); + + it('generates relative path for asset/resource', done => { + testHtmlPlugin({ + mode: 'development', + entry: path.join(__dirname, 'fixtures/index.js'), + output: { + path: OUTPUT_DIR, + filename: 'index_bundle.js', + assetModuleFilename: 'assets/demo[ext]' + }, + module: { + rules: [ + { test: /\.png$/, type: 'asset/resource' } + ] + }, + plugins: [ + new HtmlWebpackPlugin({ + template: 'html-loader!' + path.join(__dirname, 'fixtures/logo.html'), + filename: 'demo/index.js' + }) + ] + }, [' + + + + Example Plain file + + + + + + diff --git a/spec/fixtures/logo.png b/spec/fixtures/logo.png new file mode 100644 index 00000000..d71b3d78 Binary files /dev/null and b/spec/fixtures/logo.png differ