Skip to content

Commit a97234e

Browse files
committed
fix: support loaders like raw-loader
1 parent 6096c8d commit a97234e

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ class HtmlWebpackPlugin {
123123
}
124124
// The LibraryTemplatePlugin stores the template result in a local variable.
125125
// To extract the result during the evaluation this part has to be removed.
126-
source = source.replace('var HTML_WEBPACK_PLUGIN_RESULT =', '');
126+
if (source && source.indexOf('HTML_WEBPACK_PLUGIN_RESULT') >= 0) {
127+
source += ';\nHTML_WEBPACK_PLUGIN_RESULT';
128+
}
127129
const templateWithoutLoaders = templateFilename.replace(/^.+!/, '').replace(/\?.+$/, '');
128130
const vmContext = vm.createContext({ HTML_WEBPACK_PLUGIN: true, require: require, ...global });
129131
const vmScript = new vm.Script(source, { filename: templateWithoutLoaders });

lib/child-compiler.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class HtmlWebpackChildCompiler {
7575
const webpack = mainCompilation.compiler.webpack;
7676
const Compilation = webpack.Compilation;
7777

78-
const NodeTemplatePlugin = webpack.node.NodeTemplatePlugin;
7978
const NodeTargetPlugin = webpack.node.NodeTargetPlugin;
8079
const LoaderTargetPlugin = webpack.LoaderTargetPlugin;
8180
const EntryPlugin = webpack.EntryPlugin;
@@ -95,18 +94,17 @@ class HtmlWebpackChildCompiler {
9594
name: 'HTML_WEBPACK_PLUGIN_RESULT'
9695
},
9796
/** @type {'text/javascript'} */
98-
scriptType: (/** @type {'text/javascript'} */'text/javascript'),
99-
iife: false
97+
scriptType: (/** @type {'text/javascript'} */'text/javascript')
10098
};
10199
const compilerName = 'HtmlWebpackCompiler';
102100
// Create an additional child compiler which takes the template
103101
// and turns it into an Node.JS html factory.
104102
// This allows us to use loaders during the compilation
105103
const childCompiler = mainCompilation.createChildCompiler(compilerName, outputOptions, [
106104
// Compile the template to nodejs javascript
107-
new NodeTemplatePlugin(outputOptions),
108105
new NodeTargetPlugin(),
109-
new LoaderTargetPlugin('node')
106+
new LoaderTargetPlugin('node'),
107+
new webpack.library.EnableLibraryPlugin('var')
110108
]);
111109
// The file path context which webpack uses to resolve all relative files to
112110
childCompiler.context = mainCompilation.compiler.context;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@
4141
"mini-css-extract-plugin": "1.0.0",
4242
"pug": "2.0.3",
4343
"pug-loader": "2.4.0",
44+
"raw-loader": "4.0.2",
4445
"rimraf": "2.6.3",
4546
"semistandard": "^13.0.1",
4647
"standard-version": "9.0.0",
4748
"style-loader": "0.23.1",
4849
"typescript": "4.0.5",
49-
"webpack": "5.4.0",
50+
"webpack": "5.10.0",
5051
"webpack-recompilation-simulator": "3.2.0"
5152
},
5253
"dependencies": {

spec/basic.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,26 @@ describe('HtmlWebpackPlugin', () => {
519519
}, ['<link href="styles.css" rel="stylesheet">'], null, done);
520520
});
521521

522+
it('works with a javascript returning loader like raw-loader', done => {
523+
testHtmlPlugin({
524+
mode: 'production',
525+
entry: path.join(__dirname, 'fixtures/index.js'),
526+
module: {
527+
rules: [
528+
{ test: /\.html$/, use: ['raw-loader'] }
529+
]
530+
},
531+
output: {
532+
path: OUTPUT_DIR,
533+
filename: '[name].js'
534+
},
535+
plugins: [new HtmlWebpackPlugin({
536+
inject: true,
537+
template: path.join(__dirname, 'fixtures/plain.html')
538+
})]
539+
}, ['<script defer="defer" src="main.js"', '<title>Example Plain file</title>'], null, done);
540+
});
541+
522542
it('should work with the css extract plugin on windows and protocol relative urls support (#205)', done => {
523543
testHtmlPlugin({
524544
mode: 'production',

0 commit comments

Comments
 (0)