Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chunkName not displayed in angular 8 lazy loading with webpack 4

LazyLoading chunkName is not being displayed

webpack.config.js

const webpack = require('webpack');
const path = require('path');

var PROD = JSON.parse(process.env.PROD_ENV || '0');

module.exports = {
    entry: {
        clientApp: "./App/Modules/main",
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: "[name].bundle.js",
        chunkFilename: '[name]-[chunkhash].js',
        publicPath: '/dist/',
    },
    resolve: {
        extensions: ['.js']
    },
    module: {
        loaders: [ 
            'angular-router-loader'
        ]
    },

    plugins: [
    ]
}

app.routing.ts

{
                path: 'about',     
                loadChildren: () => import('../Features/about/about.module').then(m => m.AboutModule)   
            }

enter image description here

It is not displaying module name for the chunk. what am i doing wrong here?

like image 240
jack Avatar asked Nov 18 '19 18:11

jack


People also ask

How to lazy load common chunk in angular?

What we’re going to look at in this article is lazy loading in Angular, but in specific about the “common chunk” and the “commonChunk” option in the Angular builder configuration. The easiest way of applying lazy loading is to use the Angular Router’s built-in functionality.

What is code splitting in angular and lazy loading?

Let’s talk about code splitting in Angular, lazy-loading and a sprinkle of Webpack. Code splitting allows us to essentially break our codebase down into smaller chunks and serve those chunks on demand, which we call “lazy loading”. So, let’s learn how to do it and some of the concepts/terminology behind it. Want the code?

What is on demand lazy loading in angular?

This is where “on demand” comes into play. Lazy loading is the process in taking already “code split” chunks of our application, and simply loading them on demand. With Angular, the router is what allows us to lazy load. We call it “lazy” because it’s not “eagerly” loading - which would mean loading assets upfront.

Does code splitting lazy load a chunk?

Let's take the example from Code Splitting and tweak it a bit to demonstrate this concept even more. The code there does cause a separate chunk, lodash.bundle.js, to be generated and technically "lazy-loads" it as soon as the script is run.


Video Answer


1 Answers

The way that Angular named the chunks with the old string based loadChildren attribute was with a custom Angular webpack plugin similar to this one. The way that named code spliting chunks is done with straight webpack is with "Magic Comments" specifically the webpackChunkName comment - when I test this, it works for my development build, but as soon as I switch to a production build, it goes away and I am back to enumerated JavaScript files. Here is what my import looked like:

{
  path: 'account',
  loadChildren: () => import(/* webpackChunkName: "account" */ './account/account.module')
    .then((m) => m.AccountModule)
}

When this was done, my chunk gets the name account.js when built for dev, but when built for production, it is still 1.js. I don't know the exact reason that the production build differs, maybe a conflict with another webpack plugin, maybe it is related to AOT, it is hard to know for sure.

I do not think that Google/Angular will publish a webpack plugin because this problem is 1.) not really blocking development (we can just rely on source maps), 2.) simply low priority. Writing a plugin for this means supporting even more code. Here is a bug report where they state as much: https://github.com/angular/angular-cli/issues/16697 named chunks with magic comments is not something they test for, so it will likely not work.

While the string method of async loading children of a route is deprecated, it still works and we can still use it to name chunks.

like image 63
nephiw Avatar answered Oct 06 '22 00:10

nephiw