巴别塔“进口”到“需要”,但“要求在ecma5中是不可用的 [英] Babel transpiles 'import' to 'require', but 'require isn't useable in ecma5

查看:161
本文介绍了巴别塔“进口”到“需要”,但“要求在ecma5中是不可用的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,使用Babel可以让您在ecma5环境中使用ecma6 javascript进行泛化。然而,当我使用'import'时,'import'被传递到'require'。 'require'不是ecma5,需要库'require.js'。因此,您无法使用import / export而不附加依赖关系,是否正确?

It was my understanding that use Babel allows you to use ecma6 javascript in an ecma5 environment by transpiling. When I use 'import' however, the 'import' is transpilied to 'require'. 'require' isn't ecma5 and requires the library 'require.js'. Therefore you can't use 'import/ export' without additional dependencies, is this correct?

推荐答案

是的,用于翻译新语言功能以兼容现代JavaScript引擎。 Babel不编译为require.js模块导入语法。而是使用Node.js使用的CommonJS模块语法。因此,您可以直接运行代码,而不需要在Node.js中进一步构建依赖关系。

Yes, Babel is just intended for translating new language features to be compatible with modern javascript engines. Babel doesn't compile to require.js module import syntax. Rather it uses the CommonJS module syntax as used by Node.js. So you could run the code directly without further build dependencies in Node.js.

由于它一次运行在单个文件上,而是一个简单的向前翻译,做出任何关于你想如何将这些其他文件的源代码包含在当前的决定。

As it operates on single files at a time and is a straight forward translation, it doesn't make any decisions as to how you want to include the source code of those other files into the current one.

这就是说,如果你要在浏览器中使用它,您将需要一个支持CommonJS模块语句的构建系统或捆绑器:

That said, if you are going to use it in browser, you will need a build system or bundler that supports CommonJS modules statements:


  • 请参阅 https://babeljs.io/docs/setup/#installation 列出许多典型的构建配置

  • Browserify和Webpack是Javacript生态系统中最受欢迎的两个。

  • 这些系统通过引用require引用的文件来捆绑您的JavaScript代码,因此通常会产生一个输出js您可以在ecma5中运行的文件

  • See https://babeljs.io/docs/setup/#installation for a list of many typical build configurations
  • Browserify and Webpack are two of the most popular ones in the Javacript ecosystem
  • These systems 'bundle' your javascript code by injecting files wherever 'require' is referenced and thus typically produce one output js file which you can run in ecma5

这篇关于巴别塔“进口”到“需要”,但“要求在ecma5中是不可用的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