W
W
Wasya UK2019-02-09 19:28:33
JavaScript
Wasya UK, 2019-02-09 19:28:33

How to embed script in html via webpack?

I'm making 2 files, but only 1 needs to be used, so I can't set inject: true . Is it possible to select the files that are needed?

entry: {
    'bundle': './src/js/index.js',
    'bundle.min': './src/js/index.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      inject: false,
      template: path.resolve(__dirname, '../src/html/index.html'),
      files: {
        js: [
          'bundle.min.js'
        ]
      }
    }),

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RibaKit17, 2019-11-22
@dmc1989

HtmlWebpackPlugin has its own templating engine inside. This is how it inserts all the chunks it has.

<% for (var chunk in htmlWebpackPlugin.files.chunks) { %>
<script src="<%= htmlWebpackPlugin.files.chunks[chunk].entry %>"></script>
<% } %>

Here are examples of templates https://github.com/jaketrent/html-webpack-template...
Here is the dock for the plugin https://github.com/jantimon/html-webpack-plugin
May help)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question