angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

It takes forever in "92% chunk asset optimization" can it be cached or disabled?

Open marcalj opened this issue 9 years ago • 84 comments

Bug Report or Feature Request (mark with an x)

- [x] feature request

Versions.

@angular/cli: 1.0.0-rc.2 node: 6.9.2 os: darwin x64 @angular/common: 2.4.10 @angular/compiler: 2.4.10 @angular/core: 2.4.10 @angular/forms: 2.4.10 @angular/http: 2.4.10 @angular/platform-browser: 2.4.10 @angular/platform-browser-dynamic: 2.4.10 @angular/router: 3.4.10 @angular/cli: 1.0.0-rc.2 @angular/compiler-cli: 2.4.10

Desired functionality.

assets optimization it rarely changes when you only do coding, and specially for my project it takes 2-5 minutes each time. So it's killing my productivity. With "--verbose" it doesn't show me any specific files which I could remove temporally to speed up the process of compiling...

Hash: 51ba5014dae3bd179ab9                                                                 
Time: 395021ms
chunk    {0} main.bundle.js, main.bundle.js.map (main) 10.8 MB {4} [initial] [rendered]
chunk    {1} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 278 kB {5} [initial] [rendered]
chunk    {2} styles.bundle.js, styles.bundle.js.map (styles) 751 kB {5} [initial] [rendered]
chunk    {3} scripts.bundle.js, scripts.bundle.js.map (scripts) 362 kB {5} [initial] [rendered]
chunk    {4} vendor.bundle.js, vendor.bundle.js.map (vendor) 4.44 MB [initial] [rendered]
chunk    {5} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry] [rendered]

Thanks.

marcalj avatar Mar 31 '17 09:03 marcalj

it took me 1 hr to make a prod build we are using angular 4.4.6 & my build command is node --max_old_space_size=6048 node_modules/@angular/cli/bin/ng build --prod --aot --env=azureDev --extract-css --preserve-symlinks --named-chunks

image

sanjay2880 avatar Aug 02 '18 13:08 sanjay2880

Wish there was an acknowledgement from the Angular team about this issue and a tentative eta for solution

meghma avatar Aug 02 '18 17:08 meghma

Same here, Getting stuck at 92% chunk asset optimization UglifyJSPlugin for at least 3 min

hammadbinarif avatar Aug 03 '18 04:08 hammadbinarif

@meghma, this item is marked as closed. Maybe open a new issue?

@filipesilva Can you help us with this?

kdubious avatar Aug 03 '18 13:08 kdubious

Had the same issue and found some details on font awesome website:

https://fontawesome.com/how-to-use/with-the-api/other/tree-shaking

The workaround I have currently used is to create a patch.js file with the following content:

const fs = require('fs');
const common = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/common.js';

