Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack config for multiple entry points / templates

Tags:

webpack

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!

like image 651
malcoauri Avatar asked Mar 09 '16 18:03

malcoauri


People also ask

Can webpack have multiple entry points?

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.

Does webpack automatically minify?

Webpack v4+ will minify your code by default in production mode .

How can we generate webpack config file automatically?

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.

What are 4 core concept of webpack?

There are four basic concepts in webpack: entry , output , modules and plug-ins . These configurations are added in webpack.


Video Answer


1 Answers

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.

like image 196
leojh Avatar answered Oct 28 '22 01:10

leojh