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

[feature-request] - Externals option in angular-cli.json config

Open jcimoch opened this issue 8 years ago • 8 comments

Versions

Angular CLI: 1.7.3
Node: 9.4.0
OS: win32 x64
Angular: 5.2.9
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cli: 1.7.3
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.2
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.5.3
webpack: 3.11.0

Observed behavior

Right now if we want to use some external library from CDN and also use it's typings (for example leaflet library) we need to instal both typings and library via npm so the typescript compiler can recognize types properly. However in such situation webpack will transform those imports (required for typings) to actual bundling whole library. So we have the same library twice, once from cdn inside index.html and second inside main.js bundle.

Desired behavior

It would be nice to have a option for externals which is already part of webpack, so we could tell webpack to not bundle particular libraries.

jcimoch avatar Mar 28 '18 10:03 jcimoch

This might already be possible in the latest beta release. Note the beta changes project structure a bit, needs minor diff work.

ghost avatar Mar 29 '18 06:03 ghost

@robertbaker I don't see that option for beta release changelog, could you provide some resource?

jcimoch avatar Mar 30 '18 07:03 jcimoch

There's no docs up yet, as it's still beta. The new angular.json file appears much more robust. I would install it and generate a new project :) to see it.

ghost avatar Mar 30 '18 08:03 ghost

Is there any update on whether there are plans to support this in the future?

nicknisi avatar May 21 '18 17:05 nicknisi

seconding the question if support at cli level is planned? For now we have to resort to some other 3rd party like https://github.com/manfredsteyer/ngx-build-plus to have that covered, which is far from ideal.

banjankri avatar Apr 03 '19 07:04 banjankri

Is there any particular reason you want to load a library via CDN when it is also present on NPM? Loading a CDN script tag just means you're forced into using this as a global variable and can't import or lazy load it. It causes a "hidden" dependency not listed in your package.json. It's just generally less convenient than loading the dependency through NPM.

The one use case I can see is if you are migrating a legacy system and can't get rid of the global symbol. For example, if you're using jQuery in non-Angular pieces of an application and don't want to include jQuery in your Angular bundle as well. In that scenario the best option would probably be to use:

import type { jQuery } from 'jquery';
declare global {
  interface Window {
    readonly $: jQuery;
  }
}

console.log($('.foo'));

You'll still need to install @types/jquery in this example (though I don't think there is any way around that, even with the externals option you're asking for). The import type will make any value-usage of the symbol an error, so it will guarantee that you don't accidentally include jQuery in the bundle.

Would this be sufficient for your use case, or is there something we're not seeing here?

dgp1130 avatar Jun 24 '20 17:06 dgp1130

@dgp1130 well I created the issue 2 years ago, and indeed lots have changed since that time. We have angular 10 and typescript supports nowadays type imports. So indeed I think the issue can be solved with the toolbox we have now. Thanks for replay anyway ;)

jcimoch avatar Jul 10 '20 20:07 jcimoch

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.