2015-09-05 15 views
2

我正忙着使用ASP.NET 5並使用gulp。我在我的package.json文件中添加了angularjs和angular-route,它們將文件存儲在Dependencies-> NPM中。我將它添加到我的gulpfile.js文件中,認爲它會複製正確的JS文件。它確實複製了這些文件,但它也使該項目崩潰。我不得不手動進入lib文件夾並刪除所有添加的gulp。從NPM文件夾複製目標文件夾中的文件的正確方法是什麼?我希望能夠從Task Runner中運行任務。將文件複製到目的地的合適方式使用gulp

我假設這是不正確的:(這是我跑)

gulp.task("copyJs", function() { 
    return gulp.src('./node_modules/**/*.js')   
     .pipe(gulp.dest('./wwwroot/lib/')) 
}); 

回答

0

有很多方法可以做到這一點,但良好簡單的方法之一,我發現是這樣的:http://www.hanselman.com/blog/ControlHowYourBowerPackagesAreInstalledWithAGulpfileInASPNET5.aspx

哪些更新bowerrc文件以及此更新後的所有內容更有意義。

更新.BOWERRC和PROJECT.JSON

在項目的根是一個.bowerrc文件。它看起來像這樣:

> { "directory": "wwwroot/lib" } Change it to something like this, and 
> delete your actual wwwroot/lib folder. 
> 
> { "directory": "bower_components" } EXCLUDE YOUR SOURCE BOWER FOLDER 
> FROM YOUR PROJECT.JSON 

您也想進入你的project.json文件ASP.NET 5和 確保您的源文件夾bower_components從 項目及排除任何包裝和出版過程。

> "exclude": [ 
>  "wwwroot", 
>  "node_modules", 
>  "bower_components" ], 

更新GULPFILE.JS

在你gulpfile,確保路徑中存在的路徑。有 完全其他方式來做到這一點,包括有吞嚥安裝涼亭和 找出路徑。只要結果是生產準備就緒.js 結束在您的wwwroot準備好提供給客戶,這取決於您多麼複雜,只要您希望 gulp文件得到。此外, 還包含一個lib或目標,您的結果JavaScript將被複制到 。可以是腳本,可以是js,可以像我一樣處於lib狀態。

變種路徑= { 根目錄: 「./」 + project.webroot + 「/」, 亭子: 「./bower_components/」, LIB: 「./」 + project.webroot +「/ LIB/「};添加一個複製任務到你的領域

現在打開你的Gulpfile並記下所有任務。您將添加一個 複製任務,以僅複製想要與您的 網絡應用程序部署的文件。

下面是一個例子複製任務:

> gulp.task("copy", ["clean"], function() { 
>  var bower = { 
>   "bootstrap": "bootstrap/dist/**/*.{js,map,css,ttf,svg,woff,eot}", 
>   "bootstrap-touch-carousel": "bootstrap-touch-carousel/dist/**/*.{js,css}", 
>   "hammer.js": "hammer.js/hammer*.{js,map}", 
>   "jquery": "jquery/jquery*.{js,map}", 
>   "jquery-validation": "jquery-validation/jquery.validate.js", 
>   "jquery-validation-unobtrusive": "jquery-validation-unobtrusive/jquery.validate.unobtrusive.js" 
>  } 
> 
>  for (var destinationDir in bower) { 
>   gulp.src(paths.bower + bower[destinationDir]) 
>   .pipe(gulp.dest(paths.lib + destinationDir)); 
>  } }); 

請注意,這是一個很簡單,很明確的複印任務。其他 可能只是複製或多或少,甚至使用通配符通配符。 這是給你的。問題的關鍵是,如果你沒有在ASP.NET 5或在你的web應用程序的常規構建流動喜歡一個行爲,你有更多的 權力比以往任何時候。

右鍵單擊Solution Explorer中的鮑爾節點和「恢復 包。」你也可以這樣做在命令行,還是讓它 在構建時發生。

查看這個簡化的屏幕截圖,您可以看到〜/ bower_components文件夾下的依賴關係的涼亭 。當 gulpfile運行復制任務只是 我想要的部分被移動到〜/ wwwroot的/ lib目錄/文件夾**。

3

*我認爲,在gulp.dest('./wwwroot/lib/')'/'可能是問題的原因,請嘗試gulp.dest('./wwwroot/lib')代替。

這是一口流程我對角2使用與Asp.Net 5.

var gulp = require("gulp"), 
    merge = require("merge-stream"), 
    rimraf = require("rimraf"); 

var paths = { 
    webroot: "./wwwroot/", 
    node_modules: "./node_modules/" 
}; 

paths.libDest = paths.webroot + "lib/"; 

gulp.task("clean:libs", function (cb) { 
    rimraf(paths.libDest, cb); 
}); 

gulp.task("copy:libs", ["clean:libs"], function() { 
    var angular2 = gulp.src(paths.node_modules + "angular2/bundles/**/*.js") 
     .pipe(gulp.dest(paths.libDest + "angular2")); 

    var es6_shim = gulp.src([ 
      paths.node_modules + "es6-shim/*.js", 
      "!**/Gruntfile.js"]) 
     .pipe(gulp.dest(paths.libDest + "es6-shim")); 

    var systemjs = gulp.src(paths.node_modules + "systemjs/dist/*.js") 
     .pipe(gulp.dest(paths.libDest + "systemjs")); 

    var rxjs = gulp.src(paths.node_modules + "rxjs/bundles/**/*.js") 
     .pipe(gulp.dest(paths.libDest + "rxjs")); 

    return merge(angular2, es6_shim, systemjs, rxjs); 
}); 
相關問題