I need to minify 2 JS files and inline one of them into HTML template. I have the following directory structure:
- src
---- page.html
---- script-to-inline.js
---- script.js
- webpack.config.js
And the following webpack config:
const webpack = require('webpack');
const Dotenv = require('dotenv-webpack');
const HtmlWebpackPlugin = require('dotenv-webpack');
const InlineScriptWebpackPlugin = require('dotenv-webpack');
module.exports = {
context: __dirname,
mode: 'production',
entry: {
script: './src/script.js',
'script-to-inline': './src/script-to-inline.js',
},
output: {
path: __dirname,
},
plugins: [
new Dotenv(),
new HtmlWebpackPlugin({
inlineSource: './src/script-to-inline.js',
template: './src/page.html',
filename: 'page.html',
}),
new InlineScriptWebpackPlugin({
names: ['script-to-inline'],
}),
],
};
Build output is the following:
Hash: bb94c50e294f070b4fd1
Version: webpack 4.12.0
Time: 118ms
Built at: 2018-06-20 13:58:48
Asset Size Chunks Chunk Names
tracking-script-injector.js 1.24 KiB 0 [emitted] tracking-script-injector
tracking.js 1.28 KiB 1 [emitted] tracking
[0] ./src/tracking-script-injector.js 971 bytes {0} [built]
[1] ./src/tracking.js 630 bytes {1} [built]
It looks like HtmlWebpackPlugin
doesn't even try to read the template I pass to its options.
What should I change to make plugin process the page.html
file?
It is not working because the require statement is not referencing correctly to the plugin (which is weird too because webpack did'nt yell at you).
const HtmlWebpackPlugin = require('dotenv-webpack');
It should be:
const HtmlWebpackPlugin = require('html-webpack-plugin');