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

How to compress js as small as possible by ng build -prod

Open suplch opened this issue 10 years ago • 15 comments

I make a angular2 project by angular-cli tool, and write little business code. However, In dist folder, the output file main.xxxxxxxxxxx.bundle.js still have 1M size. Are there good way to reduce its size as small as posible.

suplch avatar Nov 18 '16 07:11 suplch

With --prod and --aot your js.gz file should be around 250-300kb.

grizzm0 avatar Nov 18 '16 08:11 grizzm0

@suplch like @grizzm0 said. using the --prod and --aot flag should give you a hello world app between 80 and 100kb depending on which angular modules you are using. My company has one angular2 app in prod now and it is sitting ~210k for the main bundle.

deebloo avatar Nov 18 '16 14:11 deebloo

Thanks very much, I add --aot flag, the main bundle file reduce from 1M to 780K, that is very ok. Another question, I explore website angular.io about "AoT" and "Tree Shaking", Is Our "--aot" flag from "Tree Shaking" principle?

suplch avatar Nov 21 '16 04:11 suplch

ng build --prod --aot generate .map. One of them is quite big. Do I do something wrong?

http://take.ms/0wBTz

alpavlove avatar Nov 21 '16 09:11 alpavlove

@a-pavlov-parc Those are just source maps. Nothing a "normal" user will download. Those are for debugging purposes.

grizzm0 avatar Nov 21 '16 10:11 grizzm0

@grizzm0 please let me know is there a way to turn generating of that off?

alpavlove avatar Nov 21 '16 10:11 alpavlove

@a-pavlov-parc wait for the new release. It will have this option. Check latest master commit.

elvirdolic avatar Nov 21 '16 10:11 elvirdolic

Closing as the most excellent @grizzm0, @deebloo and @elvirdolic have already answered everything, cheers!

To turn off sourcemap generation you can use --no-sourcemap.

filipesilva avatar Dec 29 '16 18:12 filipesilva

@filipesilva can you please explain the function of those .map files at the time of ng build ? also once i ran ,ng build --aot --prod angular will automatically remove .map files , i haven't set sourceMap to false yet. how's it possible any comments on this ?

PardeepWildnet avatar Sep 13 '17 06:09 PardeepWildnet

@PardeepWildnet Source maps are no longer generated by default during production build as this "exposes" your non uglified/minimized application. Aot is also default these days so ng build --prod is enough. If really want to use sourcemaps in prod you'd have to do ng build --prod --sourcemap

grizzm0 avatar Sep 13 '17 06:09 grizzm0

The following solution assumes you are serving your dist/ folder using nodejs. Please use the following app.js in root level

const express = require('express'),http = require('http'),path = require('path'),compression = require('compression');

const app = express();

app.use(express.static(path.join(__dirname, 'dist')));
app.use(compression()) //compressing dist folder 
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist/index.html'));
})

const port = process.env.PORT || '4201';
app.set('port', port);

const server = http.createServer(app);
server.listen(port, () => console.log('Running at port ' + port))

Make sure you install dependencies;

npm install compression --save
npm install express --save;

Now build the app

ng build --prod --build-optimizer

If you want to further compress the build say reduce 300kb(approx) from , then follow the below process;

Create a folder called vendor inside the src folder and inside vendor folder create a file rxjs.ts and paste the below code in it;

export {Subject} from 'rxjs/Subject';
export {Observable} from 'rxjs/Observable';
export {Subscription} from 'rxjs/Subscription';

And then add the follwing in the tsconfig.json file in your angular-cli application;

"paths": {
      "rxjs": [
        "./vendor/rxjs.ts"
      ]
    }

This will make your build size way too smaller. In my project I reduced the size from 11mb to 1mb. Hope it helps

renilbabu03 avatar Mar 07 '18 04:03 renilbabu03

hey i tried what you told but i am still getting the same.

    $ ng build --prod --build-optimizer
    Date: 2018-04-26T14:51:31.990Z
    Hash: 6c5bc40107c4145d6b81
    Time: 125927ms
    chunk {scripts} scripts.9b77efa65fc60471c617.bundle.js (scripts) 135 kB [initial] [rendered]
    chunk {0} polyfills.bf95165a1d5098766b92.bundle.js (polyfills) 59.4 kB [initial] [rendered]
    chunk {1} main.84c35a0e5dacecb2605b.bundle.js (main) 2.52 MB [initial] [rendered]
    chunk {2} styles.e2a977bfc9a0870da076.bundle.css (styles) 169 kB [initial] [rendered]
    chunk {3} inline.318b50c57b4eba3d437b.bundle.js (inline) 796 bytes [entry] [rendered]

it shows 2.52 MB. i want to decrease the size of the main bundle. please help me.

kumaresan-perumal avatar Apr 26 '18 14:04 kumaresan-perumal

is any body having idea to reduce the size of main.ts file of Angular6 ,as i already tried ng build --prod --build-optimizer ng build --aot --prod

m using rxjs6 ,search alot but haven't got the proper answer for the currentt version on Angular

ashishsober avatar Dec 29 '18 22:12 ashishsober

@re

The following solution assumes you are serving your dist/ folder using nodejs. Please use the following app.js in root level

const express = require('express'),http = require('http'),path = require('path'),compression = require('compression');

const app = express();

app.use(express.static(path.join(__dirname, 'dist')));
app.use(compression()) //compressing dist folder 
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist/index.html'));
})

const port = process.env.PORT || '4201';
app.set('port', port);

const server = http.createServer(app);
server.listen(port, () => console.log('Running at port ' + port))

Make sure you install dependencies;

npm install compression --save
npm install express --save;

Now build the app

ng build --prod --build-optimizer

If you want to further compress the build say reduce 300kb(approx) from , then follow the below process;

Create a folder called vendor inside the src folder and inside vendor folder create a file rxjs.ts and paste the below code in it;

export {Subject} from 'rxjs/Subject';
export {Observable} from 'rxjs/Observable';
export {Subscription} from 'rxjs/Subscription';

And then add the follwing in the tsconfig.json file in your angular-cli application;

"paths": {
      "rxjs": [
        "./vendor/rxjs.ts"
      ]
    }

This will make your build size way too smaller. In my project I reduced the size from 11mb to 1mb. Hope it helps

By this, we are getting circular definition errors for the modules which are exported manually from vendor/rxjs.ts

src/vendor/rxjs.ts(3,1): error TS2303: Circular definition of import alias 'Observable'. src/vendor/rxjs.ts(4,1): error TS2303: Circular definition of import alias 'SubscriptionLike'. src/vendor/rxjs.ts(5,1): error TS2303: Circular definition of import alias 'Subscription'. src/vendor/rxjs.ts(6,1): error TS2303: Circular definition of import alias 'throwError'. src/vendor/rxjs.ts(7,1): error TS2303: Circular definition of import alias 'BehaviorSubject'. src/vendor/rxjs.ts(8,1): error TS2303: Circular definition of import alias 'Observer'. src/vendor/rxjs.ts(9,1): error TS2303: Circular definition of import alias 'Operator'. src/vendor/rxjs.ts(10,1): error TS2303: Circular definition of import alias 'ObservableInput'. src/vendor/rxjs.ts(11,1): error TS2303: Circular definition of import alias 'Unsubscribable'. src/vendor/rxjs.ts(12,1): error TS2303: Circular definition of import alias 'TeardownLogic'. src/vendor/rxjs.ts(13,1): error TS2303: Circular definition of import alias 'SchedulerLike'. src/vendor/rxjs.ts(14,1): error TS2303: Circular definition of import alias 'ReplaySubject'.

Anyone faced the same scenario?

HardikDG avatar May 30 '19 07:05 HardikDG

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.