我使用 ASP.Net 5 任务运行器解决了这个问题。
我的解决方案具有以下文件夹层次结构。
Solution
->wwwroot
->Scripts
->app.js
->Controllers
->controller1.js
->controller2.js
1.) 文件监视程序监视 Scripts 目录和所有子文件夹。
2.) 当检测到文件更改时,所有 *.js 文件都会从脚本文件夹复制到我的脚本文件夹中的 wwwroot 目录。
3.) 然后在 wwwroot 目录中的脚本文件夹上运行 uglify 任务,这会将其缩小为单个 app.js 文件,供 html 应用程序引用。
uglify 任务将 sourceMap 选项转为 turn,这允许 javascript 调试器在调试模式下引用脚本,但在非调试模式下运行时使用缩小的脚本。
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.initConfig({
copy: {
main: {
files: [
{
expand: true,
src: ['Scripts/app.js', 'Scripts/Controllers/**'],
dest: 'wwwroot'
}
]
}
},
uglify: {
my_target: {
files: { 'wwwroot/app.js': ['wwwroot/Scripts/app.js', 'wwwroot/Scripts/**/*.js'] },
options : {sourceMap : true}
}
},
watch: {
scripts: {
files: ['Scripts/app.js','Scripts/**/*.js'],
tasks: ['copy:main', 'uglify']
}
}
});
grunt.registerTask('default', ['copy:main', 'uglify', 'watch:scripts']);
};