Troubleshooting: Difficulty Injecting Chunks into Custom Template Using Webpack and HtmlWebpackPlugin

Utilizing Webpack/HtmlWebpackPlugin for compiling an AngularJS application with multiple entry points and chunks. The JavaScript needs to be injected into the HTML template at a specific location using:

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

Followed instructions from here: https://github.com/jaketrent/html-webpack-template/blob/master/index.ejs. However, after compilation, the script-tags for the chunks are not being inserted as expected. What could be the issue?

Relevant segment of my 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,
        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,
        chunks: ['app-admin','defaultVendors','dptCommon']
      })
    ]
});

The relevant portion 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>
 [...]

Console output snippet:

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]

Answer №1

After stumbling upon this link, I made some changes without fully grasping the issue at hand.

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

I decided to modify it as follows, and surprisingly, it resolved the problem:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to adjust the content width within an iframe without altering the iframe's overall width

I need help with adjusting the width slider in my iframe. I want the width slider to control the content width within the iframe, not the actual width of the iframe itself. Essentially, I want to switch between mobile and desktop mode by resizing the con ...

Creating cookies in R Shiny: Storing variables for future use

Writing a fixed string to cookies can be done easily using methods like Cookies.set(\'cookie_2\', \'value\', { expires: 7 }) (see tutorial here). However, saving the variable user to cookie_2 may require a different ...

Why is the import directive necessary in JavaScript?

As per the information found here, it was believed that the import statement should work in any browser. However, this does not seem to be the case as I encountered a syntax error related to a wrong token when trying to use it in Chrome. The context of my ...

Strategies for effectively binding values in dynamically added and removed fields

I am facing an issue with making binding work on dynamic input fields that can be added and removed. Despite trying various methods, I still can't get it to function as desired. Every time I try to bind, only one data row appears in the dynamic input ...

Transform a jQuery element into an HTML element

I'm facing a dilemma where I have a jQuery element that needs to be passed into a function that only works with HTML elements. How can I efficiently convert the jQuery element into an HTML element? ...

Unable to reach socket.io.js on Raspberry Pi using Lighttpd [Node.JS & Socket.IO]

I just started learning about Node.JS and Socket.IO yesterday. I've been attempting to set up Node.JS and Socket.IO on my Raspberry Pi, but I can't seem to get it working. I'm unable to access <myip>:1337/socket.io/socket.io.js. I fol ...

Distribute or scale a specific value to an array of numbers

I'm currently working on a project that involves distributing the values of an array in a way that the total sum equals 100. Here are some examples: [70, 34, 92] with a total value of 100. The output is [35.71, 17.35, 46.94] because 35.71 + 17.35 + ...

The import component path in Angular 4/TypeScript is not being recognized, even though it appears to be valid and functional

I'm currently working on building a router component using Angular and TypeScript. Check out my project structure below: https://i.stack.imgur.com/h2Y9k.png Let's delve into the landingPageComponent. From the image, you can see that the path ...

Modifying an image with jQuery

Why won't this image change? Below is the relevant HTML, CSS, and jQuery code. HTML <nav id="desktop-nav"> <div class="logo"> <a href="#"></a> </div> <div> ... </div> ... CSS nav#desktop-nav . ...

Data retrieval is currently not functioning, as React is not displaying any error messages

One of the components in my app is as follows: import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { compose } from 'redux'; import { translate ...

Using AngularJS client and Flask server for a RESTful call, one can include the

I am currently facing an issue where I need to send a REST request from my AngularJs client to a Flask server. The problem arises when one of the ids (key) in the request contains a forward slash. Interestingly, if the key does not contain a slash, the re ...

"Encountered an issue with loading a resource in the inspector within Angular

Using angular's ng-repeat, I am uploading some placeholder content. var app = angular.module("homeApp", []); app.controller("entriesView", function ($scope){ $scope.itemEntry = [ { image: "img/112013-priscilla-600.jpg", title: "y ...

How can milliseconds be formatted to a date within a map in a React component?

After receiving an array of objects from the backend (Firebase) with properties url:string and timestamp{seconds: number, nanoseconds: number}, I attempted to use a map function to display all the pictures along with their upload date. However, I encounter ...

FullCalendar displaying inaccurate dates and times

In my ASP.NET MVC application, a full calendar element is displayed as shown below: https://i.sstatic.net/hyAMo.png Here is the JSON data returned by the server through an ajax call for the month of January 2016: [{"id":17,"title":"39/2015 - Site meetin ...

In React.js, it is required to enclose adjacent JSX elements within a parent tag

Help needed with JSX element wrapping in ReactJS What am I missing in my code?? Error message reads as follows: Adjacent JSX elements must be wrapped in an enclosing tag The issue lies within the element tags const renderTodos = currentTodos.map(( ...

Is it possible for my commitment to consistently provide identical values, even when the data varies each time it is invoked?

Initially, the getCart method is invoked in b-navbar.component.ts: export class BNavbarComponent implements OnInit{ appUser: any; cart$ : Observable<ShoppingCart | null>; constructor(private auth : AuthService, private shoppingCartService : S ...

The filtering feature in AngularJS ng-options is not functioning correctly

Greetings, I am a newcomer to angular. In my current demo application, I have created a list of users with a select filter using ng-option. There seems to be a bug that I have been unable to identify. The issue: When I select the Female option, i ...

Comparison: Chrome extension - utilizing default pop-up vs injecting a div directly into the page

I find myself perplexed by the common practices used in popular Chrome extensions. I am currently working on creating my own Chrome extension and after completing a basic tutorial, I have set up a default popup page that appears when clicking the extensi ...

Troubleshooting Issue with Angular's ng-view Implementation

I have been following a tutorial on setting up a MEAN stack single-page application with the URL provided. However, after setting up the application, the views are not displaying in the ng-view div as expected. Here is the structure of my app: - app ---- ...

The image is loaded correctly through the image picker, however, it is not displaying on the screen

When I click the button to pick an image from the gallery in this code, it is supposed to load the phone's gallery and display the selected image in the image component. Even though the image gets loaded properly (confirmed through test logs), it does ...