Is it possible to convert requirejs modules to commonjs?

后端 未结 3 1150
情书的邮戳
情书的邮戳 2021-02-15 22:40

It\'s already possible to convert commonjs modules to requirejs, but I still want to know whether it\'s possible to do the reverse. Is there any way to convert requireJS modules

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-15 23:04

    Yes its possible if you use uRequire, a tool I wrote specifically for javascript module format conversion - AMD/Requirejs --> commonjs/nodejs and back (plus more).

    It seems as a trivial AST manipulation & dependencies resolution @ start, but module formats are incompatible in many ways apart from syntax:

    • dependency/path resolution : commonjs uses fileRelative (eg '../PersonModel') whereas AMD can also use package-relative (eg 'Models/PersonModel')

    • execution environment: commonJs modules are loaded synchronously, whereas AMD are loaded asynchronously (AMD also has the asynchronous require version require(['dep1', dep2'], function(dep1, dep2){}) as its targeted mainly for the browser).

    These are also other differences (eg AMD loader plugins) but in general writing against each one 'locks' you in that format. .

    uRequire enables you to author in AMD or commonJS and convert to the other easily (or convert to UMD), hence enjoying modular Javascript without the boilerplate.

提交回复
热议问题