thgreasi / babel-plugin-system-import-transformer

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

Cannot transform when module name was a variant rather than literal #4

Closed shaunxu closed 8 years ago

shaunxu commented 8 years ago

This works.

System.import('./my-module').then((module) => {
    console.log(module);
}).catch((error) => {
    console.log(`Failed: ${error}`);
});

But if I put my module path into a variant it doesn't work.

const moduleName = './my-module';
System.import(moduleName).then((module) => {
    console.log(module);
}).catch((error) => {
    console.log(`Failed: ${error}`);
});

Seems that this plugin only transform System.import when the argument is literal. But I think in most cases, we use module loader when the module name was defined in a config file, or loop files in a folder.

thgreasi commented 8 years ago

This can't be done since in CommonJS require('module') can't accept a variant as a parameter. In more details, CommonJS needs string literals as argument to the require() method, so that it can statically analyze the source and generate bundle with all the required modules.

thgreasi commented 8 years ago

What module type are you targeting?

shaunxu commented 8 years ago

Yes I was using 'common'.

thgreasi commented 8 years ago

I this case (as said before) this can't be done since it is a limitation of

the way that CommonJS bundlers work.

Thodoris Greasidis Computer, Networking & Communications Engineer