fs.readFile(common, 'utf8', function (err, data) {
  if (err) {
    return console.log(err);
  }
  var result = data.replace(/compress: {/g, 'compress: { collapse_vars: false,');

  fs.writeFile(common, result, 'utf8', function (err) {
    if (err) return console.log(err);
  });
});

And reference it in the package.json

...

  "scripts": {
    "postinstall": "node patch.js",
...

you'll have to run node/yarn install so it is triggered.

HTH

lucboutier avatar Aug 03 '18 13:08 lucboutier

@lucboutier Doesn't work in my case.

SeverinAlexB avatar Aug 09 '18 11:08 SeverinAlexB

@lucboutier no luck here either, the only thing working for now is disabling compression all together, which is not an option in my case

vandammeb avatar Aug 09 '18 12:08 vandammeb

There's a workaround available for yarn builds, using an alias to replace uglify-es with terser, which includes the root cause fix for the collpase_vars issue. That might be an option for some folks here until that fix makes its way into the Webpack 4 dependencies.

Has anyone tried that for an angular-cli project?

(UPDATE Sept 26, 2018: this change will not make it into Webpack 4 deps, but the Webpack 5 roadmap includes a move to terser which has the fix.)

mlwilkerson avatar Aug 09 '18 16:08 mlwilkerson

Managed to get it working by explicitly overriding all compress settings with their default values according to the docs (I have yet to figure out why that actually works though).

An update for the script from @lucboutier :

const fs = require('fs');
const common = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/common.js';

fs.readFile(common, 'utf8', function (err, data) {
  if (err) {
    return console.log(err);
  }
  var result = data.replace(/compress: {[^}]*}/g, 'compress: {\n' +
    '           arrows: true,\n' +
    '           booleans: true,\n' +
    '           collapse_vars: true,\n' +
    '           comparisons: true,\n' +
    '           computed_props: true,\n' +
    '           conditionals: true,\n' +
    '           dead_code: true,\n' +
    '           drop_console: false,\n' +
    '           drop_debugger: true,\n' +
    '           ecma: wco.supportES2015 ? 6 : 5,\n' +
    '           evaluate: true,\n' +
    '           expression: false,\n' +
    '           global_defs: {},\n' +
    '           hoist_funs: false,\n' +
    '           hoist_props: true,\n' +
    '           hoist_vars: false,\n' +
    '           if_return: true,\n' +
    '           // Workaround known uglify-es issue\n' +
    '           // See https://github.com/mishoo/UglifyJS2/issues/2949#issuecomment-368070307\n' +
    '           inline: wco.supportES2015 ? 1 : 3,\n' +
    '           join_vars: true,\n' +
    '           keep_classnames: false,\n' +
    '           keep_fargs: true,\n' +
    '           keep_fnames: false,\n' +
    '           keep_infinity: false,\n' +
    '           loops: true,\n' +
    '           negate_iife: true,\n' +
    '           // PURE comments work best with 3 passes.\n' +
    '           // See https://github.com/webpack/webpack/issues/2899#issuecomment-317425926.\n' +
    '           passes: buildOptions.buildOptimizer ? 3 : 1,\n' +
    '           properties: true,\n' +
    '           pure_funcs: null,\n' +
    '           pure_getters: buildOptions.buildOptimizer,\n' +
    '           reduce_funcs: true,\n' +
    '           reduce_vars: true,\n' +
    '           sequences: true,\n' +
    '           side_effects: true,\n' +
    '           switches: true,\n' +
    '           toplevel: false,\n' +
    '           top_retain: null,\n' +
    '           typeofs: true,\n' +
    '           unsafe: false,\n' +
    '           unsafe_arrows: false,\n' +
    '           unsafe_comps: false,\n' +
    '           unsafe_Function: false,\n' +
    '           unsafe_math: false,\n' +
    '           unsafe_methods: false,\n' +
    '           unsafe_proto: false,\n' +
    '           unsafe_regexp: false,\n' +
    '           unsafe_undefined: false,\n' +
    '           unused: true,\n' +
    '           warnings: false\n' +
    '         }');

  fs.writeFile(common, result, 'utf8', function (err) {
    if (err) return console.log(err);
  });
});

To prevent the memory issue ng build still has to be run in a context with increased memory available

node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build ...

Hope still helps someone.

vandammeb avatar Aug 09 '18 16:08 vandammeb

Currently experiencing this issue.

Angular CLI: 6.1.3
Node: 8.11.3
OS: win32 x64
Angular: 6.1.2
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.7.3
@angular-devkit/build-angular     0.7.3
@angular-devkit/build-optimizer   0.7.3
@angular-devkit/build-webpack     0.7.3
@angular-devkit/core              0.7.3
@angular-devkit/schematics        0.7.3
@angular/cli                      6.1.3
@ngtools/webpack                  6.1.3
@schematics/angular               0.7.3
@schematics/update                0.7.3
rxjs                              6.2.2
typescript                        2.7.2
webpack                           4.9.2

@egervari 's solution of:

"build-prod": "node --max-old-space-size=8192 node_modules/@angular/cli/bin/ng build --prod --aot",

Makes the build go from ~12 minutes to 2 seconds for me.

gpresland avatar Aug 20 '18 18:08 gpresland

@mlwilkerson: Worked with angular-cli. Thanks for the hint :+1:

Drogglbecher avatar Aug 30 '18 08:08 Drogglbecher

@mlwilkerson: terser is working great - Significantly reduced build times from > 50 minutes to < 2 minutes for me.

Here's what I did (more of a temporary solution):

# Add terser to dependencies
npm install terser --save-dev

# Remove uglify-js
rm -r node_modules/uglify-js
rm -r node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es

# Symlink to terser (which is fully compatible)
ln -s $(pwd)/node_modules/terser $(pwd)/node_modules/uglify-js
ln -s $(pwd)/node_modules/terser $(pwd)/node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es

konstantinkoeppe avatar Aug 30 '18 12:08 konstantinkoeppe

@konstantinkoeppe thanks for the script. building time decreased by 30%

tzjoke avatar Aug 31 '18 03:08 tzjoke

konstantinkoeppe is the only solution I found in this thread. Great job mate.

vitorrd avatar Sep 04 '18 06:09 vitorrd

Trying to build this in a docker container. Had no luck with any of the above approaches. Always hangs at 92%.

Why did this issue get closed?

elie222 avatar Sep 04 '18 18:09 elie222

FYI: I'm using the linux shell on windows. When I use the linux shell it's terribly slow (didn't even wait to finish).

When I use powershell it builds in 10seconds.

dietergoetelen avatar Sep 13 '18 08:09 dietergoetelen

https://github.com/webpack-contrib/uglifyjs-webpack-plugin/issues/272

I found this solution node --max_old_space_size=4096 node_modules/.bin/webpack -p --progress --profile Hopefully, this will help someone.

anupy avatar Sep 14 '18 09:09 anupy

@anupy Any tips on what we do with that line and how it will help? I don't see the connection, yet.

kdubious avatar Sep 14 '18 12:09 kdubious

I had also a very slow build time from up to 20 min. But in my case, i had to optimize the imports. So, the tree shaking will not have to many to do. Especially the optimization of the fontawesome imports to deep imports solved the build time problem. Maybe this will help someone.

Bl4ckPix3l avatar Sep 23 '18 10:09 Bl4ckPix3l

I can confirm moving to terser has done the trick as described by @mlwilkerson , the short simple steps are:

npm i terser --save-dev

and then

  "resolutions": {
    "uglify-es": "npm:terser"
  }

In your package.json

My build reduced to less than a minute. (from over several mins)

inthegarage avatar Oct 25 '18 11:10 inthegarage