Loader Options
Option: loaderOptions
This option allow to define special options for build-in template loader.
To handle templates you don't need define a loader. The plugin configures it with default options automatically.
The default build-in template loader will be automatically added in module.rules
:
{
test: /\.(html|eta)$/,
loader: HtmlBundlerPlugin.loader,
}
warningThe plugin works only with the build-in
HtmlBundlerPlugin.loader
. This loader replaces the functionality ofhtml-loader
and many other template loaders such ashandlebars-loader
,nunjucks-loader
,pug-loader
, etc.
For example, following configurations are functionally identical:
- The variant using plugin option aliases to loader options. It's recommended for common use cases:
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
module.exports = {
plugins: [
new HtmlBundlerPlugin({
entry: {
index: 'src/views/index.ejs',
},
preprocessor: 'ejs', // Use EJS template engine
preprocessorOptions: {
views: [path.join(__dirname, 'src/views')],
},
loaderOptions: {
root: path.join(__dirname, 'src'),
}
}),
],
};
- The variant using the
loaderOptions
object as alias tomodule.rules[].options
:
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
module.exports = {
plugins: [
new HtmlBundlerPlugin({
entry: {
index: 'src/views/index.ejs',
},
loaderOptions: {
preprocessor: 'ejs', // Use EJS template engine
preprocessorOptions: {
views: [path.join(__dirname, 'src/views')],
},
root: path.join(__dirname, 'src'),
},
}),
],
};
- The
low level
variant using themodule.rules
:
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
module.exports = {
plugins: [
new HtmlBundlerPlugin({
entry: {
index: 'src/views/index.ejs',
},
}),
],
module: {
rules: [
{
test: /.(html|ejs)$/,
loader: HtmlBundlerPlugin.loader, // Template loader
options: {
preprocessor: 'ejs', // Use EJS template engine
preprocessorOptions: {
views: [path.join(__dirname, 'src/views')],
},
root: path.join(__dirname, 'src'),
},
},
],
},
};
cautionIf you're unsure why you need this, don't define a rule for templates. The plugin configures it automatically.
infoOptions defined in
module.rules
take precedence over the same options defined inloaderOptions
.