thgreasi / babel-plugin-system-import-transformer

import() & System.import() to UMD pattern transformer plugin for Babel
MIT License
72 stars 6 forks source link

When compiling AMD modules and the moduleIds flag is set, the wrong moduleId is inserted #19

Open andrewnicols opened 1 year ago

andrewnicols commented 1 year ago

If the moduleIds babel configuration is configured, then the wrong file-level module name will be used whenever a dynamic import is transformed using this plugin.

For example, the following:

// index.js
export default () => {
    import('./example').then((example) => console.log(example()));
};

Should generate a module:

// build/index.js
define("index", ["exports"], function(_exports) {

    // ...
    _systemImportTransformerGlobalIdentifier.require(["example"], resolve, reject);

});

Unfortunately the imported module name is hoisted to the top of the file and the wrong moduleId is inserted into the built file:

// build/index.js
define("example", ["exports"], function(_exports) {
//      ^^^^^^^ <== Should be "index"
    // ...
    _systemImportTransformerGlobalIdentifier.require(["example"], resolve, reject);

});
andrewnicols commented 1 year ago

Reproduction repository here: https://github.com/andrewnicols/bugreport-systemimport-19

andrewnicols commented 1 year ago

And a unit test fixture demonstrating the issue here: https://github.com/andrewnicols/babel-plugin-system-import-transformer/commit/bugreport19