有没有办法将文件自动复制到wwwroot?

我有我的index.htmlwwwroot目录中,它正在从locahost:5001浏览器访问。 当我们使用NPM安装了一些软件包时,目录node_modules被放置在与wwwroot相同的级别上。

当我链接到文件时,我使用这样的相对path。

HREF = “../ node_modules / package_this_or_that / package.min.js”

在我看来,一个更好的方法是将那些交付到wwwroot目录,并让他们驻留在那里。 不是所有的包内容,只是实际使用的文件(跳过自述文件等)。

有包吗? 还是需要使用构build脚本来完成?

您不应该从您的html或cshtml文件访问前端的node_modules文件。 所以你是对的,你应该将它们复制到wwwroot文件夹。

您可以使用grunt作为Tseng评论中的链接,但我个人更喜欢Gulp ,我认为使用起来更快更简单。

你的package.json文件:

 { "version": "1.0.0", "name": "asp.net", "private": true, "devDependencies": { "gulp": "3.9.1", "gulp-cached": "1.1.0", } } 

然后在你的项目的根级创build一个gulpfile.js ,你可以写类似的东西

 var gulp = require('gulp'), cache = require('gulp-cached'); //If cached version identical to current file then it doesn't pass it downstream so this file won't be copied gulp.task('default', 'copy-node_modules'); gulp.task('copy-node_modules', function () { try { gulp.src('node_modules/**') .pipe(cache('node_modules')) .pipe(gulp.dest('wwwroot/node_modules')); } catch (e) { return -1; } return 0; }); 

最后打开Task Runner Explorer (如果使用的是Visual Studio),执行default任务或者直接执行copy-node_modules任务。

Gulp是非常有用的,我build议你探索其他不同的吞咽任务。 您可以连接并缩小CSS和JS文件,删除注释,甚至可以创build一个watch任务,在文件更改后立即执行其他任务。