Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htmlWebpackPlugin add only one entry point to the generated html

Below are the webpack config entries :

entry:{
    background: './app/main/background.js',
    options: './app/main/options.js'
}

One HTML page which is suplied to htmlwebpackplugin as below

new HtmlWebpackPlugin({
    template :'./app/templates/options.html',
    // chunks : ['jquery','angular','app'],
    filename: "./options.html",
    cache : true
}),

This results in injecting both background.js, options.js in the options.html page as below:

 <script type="text/javascript" src="js/background.js"></script><script type="text/javascript" src="js/options.js"></script></body>

Is there a way to restrict it to only one JS file or specify the file names to be injected in html page?

like image 243
Irshad Avatar asked Jun 12 '18 20:06

Irshad


People also ask

What does HtmlWebpackPlugin do?

The HtmlWebpackPlugin simplifies creation of HTML files to serve your webpack bundles. This is especially useful for webpack bundles that include a hash in the filename which changes every compilation.

What is HtmlWebpackPlugin template?

template: './src/index. html' – this option specifies which file to use as template for the index. html being created. title: 'My Awesome application' – this is a custom property we added to the config object, the value of this property would be used to embed in the placeholder element <%= htmlWebpackPlugin. options.

What is clean webpack plugin?

GitHub - johnagan/clean-webpack-plugin: A webpack plugin to remove your build folder(s) before building. Skip to content Toggle navigation. Product. Actions. Automate any workflow.

What is HTML loader?

The html-loader will parse the URLs, require the images and everything you expect. The extract loader will parse the javascript back into a proper html file, ensuring images are required and point to proper path, and the asset modules will write the .html file for you. Example: webpack.config.js module.


2 Answers

The chunks option is probably what you want to use (see the filtering chunks section of the documentation). I don't know why it's commented in your snippet but you could write this:

entry: {
    background: './app/main/background.js',
    options: './app/main/options.js'
}

If you want to only inject the options entrypoint in the generated HTML, specify the chunks:

new HtmlWebpackPlugin({
    template :'./app/templates/options.html',
    chunks : ['options'],
    filename: "./options.html",
    cache : true
}),
like image 159
fstephany Avatar answered Sep 22 '22 06:09

fstephany


Seems you can use html-webpack-exclude-assets-plugin

plugins: [
  new HtmlWebpackPlugin({
    excludeAssets: [/style.*.js/] // exclude style.js or style.[chunkhash].js 
  }),
  new HtmlWebpackExcludeAssetsPlugin()
]  
like image 34
user2473015 Avatar answered Sep 25 '22 06:09

user2473015