Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get one file as output of angular cli

I'm working on ng2 application and I use @angular/cli to build it. As output it emits several js files like .inline.js, .vendor.js etc.

The question is -- how to set up angular-cli to emit only one file as a result, i.e. bundle vendor.js, inline.js etc. into one big file?

I understand that it could be done by using additional bundler but it would be nice to achieve it via ng-cli

PS I don't use lazy loading in this app and definitely won't.

PPS Just concat files afterwards is not an option because:

  • it doesn't work with hash in file name, need to update html as well
  • it introduces an additional build step which is not necessary

As for now it looks like moving to pure webpack would be the easiest and best option

UPDATE

there is possibility to avoid vendor.js setting --vendor-chunk to true but as a result I still get several files: inline.bundle.js main.bundle.js polyfills.bundle.js

like image 321
Leo Avatar asked Mar 21 '17 16:03

Leo


People also ask

What command is used to build an angular app to a separate output directory?

ng buildlink. Compiles an Angular application or library into an output directory named dist/ at the given output path.

What is the output of NG build?

The ng build command is intentionally for building the apps and deploying the build artifacts. The command does not generate an output folder. The output folder is – dist/. The ng serve builds artifacts from memory instead for a faster development experience.

What is output hashing angular?

--output-hashing all — hash contents of the generated files and append hash to the file name to facilitate browser cache busting (any change to file content will result in different hash and hence browser is forced to load a new version of the file)

What is SRC file in angular?

src folder: This is the folder which contains the main code files related to your angular application. app folder: The app folder contains the files, you have created for app components. app.


1 Answers

Angular CLI natively doesn't support this.

There are other solutions, including further processing with other tooling after Angular CLI finishes doing its thing, but this will hinder or remove the debugging capabilities Angular provides out-of-the-box.

As the ng eject command is also being deprecated, the option to reconfigure webpack is not as attractive as it used to be. However, that is still technically a possibility.

My solution:

The best solution I have found is the ngx-build-plus plugin, which can be found at https://github.com/manfredsteyer/ngx-build-plus or added to a project via the ng add ngx-build-plus Angular-CLI command currently. Once installed, it provides additional configuration options for building, including the --single-bundle true flag that can be added to an ng build or ng serve command.

With that flag, a main.js file and a main.js.map file are created, already referenced in the HTML, and it'll just run like that properly out of the box, with full source mapping and debugging.

like image 134
Qwertronix Avatar answered Sep 21 '22 18:09

Qwertronix