Skip to content

Commit d654f5b

Browse files
committed
feat: allow to modify the interpolation options in webpack config
1 parent 41d7a50 commit d654f5b

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

lib/loader.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,28 @@ module.exports = function (source) {
88
const options = this.getOptions();
99
const force = options.force || false;
1010

11-
const allLoadersButThisOne = this.loaders.filter(function (loader) {
12-
return loader.normal !== module.exports;
13-
});
11+
const allLoadersButThisOne = this.loaders.filter((loader) => loader.normal !== module.exports);
12+
1413
// This loader shouldn't kick in if there is any other loader (unless it's explicitly enforced)
1514
if (allLoadersButThisOne.length > 0 && !force) {
1615
return source;
1716
}
17+
18+
// Allow only one html-webpack-plugin loader to allow loader options in the webpack config
19+
const htmlWebpackPluginLoaders = this.loaders.filter((loader) => loader.normal === module.exports);
20+
const lastHtmlWebpackPluginLoader = htmlWebpackPluginLoaders[htmlWebpackPluginLoaders.length - 1];
21+
if (this.loaders[this.loaderIndex] !== lastHtmlWebpackPluginLoader) {
22+
return source;
23+
}
24+
1825
// Skip .js files (unless it's explicitly enforced)
1926
if (/\.js$/.test(this.resourcePath) && !force) {
2027
return source;
2128
}
2229

2330
// The following part renders the template with lodash as a minimalistic loader
2431
//
25-
const template = _.template(source, _.defaults(options, { interpolate: /<%=([\s\S]+?)%>/g, variable: 'data' }));
32+
const template = _.template(source, { interpolate: /<%=([\s\S]+?)%>/g, variable: 'data', ...options });
2633
// Use __non_webpack_require__ to enforce using the native nodejs require
2734
// during template execution
2835
return 'var _ = __non_webpack_require__(' + JSON.stringify(require.resolve('lodash')) + ');' +

spec/basic.spec.js

+32
Original file line numberDiff line numberDiff line change
@@ -2750,4 +2750,36 @@ describe('HtmlWebpackPlugin', () => {
27502750
]
27512751
}, ['<body>'], null, done);
27522752
});
2753+
2754+
it('allows to set custom loader interpolation settings', done => {
2755+
testHtmlPlugin({
2756+
mode: 'production',
2757+
entry: {
2758+
app: path.join(__dirname, 'fixtures/index.js')
2759+
},
2760+
output: {
2761+
path: OUTPUT_DIR,
2762+
filename: '[name]_bundle.js'
2763+
},
2764+
module: {
2765+
rules: [
2766+
{
2767+
test: /\.html$/,
2768+
loader: require.resolve('../lib/loader.js'),
2769+
options: {
2770+
interpolate: /\{%=([\s\S]+?)%\}/g
2771+
}
2772+
}
2773+
]
2774+
},
2775+
plugins: [
2776+
new HtmlWebpackPlugin({
2777+
title: 'Interpolation Demo',
2778+
template: path.join(__dirname, 'fixtures/interpolation.html')
2779+
})
2780+
]
2781+
}, ['Interpolation Demo'], null, () => {
2782+
done();
2783+
});
2784+
});
27532785
});

spec/fixtures/legacy.html renamed to spec/fixtures/interpolation.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
<html>
33
<head>
44
<meta charset="utf-8"/>
5-
<title>Test</title>
5+
<title>{%= htmlWebpackPlugin.options.title %}</title>
66
</head>
77
<body>
88
<p>Some unique text</p>
9-
<script src="{%=o.htmlWebpackPlugin.assets.app%}"></script>
109
</body>
1110
</html>

0 commit comments

Comments
 (0)