2016-10-12 2 views
0

재귀 적 디렉토리에 포함되지만 현재 디렉토리 내 파일이 다른 포함 된 디렉토리에 파고 전에, 전을 추가 할 수 있음을 보장하는 자바 스크립트 파일 CONCAT 필요 here에서 제시 한 해결책은 재귀 적이 함께 파일을 얻을 수있었습니다 :그런트의 CONCAT 디렉토리 재귀 파일

// get all module directories 
    grunt.file.expand("src/*").forEach(function (dir) { 
     // get the module name from the directory name 
     var dirName = dir.substr(dir.lastIndexOf('/') + 1); 

     // create a subtask for each module, find all src files 
     // and combine into a single js file per module 
     concat.main.src.push(dir + '/**/*.js'); 
    }); 

    console.log(concat.main.src); 

을하지만 난 concat.main.src에 검사 할 때 다음 순서는 내가 필요로하는 방식이되지 않습니다 :

//console.log output 
['src/module1/**/*.js', 
'src/module2/**/*.js', 
'src/main.js'] 

내가 필요로하는 순서는 다음과 같습니다

내가 이것을 달성 할 수있는 방법에 대한 아이디어
['src/main.js', 
'src/module1/**/*.js', 
'src/module2/**/*.js'] 

?

답변

0

덕분에 많은 당신의 모듈을 추가 할 것입니다!

샤워 후 Grunts 설명서를 자세히 읽으면서 머리가 깨끗 해지면 다른 사람들에게 관심을 가질만한 해결책을 찾았습니다. 그것은 grunt.file.expand 대신 grunt.file.recurse을 사용하고 있습니다.

var sourceSet = []; 
grunt.file.recurse("src/main/", function callback(abspath, rootdir, subdir, filename) { 
    sourceSet.push(abspath); 
}); 

concat.main.src = concat.main.src.concat(sourceSet.sort()); 

이제 완벽하게 작동합니다!

1

알파벳순으로 처리되므로 main.js의 이름을 app.js로 바꾸는 것이 가장 빠릅니다.

하지만 main.js를 유지하려면 먼저 배열로 밀어 넣으십시오. 당신이 파일의 글로브를 변경할 경우, SRC의 서브 디렉토리를 찾기 위해 확장 다음은 main.js를 건너 뛰고 배열

concat.main.src.push('src/main.js'); 
// get all module directories 
grunt.file.expand("src/**").forEach(function (dir) { 
    // get the module name from the directory name 
    var dirName = dir.substr(dir.lastIndexOf('/') + 1); 

    // create a subtask for each module, find all src files 
    // and combine into a single js file per module 
    concat.main.src.push(dir + '/**/*.js'); 
}); 

console.log(concat.main.src); 
+0

main.js는 단지 예일뿐입니다. 사실 루트 디렉토리에는 n 개의 파일이 존재할 수 있으며 레벨이 n 일 수도있는 동일한 동작을 보장하는 솔루션을 얻고 싶습니다. 위의 내용은 내가 필요한 것을 설명하기위한 것이지만, 이것이 적절한 해결책이라고 생각하지 않습니다. 어쨌든 많은 감사합니다 !!! – YadirHB