Skip to content

Add babel option to pre-include polyfills #607

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

Closed
wants to merge 1 commit into from
Closed

Add babel option to pre-include polyfills #607

wants to merge 1 commit into from

Conversation

ihmels
Copy link
Contributor

@ihmels ihmels commented Jul 10, 2019

This PR adds an polyfills option to Encore.configureBabel() to configure the include option of @babel/preset-env. It's inspired by the polyfills option of @vue/babel-preset-app.

This option is useful to include polyfills that are required by project dependencies and therefore cannot be covered by useBuiltIns: 'usage'.

@Lyrkan
Copy link
Collaborator

Lyrkan commented Jul 10, 2019

Hi @ihmels,

Thank you for opening this PR.
I kind of agree that we should add a way to set include (and exclude maybe?) but I'm less convinced by the use case you are describing (which is important since the "why" should be explained in the doc) and the name of that new option.

If a dependency requires polyfills it should probably be handled by includeNodeModules (or by changing the exclude rule). The reasons for that are:

  • you'd have to know the code of that dependency in order to know which polyfills need to be added (which is probably not the case most of the time)
  • Babel doesn't only handle polyfills and if the dependency requires syntax changes (for instance if it uses the class keyword and you're targeting IE11) it won't be covered

The description of include given in the documentation @babel/preset-env is a bit more realistic I think:

An array of plugins to always include
(...)
This option is useful if there is a bug in a native implementation, or a combination of a non-supported feature + a supported one doesn't work.

Also, could you also add some tests to check that it works as expected?

@ihmels
Copy link
Contributor Author

ihmels commented Jul 10, 2019

I'm not of the opinion that it should be handled by includeNodeModules if a dependency requires a polyfill to work, because it does not follow that I also want to transpile that dependency.

But I think it is a good idea to add a way to set include and exclude in general. Do you have an idea how the two options can be called? I was thinking about includeEnvPlugin and excludeEnvPlugin. Actually, a more general solution would be better to configure @babel/present-env with an additional method like configureBabelPresetEnv() or so.

@Lyrkan
Copy link
Collaborator

Lyrkan commented Jul 10, 2019

I'm not of the opinion that it should be handled by includeNodeModules if a dependency requires a polyfill to work, because it does not follow that I also want to transpile that dependency.

That's my point, if a dependency requires a polyfill to work you already know that it uses things that are not supported by your env, so it would be way safer to transpile it anyway.

And if you really know that you are only missing polyfills you should already be able to add them by importing individual core-js modules.

Do you have an idea how the two options can be called? I was thinking about includeEnvPlugin and excludeEnvPlugin

I can't find anything better right now, maybe just with the plural form instead since they'll most likely contain arrays?

Actually, a more general solution would be better to configure @babel/present-env with an additional method like configureBabelPresetEnv() or so.

Hmm... I'm not sure that's needed. For more advanced configurations having a babel.config.js/.babelrc.js file is probably a better choice (while writing this I'm even wondering if that shouldn't be the case for include/exclude as well).

@weaverryan
Copy link
Member

Hmm... I'm not sure that's needed. For more advanced configurations having a babel.config.js/.babelrc.js file is probably a better choice (while writing this I'm even wondering if that shouldn't be the case for include/exclude as well).

The downside to that would be that you would now be responsible for configuring all of the Babel config (e.g. can't take advantage of us adding the react preset if you enable React).

BUT, I hear your point. Configuring the env preset is already possible, but ugly as you'd need to rely on the preset being on the 0 index.

Encore
    .configureBabel((babelConfig) => {
        babelConfig.presets[0][1].include = ['foo-plugin'];
    });

That's not great :). We DO expose some copy "options" as the second argument of configureBabel() (e.g. useBuiltIns), but trying to repeat ALL the preset-env options there is a bad idea and not what we intended.

If we're going to solve this, I think it would be via some sort of configureBabelPresetEnv() as @ihmels suggested. Otherwise, your options for configuring these less-common options include either (A) using configureBabel() with very ugly [0][0] keys or (B) using a babel.config.js file, and then not allowing Encore to handle the Babel config.

WDYT?

@ihmels
Copy link
Contributor Author

ihmels commented Sep 29, 2019

I have created #642 that introduces the method configureBabelPresetEnv().

@ihmels ihmels closed this Sep 29, 2019
@ihmels ihmels deleted the pre-include-polyfills branch December 6, 2019 07:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants