37

It seems that webpack gets stuck on 92% chunk asset optimization for about 30+ seconds to show a simple js/css change. This is too long for anyone sane to sit and wait that much of their life to see something that should be rendered near instantly.

We're in development mode (so we need source maps, which add to the latency) but it should still NOT be 30+ seconds. Also, we're not using uglify (which I've seen mentioned on GitHub as taking up a good amount of time).

How can we get the build time to be near instant, or much much faster than right now?

UPDATE

Here is the laravel-mix file:

let mix = require('laravel-mix');

mix.react('resources/assets/js/app.js', 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css')
   .options({
     processCssUrls: false
   });

mix.webpackConfig({
    // Note: First build will always be slower regardless
    // Here we're talking about rebuild time

    // If commented out, rebuild is ~6 secs
    // devtool: "inline-source-map",

    // If not commented out, rebuild is 30+ secs
    devtool: "inline-source-map",
});

I found inline-source-map to be the best for quickest debugging, as it provides the most detail on which line of error to fix in source, very very straight forward on what to fix where. I find other types are more cryptic in comparison and there is no indication of which line number to fix in source, so it takes much longer to debug.

How do you guys do it? Is there a way to rebuild really fast while still being able to debug with the error line number in source to fix it (shown in chrome devtools console)?

Wonka
  • 8,244
  • 21
  • 73
  • 121
  • Could you share you webpack config? or at least an external link to see it? I've worked in a medium/big projects with webpack and in dev mode, it only takes half a second to apply changes. The key is the exclude node_modules from your loaders and put vendors in a separate entry. Maybe you are compiling all your node_modules in each build. – The.Bear Mar 09 '18 at 02:02
  • Using default laravel mix webpack configuration (uses many files), but we can override the webpack config (https://github.com/JeffreyWay/laravel-mix/blob/master/docs/quick-webpack-configuration.md) – Wonka Mar 09 '18 at 04:39
  • @The.Bear can you share how the exclusion of node_modules looks like? It's still having the issue.. – Wonka Mar 09 '18 at 15:51
  • I suppose that you are using babel for reactjs, check this https://github.com/babel/babel-loader#troubleshooting. – The.Bear Mar 09 '18 at 15:55
  • The other problem if that you have a huge amount of dependencies, the best you can do is have a separated entry for your vendors, so when you save you src files, the vendor won't be compiled. That saves lot of time. But I don't understand the github that you shared, the laravel webpack mix (I don't even use laravel in my life). I don't find where the webpack.config is present. If you can paste the generated webpack config in your question, I could help you. – The.Bear Mar 09 '18 at 16:01
  • I believe these are the webpack files that all create it: https://github.com/JeffreyWay/laravel-mix/tree/5a481eae7eecbffa28d6dbb61ca061b165b52099/src/builder the earlier link just shows that we can add our custom rules without touching the actual webpack config files (since it's composed of many files). This makes it a bit harder to debug however :/ – Wonka Mar 09 '18 at 16:32

8 Answers8

11

I too faced similar issue while running build remotely, So, in jenkin after adding following command, problem got resolved for me.

export "NODE_OPTIONS=--max_old_space_size=2000"
vipin goyal
  • 671
  • 11
  • 17
4

I do a yarn cache cleaning and it fixed my issue "92% chunk asset optimization TerserPlugin" on my Ubuntu 16.04 host on google cloud.

Not sure if it works on your machine

yarn cache clean

I have this issue on 2nd machine, and this machine requires a reboot.

sudo reboot
halfer
  • 19,824
  • 17
  • 99
  • 186
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
3

Run ng serve --sourceMap=false

dota2pro
  • 7,220
  • 7
  • 44
  • 79
2

I've had great success using a combination of the following:

https://github.com/mzgoddard/hard-source-webpack-plugin

and

https://github.com/amireh/happypack

HardSourceWebpackPlugin is a plugin for webpack to provide an intermediate caching step for modules. In order to see results, you'll need to run webpack twice with this plugin: the first build will take the normal amount of time. The second build will be signficantly faster.

HappyPack makes initial webpack builds faster by transforming files in parallel.

Report back and let me know how it goes.

KFE
  • 637
  • 5
  • 10
  • Thanks @KFE but I was looking for something without additional packages if possible. It seems source maps was the slow down reason as posted in my update, it now takes ~6 secs to rebuild but no good debugging, so focusing the question on the update. – Wonka Mar 09 '18 at 21:26
  • Hi KFE, any idea is its possible to plug this into angular cli ? The ability to have more parallel processing is very interesting to me. – Astronaut Aug 23 '18 at 17:27
  • @Astronaut no idea if it works with angular cli, I haven't used it. This does work with an angular 1.6 x webpack setup though. – KFE Aug 24 '18 at 00:18
0

For me 92% chunk asset was taking forever, so I decided to let it run overnight, after it later I received the following error:

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

Solution: The core problem is that node comes with a default memory restriction to 1.76 GB. If you need more you need to set the option --max_old_space_size={desiredSize} when you start a node process.

Try to increase the memory limit:

https://www.npmjs.com/package/increase-memory-limit

Victoria
  • 165
  • 1
  • 12
0

Anyone googling around with this issue using Node with Angular CLI on Windows 10;

Make sure the directory the CLI is writing to has the appropriate write permissions. I got this exact issue when trying to write to c:/Users/UserName/Documents/SoultionDir

For me it might be to do with company usage policy.

The "92% chunk asset optimization TerserPlugin" message comes up just before it writes to the folder. If the permissions are wrong it silently crashes and hangs forever. Change the directory to one you know has the correct permissions to write to, using an admin command prompt.

Aeptitude
  • 172
  • 3
  • 20
  • Could you give a bit more detail about what you did? We have company rules set up around this as well and it would be good to get some detailed instructions if you're able to give them. – Shafiq Jetha Sep 11 '20 at 15:59
  • I think the "Documents" folder has some odd permissions. I don't know exactly what's wrong there. The folder I used is "C:\Users\username\source" the folder name "source" is arbitrary - you could call it what you want. – Aeptitude Sep 15 '20 at 10:53
0

I got the same issue with following specs

webpack version 5.69.0

my webpack copy plugins options

new CopyPlugin({
  patterns: [
    {
      to({ context, absoluteFilename }) {
        return `./${path.relative(context, absoluteFilename)}`;
      },
      from: 'public',
      globOptions: {
        ignore: ['**/index.html']
      }
    }
  ]
})

How I fixed it added these options to above existing configuration

info: {
   minimized: true
}

so what above does is it tell webpack to not minifiy the files (so the issue was I was having couple of file of about 20MB generated by one of external library and webpack was trying to minify it)

for more info visit https://www.npmjs.com/package/copy-webpack-plugin#info

So the final webpack.prod.js configuration is

const { merge } = require('webpack-merge');
const path = require('path');
const common = require('./webpack.common.js');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = merge(common, {
  mode: 'production',
  plugins: [
    new CopyPlugin({
      patterns: [
        {
          to({ context, absoluteFilename }) {
            return `./${path.relative(context, absoluteFilename)}`;
          },
          from: 'public',
          globOptions: {
            ignore: ['**/index.html']
          },
          info: {
            minimized: true
          }
        }
      ]
    })
  ]
});

I would like to give credit to this answer https://stackoverflow.com/a/69081475/9871509, since it helped me to reach this solution.

Ankit Tiwari
  • 1,069
  • 10
  • 15
-3

I have faced the same problem during the execution of ng build command.

I got the following error:

92% chunk asset optimizationKilled

The process has been stopped at 92% but the following commands are working fine for me.

Try these:

pm2 stop all

ng build

pm2 start all

I am using pm2 as my process manager.

I hope it works for you too.

GMachado
  • 791
  • 10
  • 24
hardy
  • 7
  • 1
  • this has nothing to do with the question asked, doesn't even say they are using pm2 – Krzysztof Krzeszewski Sep 28 '21 at 14:46
  • Yes, you are right. Finally, I got the solution for this issue. This whole game is about memory. During building process, builder required some of free memory. In my case when I stop all pm2 processes couple of memory was released and builder use that for their process and I thought that pm2 is creating the problem. I got this issue each and every time then I increased my swap memory partition in 2019 and from that day to till now-a-day never got same problem again and working without pm2 stop. – hardy Dec 13 '21 at 18:09