Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intelligently load polyfills #6577

Closed
deebloo opened this issue Jun 5, 2017 · 25 comments
Closed

Intelligently load polyfills #6577

deebloo opened this issue Jun 5, 2017 · 25 comments
Labels
area: @angular-devkit/build-angular feature Issue that requests a new feature
Milestone

Comments

@deebloo
Copy link
Contributor

deebloo commented Jun 5, 2017

Feature Request

- [ ] bug report -> please search issues before submitting
- [x] feature request

Versions.

future

Desired functionality.

Apologies if this already tracked somewhere I couldn't find it/
At the moment all polyfills are loaded into all browsers, it would be awesome if we could only load the polyfills that are needed by the browser the app is running in. This would help will smaller overall payload size for more modern browsers and keep legacy browsers working without having to manually manage the polyfills.ts file.

Additional information

Article for reference: https://philipwalton.com/articles/loading-polyfills-only-when-needed/

Initial thoughts

  1. a script could be inlined (maybe) to load the polyfills that we know for sure angular requires
  2. the initial bootstrapping would need to wait until the polyfills were loaded or even better, not loaded. (reference bottom of the referenced article)
  3. polyfills.ts would still exist but would only be used to load zone.js and additional polyfills that the user wants to add.
@filipesilva filipesilva self-assigned this Jun 6, 2017
@filipesilva filipesilva added P5 The team acknowledges the request but does not plan to address it, it remains open for discussion feature Issue that requests a new feature type: discussion labels Jun 6, 2017
@filipesilva
Copy link
Contributor

I don't think there's another issue for this, but I remember @clydin was investigating possible ways of implementing it.

@hccampos
Copy link

hccampos commented Sep 1, 2017

How about something like https://polyfill.io/v2/docs/ ?

@filipesilva
Copy link
Contributor

@hccampos that's definitely an option, but it's something that one use right now with no changes to the CLI.

A cool thing to do would be to solve this issue and #2907 at once via a .browserlist-like approach (#2907 (comment)).

@deebloo
Copy link
Contributor Author

deebloo commented Sep 4, 2017

The only thing about using the polyfill.io website by default is that it won't work with systems without broad access to the internet. (I know that sounds weird but it's more common then you might think)

@aciccarello
Copy link
Contributor

aciccarello commented Sep 19, 2017

Philip Walton put out another article on a similar topic. I would be happy with his approach of one bundle for modern browsers and a legacy fallback script bundle.

Now that #2907 is resolved, is there a way to configure the CLI to do two separate builds and use the type="module" import for ES2015 and nomodule imports with a polyfill and ES5? Ideally the CLI would make the configuration simple but I'd be okay with something more complicated for now.

@firstor
Copy link

firstor commented Sep 30, 2017

I also need this issue to be resolved.

I solved this issue myself according to Philip Walton's article and so in my workaround two different polyfills are prepared and polyfills.ts acts as a router to serve different polyfills for different browser targets :

  • polyfills.ts
import * as checkBrowser from 'check-browser';
let evergreenBrowser = checkBrowser({
    chrome: 49,
    firefox: 52,
    edge: 14,
    safari: 10
});

declare var System: any;

/** Import basic polyfills required by Angular itself */
import './polyfills.basic';

/** Import optional polyfills to target browsers */
if (!evergreenBrowser) {
    System.import('./polyfills.target');
}
  • polyfills.target.ts
/***************************************************************************************************
 * BROWSER POLYFILLS
 */

/** IE9, IE10 and IE11 requires all of the following polyfills. **/
import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/string';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/set';

/** IE10 and IE11 requires the following for NgClass support on SVG elements */
import 'classlist.js';  // Run `yarn add classlist.js`.

/** IE10, IE11 and all Firefox browsers require the following to support `@angular/animation`. **/
/** ALREADY IMPORTED WITH POLYMER COMPONENTS */
// import 'web-animations-js';  // Run `yarn add web-animations-js`.


