我并不专业地使用 grunt,但我自己使用它,我想我可以向您解释如何使用它来满足您的要求。
首先,您应该将“New Item”添加到您的项目中,选择“Client-Side”和“NPM Configuration file”来添加package.json
到项目(在您所在的同一目录中)project.json
)。我想您已经创建了该文件,但该文件的存在对于 grunt 也很重要。然后添加一些客户端需要的依赖项"dependencies"
部分package.json
并至少添加grunt
and grunt-contrib-copy
to "devDependencies"
部分。您将在下面看到的文件示例
{
"version": "1.0.0",
"name": "ASP.NET",
"private": true,
"dependencies": {
"font-awesome": "^4.5.0",
"jquery": "^1.11.3"
},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-clean": "^0.7.0",
"grunt-contrib-copy": "^0.8.2"
}
}
现在您应该以与添加“NPM 配置文件”相同的方式添加“Grunt 配置文件”。你将创造gruntfile.js
(在您所在的同一目录中project.json
)。最后你应该填写gruntfile.js
有更多有用的代码。例如代码
module.exports = function(grunt) {
grunt.initConfig({
clean: ["wwwroot/font-awesome/", "wwwroot/jquery*.*"],
copy: {
main: {
files: [
{
src: "node_modules/font-awesome/css/*",
dest: "wwwroot/font-awesome/css/",
expand: true,
filter: "isFile",
flatten: true
},
{
src: "node_modules/font-awesome/fonts/*",
dest: "wwwroot/font-awesome/fonts/",
expand: true,
filter: "isFile",
flatten: true
},
{
src: "node_modules/jquery/dist/*",
dest: "wwwroot/",
expand: true,
filter: "isFile",
flatten: true
}
]
}
}
});
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-contrib-copy");
grunt.registerTask("all", ["clean", "copy"]);
grunt.registerTask("default", "all");
};
注册两个任务:clean
and copy
和别名all
and default
。您可以选择gruntfile.js
在解决方案资源管理器中打开文件,打开上下文菜单并选择“任务运行程序资源管理器”。您将看到所有已定义的任务。如果执行,将执行名称为“default”的任务grunt
命令行中不带参数(不带任务名称)。
现在您可以选择某个任务并运行它。您可以选择某个任务,单击鼠标右键打开上下文菜单并选中“Bindings”中的“After Build”:
现在每次构建项目时都会执行该任务。您可以选择单击左侧的“V”按钮来查看已执行任务的详细信息。
我希望这已经是您需要的主要信息。有关插件的许多其他有用信息grunt-contrib-clean
, grunt-contrib-copy
, grunt-contrib-jshint
, grunt-jscs
, grunt-newer
还有许多其他的,你会发现自己。值得一提的是 ASP.NET 5 文档的一个官方位置。它是这个地方 http://docs.asp.net/en/latest/client-side/using-grunt.html.
附:您还询问了凉亭的用法。我发现 npm 和 Bower 都不是完美的,但仍然很实用。我更愿意完全控制依赖关系,尤其是数据,这些数据将复制到wwwroot
。因此我改变了内容.bowerrc
文件来自{ "directory": "wwwroot/lib" }
to { "directory": "bower_components" }
我使用 grunt 从中复制所需的数据bower_components
就像我对来自的文件执行此操作一样node_modules
. See 文章 http://www.hanselman.com/blog/ControlHowYourBowerPackagesAreInstalledWithAGulpfileInASPNET5.aspx更多细节。换句话说,我使用仅在 Bower 存储库中发布的包,就像使用 npm 包一样。