You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
side effect only import statement should be a param for AMD's factory function (T7382) #3802
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
babel-bot commentedon Jul 27, 2016
up! encountered the same problem!
babel-bot commentedon Jul 31, 2016
I've made a PR to resolve this issue: #3620
Ensure the AMD/UMD loaders all have params for each import to avoid l…
mlunoe commentedon Nov 21, 2017
Is there a workaround for this issue until the fix lands in a stable version?
nicolo-ribaudo commentedon Nov 21, 2017
I haven't tried it, but I think
import * as __unused__ from "foo";could work.