How to compress js as small as possible by ng build -prod
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.
With --prod and --aot your js.gz file should be around 250-300kb.
@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.
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?
ng build --prod --aot generate .map. One of them is quite big. Do I do something wrong?
http://take.ms/0wBTz
@a-pavlov-parc Those are just source maps. Nothing a "normal" user will download. Those are for debugging purposes.
@grizzm0 please let me know is there a way to turn generating of that off?
@a-pavlov-parc wait for the new release. It will have this option. Check latest master commit.
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 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 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
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
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.
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
@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-optimizerIf you want to further compress the build say reduce 300kb(approx) from , then follow the below process;
Create a folder called
vendorinside thesrcfolder and inside vendor folder create a filerxjs.tsand 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.jsonfile 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?
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.