I am trying to automate assets going into /dist. I have the following config.js:
module.exports = {
context: __dirname + "/lib",
entry: {
m
You can add the index directly to your entry config and using a file-loader to load it
module.exports = {
entry: [
__dirname + "/index.html",
.. other js files here
],
module: {
rules: [
{
test: /\.html/,
loader: 'file-loader?name=[name].[ext]',
},
.. other loaders
]
}
}
To copy an already existing index.html
file into the dist
directory you can simply use the HtmlWebpackPlugin by specifying the source index.html
as a template.
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
// ...
plugins: [
new HtmlWebpackPlugin({
template: './path/to/index.html',
})
],
// ...
};
The created dist/index.html
file will be basically the same as your source file with the difference that bundled resources like .js files are injected with <script>
tags by webpack. Minification and further options can be configured and are documented on github.
I also found it easy and generic enough to put my index.html
file in dist/
directory and add <script src='main.js'></script>
to index.html
to include my bundled webpack files. main.js
seems to be default output name of our bundle if no other specified in webpack's conf file. I guess it's not good and long-term solution, but I hope it can help to understand how webpack works.