Is it possible to convert requirejs modules to commonjs?

后端 未结 3 1093
情书的邮戳
情书的邮戳 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:07

    Manually, it should be possible.

    A require.js module looks like:

    define('module', ['dep1', 'dep2'], function(dep1, dep2) {
       return myFunction = function() {
       };
    
    });
    

    Exporting to a CommonJs module shouldn't be too hard:

    var dep1 = require('dep1');
    var dep2 = require('dep1');
    
    exports.myFunction = function() {
    };
    

    You don't have to return a function, it can be an object too:

    define('module', ['dep1', 'dep2'], function(dep1, dep2) {
       return {
         myFunction1: function() {
         },
         myFunction2: function() {
         }
       };
    
    });
    

    ...

    var dep1 = require('dep1');
    var dep2 = require('dep1');
    
    exports.myObject = {
         myFunction1: function() {
         },
         myFunction2: function() {
         }
    };
    

    Edit: converting is also possible.

提交回复
热议问题