Skip to content

side effect only import statement should be a param for AMD's factory function (T7382) #3802

Closed

Description

@babel-bot

Issue originally made by @otakustay

Bug information

  • Babel version: 6.7.7
  • Node version: 6.0.0
  • npm version: 3.8.6

Options

--plugin=transform-es2015-modules-amd

Description

Babel have different behaviors whether an import statement is bound to an identifier

For the code below:

import foo from 'foo';

babel outputs:

define(['foo'], function (_foo) {
  'use strict';

  var _foo2 = _interopRequireDefault(_foo);

  function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
      default: obj
    };
  }
});

which is exactly correct and well worked, however with a side effect only import statement:

import 'foo';

babel outputs:

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

The problem is, babel only place the module foo in dependencies array and omit it in params of factory function.

Some AMD Loader have a behavior called "lazy evaluate factory" in which it eagerly invoke factories of dependencies if it is defined as a param of factory function, but when not in factory's param list, the invocation is delayed to the first time this module is required, in this case, foo's factory never execute in the second example

The AMD specification does not specify when should factories be invoked, so we cannot say loaders with "lazy evaluate factory" behavior are not valid loaders.

I propose that the transform-es2015-modules-amd plugin could transform the second example to:

define(['foo'], function (require) {
    // There is no need fo _interopRequireDefault since we just want the factory to execute but not intrested in exports
    require('foo');
});

this could be backward compatible since it does not add any param to factory function, but also work with loaders with "lazy evaluate factory" behavior.

Activity

  1. babel-bot commented on Jul 27, 2016

    @babel-bot
    CollaboratorAuthor

    Comment originally made by zhangli (yibuyisheng)

    up! encountered the same problem!

  2. babel-bot commented on Jul 31, 2016

    @babel-bot
    CollaboratorAuthor

    Comment originally made by @Justineo

    I've made a PR to resolve this issue: #3620

  3. added a commit that references this issue on Sep 13, 2017
    cc0d412
  4. mlunoe commented on Nov 21, 2017

    @mlunoe

    Is there a workaround for this issue until the fix lands in a stable version?

  5. nicolo-ribaudo commented on Nov 21, 2017

    @nicolo-ribaudo
    Member

    I haven't tried it, but I think import * as __unused__ from "foo"; could work.

  6. added
    outdated
    on May 4, 2018
  7. locked as resolved and limited conversation to collaborators on May 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdated

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions