Skip to content

Fix potential failure for AMD loaders with lazy load - #3620

Closed
Justineo wants to merge 3 commits into
babel:masterfrom
Justineo:fix-amd-lazy-load
Closed

Justineo wants to merge 3 commits into
babel:masterfrom
Justineo:fix-amd-lazy-load

Conversation

@Justineo

Copy link
Copy Markdown

Package: babel-plugin-transform-es2015-modules-amd
Related issue: https://phabricator.babeljs.io/T7382

import 'foo';

Currently the code above is transformed into:

define(['foo'], function () {});

For AMD implemetations, it is explicitly allowed to not execute the module factory of foo in this kind of situation (https://github.com/amdjs/amdjs-api/wiki/AMD#dependencies-):

The dependencies argument is optional. If omitted, it should default to ["require", "exports", "module"]. However, if the factory function's arity (length property) is less than 3, then the loader may choose to only call the factory with the number of arguments corresponding to the function's arity or length.

So foo's factory may never have chance to be called in certain AMD-compatible loaders.

I've added an addBareSourceParams option for babel-plugin-transform-es2015-modules-amd, which will append params corresponding to those bare imports to the module's param list.

The output looks like this with this option set to true:

define(['foo'], function (_temp) {});

@codecov-io

codecov-io commented Jul 31, 2016 •

Copy link
Copy Markdown

Codecov Report

❗ No coverage uploaded for pull request base (master@2127df0). Click here to learn what that means.
The diff coverage is n/a.

@@            Coverage Diff            @@
##             master    #3620   +/-   ##
=========================================
  Coverage          ?   88.28%           
=========================================
  Files             ?      194           
  Lines             ?    13620           
  Branches          ?     3142           
=========================================
  Hits              ?    12024           
  Misses            ?     1596           
  Partials          ?        0

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2127df0...d2a7eef. Read the comment docs.

@hzoo

hzoo commented Aug 20, 2016

Copy link
Copy Markdown
Member

Hmm ok, so would doing this by default be non-backwards compatible?

@Justineo

Copy link
Copy Markdown
Author

This is backward compatible but for those who use RequireJS or other certain AMD loaders, the extra params are useless so people may not want to have them in the results. So make it optional should be fine I think.

@danez

danez commented Aug 23, 2016 •

Copy link
Copy Markdown
Member

As far as I understand the amd specs it says something like:

If there are no dependencies, it should default to ["require", "exports", "module"] and if there are less than 3 params in the factory it might only supply ["require"] (1) or ["require", "exports"] (2).

This all applies when there are no dependencies afaics, but we always set dependencies.
Imho it would be fatal if the loader would not resolve dependencies if they are explicitly in the dependencies array, even if not used.

Ah by some further investigation I now understand that some loaders are doing some lazy loading, so in that case we might just enable this by default without option. As the useless params would/could be stripped by minifiers later anyway, if desired. Or is there any problem you see with having this enabled by default? @Justineo

The reasoning here is that we want to avoid options if it is avoidable, to not make babel a more complex config monster. :)

@Justineo

Justineo commented Aug 24, 2016 •

Copy link
Copy Markdown
Author

@danez whether to strip "useless" params seem to differ among different minifiers. I can't find how to let Google Closure Compiler keep unused function params, while UglifyJS has an option (keep_fargs) for this (which keeps them by default). But as unused function params seem to be always "strippable" for minifiers, I think it's OK for us to keep them by default in Babel. I'll update the PR to make it a default behavior today.

@Justineo Justineo changed the title Add an option to allow side-effect-only imports generate factory params Fix potential failure for AMD loaders with lazy load Aug 24, 2016
@Justineo

Copy link
Copy Markdown
Author

Hi guys, how can I get this PR merged?

@Justineo
Justineo force-pushed the fix-amd-lazy-load branch from 85c2bbc to d2a7eef Compare March 3, 2017 09:40
@lock lock Bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 5, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Oct 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants