我不知道为什么这还没有提到,但我确实认为这个线程有点过时。由于我在搜索过程中偶然发现了这个问题,所以我想我会在这里为其他新手JS人员找一个关于GruntJS的快速报告。
本质上,正确配置的Gruntfile.js将能够执行围绕JS的各种任务,包括但不限于:连接文件,缩小文件,代码线索等等。
您可以安装在Ubuntu grunt
:
$ sudo apt-get install nodejs
$ sudo npm -g install grunt-cli
$ cd /path/to/my/project
--- Assumming you have a package.json file already in place ---
$ npm install grunt --save-dev
--- Install grunt plugins you wish to use ---
$ npm install grunt-contrib-concat --save-dev
$ npm install grunt-contrib-uglify --save-dev
$ npm install grunt-contrib-jshint --save-dev
$ npm install grunt-contrib-watch --save-dev
在GruntJS site,有一个相当不错的写了如何使用GruntJS,但这里有一个例子Gruntfile.js将:
- Lint所有的JS文件(当前目录中的
app.js
和ngmodules
目录中的所有.js
文件)。
- 将文件连接并保存到
dist/package-name.js
。
- 缩小连接文件并将其保存到
dist/package-name.min.js
。
Gruntfile.js
:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
options: {
separator: ';'
},
dist: {
src: ['app.js', 'ngmodules/**/*.js'],
dest: 'dist/<%= pkg.name %>.js'
}
},
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'
},
dist: {
files: {
'dist/<%= pkg.name %>.min.js': ['<%= concat.dist.dest %>']
}
}
},
jshint: {
files: ['Gruntfile.js', 'app.js', 'ngmodules/**/*.js'],
options: {
// options here to override JSHint defaults
globals: {
jQuery: true,
console: true,
module: true,
document: true
}
}
},
watch: {
files: ['<%= jshint.files %>'],
tasks: ['jshint']
}
});
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.registerTask('default', ['jshint', 'concat', 'uglify']);
};
除非您的服务器支持这样的事情,它不会工作。这不是浏览器问题,应该为您正在使用的服务器技术添加标签以查看是否有解决方案。 – lucuma 2013-04-11 14:42:05