2

We are building an Angular application that should have the capability of discovering and dynamically loading custom libraries, i.e. the libraries that are not known/available at build time.

To support that, we cannot tree-shake the main application since these custom libraries can use symbols from third-party libraries (e.g. angular, rxjs) that the main application does not.

What would be the way to solve it with angular-cli v6? Is there a way to disable tree-shaking in the production build?

3
  • Why would these dynamic/custom libraries not load their own dependencies? Also where have you read that angular supports this? The question is a little light on the technical details on how you would go about doing this... Commented May 16, 2018 at 21:43
  • @Igor, Are you suggesting that each custom library should be packaged with e.g. it's own copy of angular? These libraries have angular and other common dependencies provided by the platform (main application) in its peerDependencies. Where did I read that angular supports what exactly? Disabling tree-shaking? Please explain what other technical details you are looking for? Commented May 17, 2018 at 14:41
  • Maybe, hard to say. The question is ambiguous so any answer or comment you are going to get is also going to be ambiguous. It would be better if you could provide a concrete example and include an minimal reproducible example for how you plan on dynamically loading external libraries. If that is not an option then maybe this is relevant to your question? github.com/angular/angular-cli/issues/5275 Commented May 17, 2018 at 14:48

1 Answer 1

1

I think the tree shaking process is part of the angular-cli AOT build feature, but the documentation itself does not give a lot of information about how exactly this is done and weather or not you can enable/disable this feature. As far as I'm concerned, there is no straight forward way to just disable tree shaking within the angular-cli. The cli documentation states the following:

All builds make use of bundling and limited tree-shaking, while --prod builds also run limited dead code elimination via UglifyJS.

So this seems to be pretty much hooked into AOT. Disabling tree shaking is probably also contrary to what the angular-cli has been trying to achieve with its building options for the last year or so, mainly to make it an easy to use tool to decrease angular's bundling sizes.

The angular-cli provides some options to adjust the building, but for the tree shaking part, I guess you either have to go with it somehow, or, if not possible, you probably have to dig a lot deeper and try to create your own build with something like webpack (which the cli uses underneath its hood I think). You can adjust pretty much everything there. This freedom of course comes with the cost of an increased time investment to create an efficient build that is comparable to angular's build command.

Sign up to request clarification or add additional context in comments.

4 Comments

High costs of maintaining the custom build configuration is the reason we are evaluating angular-cli (after support for libraries was added in v6). I tried turning off aot but the unused code still gets striped.
I see, I got quite wrong then. I checked the documentation again, and the quote actually describes this pretty well, saying that all builds make us of bundling and limited tree-shaking which seems to also include the jit builds.
Nonetheless if it is rooted this deep inside the builds, this might be tricky. AOT and the build optimizer maybe also rely on this preproduced step, which is not gonna make it easier to skip. Anyways, have you checked the build documentation? Every cli option for the build is listed here, maybe there's something that you can build on.
Yes, I did check all of the options. It does not look promising. There is one option with no details on it (--optimization - Defines the optimization level of the build), will have to look in the code to see what it does. We may end up not using --prod but even that may not help if "all builds make use of ... limited tree-shaking".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.