簡體   English   中英

將RequireJS模塊(AMD)重構為Webpack模塊(CommonJS)

[英]Refactor RequireJS modules (AMD) to Webpack modules (CommonJS)

我需要重構一堆這樣的文件

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();
});

這是一項一次性的工作,所以我正在尋找一種快速解決問題的方法,無論如何我以后都必須手動檢查所有文件,我只想節省一些時間。

正則表達式不是工作的工具。

您應該使用某種js解析器,其中有很多。 有些短語與修改代碼, code-modcode-shift

看一下那個工具

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM