Babel transpiles 'import' to 'require', but 'require isn't useable in ecma5

…衆ロ難τιáo~ 提交于 2019-12-04 02:02:13

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.

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

  • 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

Try to replace require in transpilied code by a correspondent method. Ex: For me, replace require('react') by window.react will work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!