There is the following task - I've got folder with views:
--views
----view1
------view1.js
------view1.html(or jade)
----view2
------view2.js
------view2.html(or jade)
So, I need to create a simple config for webpack, which can create the following output 'public' folder:
--public
----js
------view1.js
------view2.js
----view1.html
----view2.html
I understand, that I can use multiple entry points with it:
entry: {
view1: './views/view1/view1'
view2: './views/view2/view2
}
Also I understand, that I can inject bundle (public/js/view1.js
) in public/view1.html
using HtmlWebpackPlugin. But what about multiple points? Must I add HtmlWebpackPlugin for each html view? Thanks in advance!
The html-webpack-plugin is configured by setting the template property to the HTML template. Then we specify the output name with filename , and finally we associate it with one or more of the entry points with the chunks property. After saving this file, we can ask webpack to generate our bundles and HTML files.
Webpack v4+ will minify your code by default in production mode .
Use webpack-cli's init command to rapidly generate webpack configuration files for your project requirements, it will ask you a couple of questions before creating a configuration file. npx might prompt you to install @webpack-cli/init if it is not yet installed in the project or globally.
There are four basic concepts in webpack: entry , output , modules and plug-ins . These configurations are added in webpack.
You must add multiple HtmlWebpackPlugin
sections for each .html page you wish to create.
Here's a sample of my own config:
plugins: [
new HtmlWebpackPlugin({
title: 'SearchView',
chunks: ['manifest', 'vendor', 'searchView'],
template: `${PATHS.app}/index.ejs`, // Load a custom template
inject: 'body', // Inject all scripts into the body
filename: 'searchView.html'
}),
new HtmlWebpackPlugin({
title: 'TicketView',
chunks: ['manifest', 'vendor', 'ticketView'],
template: `${PATHS.app}/index.ejs`, // Load a custom template
inject: 'body', // Inject all scripts into the body
filename: 'ticketView.html'
})
]
The chunks
property is key because it lets you select only the resources you need for each page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With