5

I'm using Webpack/HtmlWebpackPlugin with multiple entry points & chunks to compile an AngularJS application. I'll need the JS injected somewhere in the middle of the HTML template, so I use

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

as described here: https://github.com/jaketrent/html-webpack-template/blob/master/index.ejs. The template get's compiled, but no script-tags are inserted for my chunks. What's wrong?

My relevant Webpack configuration [webpack.prod.config.js]:

const path = require('path');
const merge = require('webpack-merge');
const webpack = require('webpack');
const common = require('./webpack.base.config.js');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = merge(common, {
  mode: 'production',
  output: {
    filename: '[name].bundle.min.js',
    path: path.resolve(__dirname, 'dist.prod')
    },
    plugins: [
      new HtmlWebpackPlugin({
        template: './src-ts-store/landingpage_store.liquid.ejs',
        filename: 'landingpage_store.liquid',
        base: {href: '/apps/sgp'},
        inject: false,/* instead using this:  https://github.com/jantimon/html-webpack-plugin/issues/386 , https://github.com/jaketrent/html-webpack-template */
        chunks: ['app-store','defaultVendors','dptCommon']
      }),
      new HtmlWebpackPlugin({
        template: './src-ts-admin/landingpage_admin.html.ejs',
        filename: 'landingpage_admin.html',
        base: {href: '/frontend-admin/'},
        inject: false,/* instead using this:  https://github.com/jantimon/html-webpack-plugin/issues/386 , https://github.com/jaketrent/html-webpack-template */
        chunks: ['app-admin','defaultVendors','dptCommon']
      })
    ]
});

The relevant part of the template [landingpage_admin.html.ejs]

<html ng-app="DPT" ng-controller="appController">
 <script src="app.environment.var.js"></script>
 <link href="plugins/bootstrap-dropdown-removed-typography.css" rel="stylesheet" type="text/css" />


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


 <script src="https://kit.fontawesome.com/b6b551a0d0.js" crossorigin="anonymous"></script>
 [...]

The relevant console output

Child HtmlWebpackCompiler:
     2 assets
    Entrypoint HtmlWebpackPlugin_0 = __child-HtmlWebpackPlugin_0
    Entrypoint HtmlWebpackPlugin_1 = __child-HtmlWebpackPlugin_1
    [0] ./node_modules/html-webpack-plugin/lib/loader.js!./src-ts-store/landingpage_store.liquid.ejs 2.26 KiB {0} [built]
    [1] ./node_modules/html-webpack-plugin/lib/loader.js!./src-ts-admin/landingpage_admin.html.ejs 12 KiB {1} [built]

1 Answer 1

7

I have found https://github.com/jaketrent/html-webpack-template/issues/69#issuecomment-376872044 and without truly understanding what is actually going wrong I have changed

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

to the following and it fixed the issue for me:

<% for (var chunk in htmlWebpackPlugin.files.js) { %>
    <script src="<%= htmlWebpackPlugin.files.js[chunk]%>"></script>
<% } %>
Sign up to request clarification or add additional context in comments.

2 Comments

my hero! Why in the world will <%= htmlWebpackPlugin.tags.headTags %> not work
Very helpful! For reference, in version 4.0.0 of html-webpack-plugin, the "chunks" property was removed, which is why chunks are now referenced as the [chunk] property of files.js instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.