简体   繁体   English

babel是否提供将es6 import语句转换为AMD“ require”调用的方法

[英]does babel provide a method of converting es6 import statements into AMD 'require' calls

The Babel AMD module formatter works perfectly for modules, wrapping the script in a define statement. Babel AMD模块格式化程序非常适合于模块,可以将脚本包装在define语句中。 However, my project also has 'page scripts' that should execute as soon as dependencies are loaded. 但是,我的项目还具有“页面脚本”,应在加载依赖项后立即执行。 These should be wrapped in a require statement. 这些应该包装在require语句中。

Does Babel provide a means of accomplishing this? Babel是否提供实现此目的的方法?

I don't see a way to get Babel to generate something like this: 我没有办法让Babel生成这样的东西:

require(['foo'], function (foo) {
   ... 
});

All the examples for AMD in the Babel documentation show Babel generating define(... . Moreover, there does not seem to be any clear way to indicate in ES6 that what should be generated by Babel is a require call instead of a define call. So the directive to Babel to output require would have to be extra-linguistic. Babel文档中有关AMD的所有示例都显示了Babel生成define(... 。此外,在ES6中似乎没有任何明确的方法表明Babel 应该生成的是require调用而不是define调用。因此,通向Babel的输出require的指令必须是额外的语言。

I've taken a look at the source for the generation of AMD modules. 我看了一代AMD模块的来源 I do not see any logic there that would cause Babel to output a require call, ever. 我在那里看不到任何逻辑会导致Babel输出一个require调用。

If I decided I needed a module translator that allows me to do this, I'd derive from the class in this module a new class that would allow specifying that in some cases, a require call should be produced rather than define . 如果我确定需要一个允许我执行此操作的模块转换器,则可以从该模块中的类派生一个新类,该类可以指定在某些情况下应生成require调用而不是define I don't know Babel well enough to know whether it would be possible to prevent generating invalid creations (eg code in a require call that tries to export anything). 我对Babel不太了解,无法知道是否可以防止生成无效的创建内容(例如,在require调用中尝试export任何内容的代码)。

Otherwise, you can always call require like you used to. 否则,您可以像过去一样随时调用require

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM