【问题标题】:Refactor RequireJS modules (AMD) to Webpack modules (CommonJS)将 RequireJS 模块 (AMD) 重构为 Webpack 模块 (CommonJS)
【发布时间】:2017-09-28 04:01:59
【问题描述】:

我需要重构一堆像这样调用的文件

define(['module1','module2','module3' etc...], function(a, b, c etc...) {

   //bunch of code

   return Something;

});

var a = require('module1');
var b = require('module2');
var c = require('module3');

//bunch of code

module.exports = Something;

看起来是一个非常简单的任务,99% 的代码都遵循这种模式,但我就是不能使用正则表达式,因为我无法用它来平衡括号。

我已经弄清楚了文件遍历,在一项繁重的任务中,我只需要转换文件并将其写回。

grunt.registerTask('refactor', '', function() {
    //traverse all files   
    grunt.file.recurse('./src/js/views', function callback(abspath, rootdir, subdir, filename) {
        var c;
        //only care about .js files
        if(filename.match(/\.js$/)){
            console.log(subdir, filename);
            c = grunt.file.read(abspath); //read file

            //modify it
            c = c + "\n/* Comment appended! */";  

            grunt.file.write(abspath, c); //write it back
        }

    });

    grunt.log.write("DONE").ok();
});

这是一个一次性的工作,所以我正在寻找一个快速n-dirty的解决方案,无论如何我必须手动检查所有文件,我只是想节省一些时间。

【问题讨论】:

    标签: javascript webpack gruntjs requirejs refactoring


    【解决方案1】:

    正则表达式不是这项工作的工具。

    你应该使用某种 js 解析器,其中有很多。 有与修改代码相关的短语,code-mod & code-shift

    看看that tool

    【讨论】:

      猜你喜欢
      • 2017-04-24
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      • 2015-04-22
      • 2017-10-27
      • 2012-12-18
      • 2021-06-26
      • 2012-05-28
      相关资源
      最近更新 更多