2017-03-05 62 views
0

也許有人比我有更多的Java腳本經驗可以回答這個問題。到目前爲止,我已經在'usemin'塊中複製了&粘貼,如課程中所示。這裏是代碼:我怎樣才能更有效地編碼? - 一個吞嚥任務

gulp.task('useminTrigger', ['deleteDistFolder'], function() { 
    gulp.start("usemin", "usemin-de"); 
}); 

gulp.task('usemin', ['styles', 'scripts'], function() { 
    return gulp.src("./app/index.html") 
    .pipe(usemin({ 
     css: [function() {return rev()}, function() {return cssnano()}], 
     js: [function() {return rev()}, function() {return uglify()}] 
    })) 
    .pipe(gulp.dest("./dist")); 
}); 

gulp.task('usemin-de', ['styles', 'scripts'], function() { 
    return gulp.src("./app/de/index.html") 
    .pipe(usemin({ 
     css: [function() {return rev()}, function() {return cssnano()}], 
     js: [function() {return rev()}, function() {return uglify()}] 
    })) 
    .pipe(gulp.dest("./dist/de")); 
}); 

該腳本的工作原理應該是這樣,但也許有一個更容易或更優雅的方式來編碼。

並以優雅的我的意思是:有沒有辦法來合併usemin - 阻塞與usemin德在一起嗎?

幫助將非常感激。提前致謝!

+0

歡迎去吧!很難說出你的問題是什麼。你想要一個簡明的方式在'.app/index.html'(輸出到'。/ dist')和'。/ app/de/index.html'(輸出到'./dist/de')? – henry

+0

你好亨利,謝謝你的回覆。我想這是一個菜鳥問題...... :)我想我實際上並不瞭解我寫的所有代碼,需要一些幫助,看看它應該如何正確告訴我想要的東西。我已經編輯了我的問題,因爲你會看到更清楚。希望這會有所幫助。再次感謝。 – georg

回答

0

如果您使用您gulp.src(也就是說,如果你使用通配符來選擇您想要的gulp.task處理的文件),源文件結構將被保留,所有的方式通過globgulp.dest

爲了配合剛app/index.htmlapp/de/index.html你可以使用水珠'./app/{,de}/index.html'(它也將努力使用'./app/{,de/}index.html''./app/{.,de}/index.html')。

那麼你的任務是

gulp.task('usemin', ['styles', 'scripts'], function() { 
    return gulp.src('./app/{,de}/index.html') 
    .pipe(usemin({ 
     css: [function() {return rev()}, function() {return cssnano()}], 
     js: [function() {return rev()}, function() {return uglify()}] 
    })) 
    .pipe(gulp.dest("./dist")); 
}); 

這一個任務將採取./app/index.html./app/de/index.html的源文件和將輸出文件./dist/index.html./dist/de/index.html

請注意,使用glob是必要的 - 如果你只是做了gulp.src(['./app/index.html','./app/de/index.html'])相關的文件結構將不會被保留。 ./app/index.html的處理版本將被寫入./dest/index.html,然後處理後的版本./app/de/index.html將被寫入./dest/index.html(覆蓋第一個輸出文件)。

這是一個很好的glob primer,這裏是一個tester learning tool

+0

謝謝henry,一個非常緊湊的解決方案。我已經閱讀了一些關於這個話題(你的鏈接)。我只使用regexp來搜索文件 - 一種新的體驗來使用它來爲我編寫代碼。我會嘗試。 – georg

0

中沒有任何的Javascript咕嘟咕嘟讓你從提取功能:

您的任務

gulp.task('useminTrigger', ['deleteDistFolder'], function() { 
    gulp.start("usemin", "usemin-de"); 
}); 

gulp.task('usemin', ['styles', 'scripts'], function() { 

    return createMinificationPipe("./app/index.html", "./dist"); 
}); 

gulp.task('usemin-de', ['styles', 'scripts'], function() { 
    return createMinificationPipe("./app/de/index.html", "./dist/de"); 
}); 

常用功能

function createMinificationPipe(src, dest){ 
    return gulp.src(src) 
     .pipe(usemin({ 
      css: [function() {return rev()}, function() {return cssnano()}], 
      js: [function() {return rev()}, function() {return uglify()}] 
     })) 
     .pipe(gulp.dest(dest)); 
} 
+0

感謝你的努力和這個偉大的分裂,這是我無法做到的令人興奮的事情。我還沒有嘗試過,但我會更新,如果我有。 – georg