2016-08-20 30 views
4

我有以下一飲而盡任務:'... paths.js'是什麼意思在'gulp.src([... paths.js,'!gulpfile.babel.js'],{base:'。'})'?

// Compile ES6 to ES5 and copy to dist 
 
gulp.task('babel',() => 
 
    gulp.src([...paths.js, '!gulpfile.babel.js'], { base: '.' }) 
 
    .pipe(plugins.newer('dist')) 
 
    .pipe(plugins.sourcemaps.init()) 
 
    .pipe(plugins.babel()) 
 
    .pipe(plugins.sourcemaps.write('.', { 
 
     includeContent: false, 
 
     sourceRoot(file) { 
 
     return path.relative(file.path, __dirname); 
 
     } 
 
    })) 
 
    .pipe(gulp.dest('dist')) 
 
);

按照咕嘟咕嘟文件(gulp.src)我瞭解到,gulp.src 發出文件匹配的glob提供或水珠的陣列。
但我無法理解這裏'... paths.js'的含義。 項目目錄中沒有以'paths.js'命名的文件。

有沒有人可以幫助我理解它?

+0

我知道意思......但我只是對Gulp任務中的paths.js感興趣。 –

+0

我認爲這是一個文件。 –

回答

5

...在這種情況下是ES2015(又名「ES6」)spread syntax:它將可迭代的內容(如數組)分散到數組中的離散元素中。

實施例:

let a = [1, 2, 3]; 
 
let b = [...a, 4, 5]; 
 
console.log(b); // 1, 2, 3, 4, 5

所以

gulp.src([...paths.js, '!gulpfile.babel.js'], { base: '.' }) 

...被創建具有的paths.js隨後'!gulpfile.babel.js'內容的新的數組,並通過該陣列分成src。我假設paths.js是一個數組;如果是的話,在這個特殊的情況下,它可以與concat取代:

gulp.src(paths.js.concat('!gulpfile.babel.js'), { base: '.' }) 

您也可以使用傳播語法在函數調用:

function testing(a, b, c) { 
 
    console.log("a = " + a); 
 
    console.log("b = " + b); 
 
    console.log("c = " + c); 
 
} 
 
let x = [1, 2, 3]; 
 
testing(...x); // Shows: 
 
       // a = 1 
 
       // b = 2 
 
       // c = 3

+0

你能告訴我在這裏做什麼path.js? –

+0

@ColinWitkamp:'paths.js'大概是在你的gulp文件中的某處定義的,很可能是作爲JavaScript源文件路徑的一個數組。 –

+0

啊,我明白了!我發現了以下代碼片段:const paths = {'./**/*.js','!dist/**','!node_modules/**','!coverage/**'], nonJs:['./package.json','./.gitignore'], tests:'./server/tests/*.js' }; –