/***************************************************************************************************
 * APPLICATION IMPORTS
 */

/**
 * Date, currency, decimal and percent pipes.
 * Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10
 */
import 'intl';  // Run `yarn add intl`.
/**
 * Need to import at least one locale-data with intl.
 */
import 'intl/locale-data/jsonp/en';

The workaround seems working perfectly, but recently I found a critical issue on IE (and plus on Edge probably), that is, my app cannot be loaded the very first time after IE cache is completely cleaned, and it can be loaded without problem after refreshing browser ~ similar with this issue: Polymer/polymer#4760

So any suggestion on this?

@hccampos
Copy link

hccampos commented Oct 1, 2017 via email

@firstor
Copy link

firstor commented Oct 2, 2017

@hccampos Thank you for the update. But then how can I accomplish that?
For me, the best way is to import synchronously like this:

...
/** Import basic polyfills required by Angular itself */
import './polyfills.basic';

/** Import optional polyfills to target browsers */
if (!evergreenBrowser) {
    synchronously-import('./polyfills.target');
}

Is there any way to accomplish that?

@mcterrySep
Copy link

I am also interested in this feature.

@mcterrySep
Copy link

@First87 -- did you have luck with your approach from above? if so, can you share with me what you ended up with?

@aciccarello
Copy link
Contributor

Here's a related issue for the nomodule approach: jantimon/html-webpack-plugin#782

@dharapvj
Copy link

See how angular.io website splits IE polyfills out..
Issue
Fix

Can something like this be implemented? It does not seem very difficult.

@mcterrySep
Copy link

@dharapvj are you asking if the angular-cli team is going to implement this? or are you recommending that app developers go around angular-cli's framework for polyfils and do this?

@dharapvj
Copy link

dharapvj commented Jan 2, 2018

@mcterrySep - i am sorry I was not very clear.. i was just making suggestion that something like what is done for main angular.io site could also be implemented for generation via angular-cli.

HTH..

@tmakin
Copy link

tmakin commented Jan 19, 2018

I just tried using polyfill.io but couldn't get my app working in IE11, so have reverted to corejs. This is the url I used:
https://cdn.polyfill.io/v2/polyfill.min.js?features=es5,es6

Has anyone found a feature set for polyfill.io that works with Angular 5 and IE11?

@firstor
Copy link

firstor commented Jan 23, 2018

@mcterrySep Since I found a problem with this approach: #6577 (comment), I adopted another approach to load polyfills intelligently - the key is to prepare multiple builds and serve the proper one of them to each target.

@filipesilva filipesilva added comp: cli/build and removed P5 The team acknowledges the request but does not plan to address it, it remains open for discussion labels Feb 14, 2018
@filipesilva
Copy link
Contributor

@cyrilletuzi also suggested (#10337) using the browserlist file to determine whether to use ES5 or ES2015 should be used.

@cyrilletuzi
Copy link
Contributor

Inspiration could be taken from babel-present-env, which uses browserslist.

@CRACKISH
Copy link

What is the final solution for this problem?

@clydin
Copy link
Member

clydin commented Jan 25, 2019

Support for this will be available in CLI version 7.3 currently in release candidate stage. An overview of the functionality can be found in the PR description found here: #13403

@clydin clydin closed this as completed Jan 25, 2019
@michaelz
Copy link

@clydin Will this work with other browsers that don't support ES2015, e.g. chrome 41 ?

@tomgruszowski
Copy link

Love this feature, are there any docs on how to enable this?

@mackelito
Copy link

What about ES7 polyfills?

@crysislinux
Copy link

I would like es5BrowserSupport be changed to legacyBrowserSupport and let the user define an array of polyfills to be included. es5BrowserSupport is limited in my opinion.

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: @angular-devkit/build-angular feature Issue that requests a new feature
Projects
None yet
Development

No branches or pull requests