我在gif動畫中爲你錄製了一個小屏幕錄像。在那裏我展示瞭如何通過npm安裝依賴模塊,並在啓用livereload的情況下構建項目。
截屏位於http://imgur.com/3CpVI2j
我的版本的工具是 NPM 1.4.21 和 的NodeJS v0.10.30
這是我Gruntfile.js與評論
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// Delete contents before the full build
clean: {
dist: ['dist', 'dist/js'],
html: ['dist/**/*.html']
},
/* Uglify javascript located in app/js/*.js
And copy it to dist/js/main.js
So all JS will be in one file.
P.S. Remember that our js task in uglify is called "js" */
uglify: {
options: {
report: 'min'
},
js: { // task to combine separate files into main.js
files: {
'dist/js/main.js': [
'app/js/**/*.js'
]
}
}
},
copy: { // Just copy HTML files from app folder
html: {
files: [{
expand: true,
cwd: 'app/',
src: ['**/*.html'],
dest: 'dist/',
filter: 'isFile'
}]
},
},
connect: { // Create server that will serve
// requests to our compiled app
dist: {
options: {
port: 3000,
base: 'dist'
}
}
},
watch: { // Most instersting.
// Will be watching for changes in JS and HTML.
options: {
livereload: true
},
js: { // Here you need to specify the same task name
// as in uglify task we thought above
files: ['app/js/**/*.js'], // Which files to uglify and copy
tasks: ['newer:uglify'] // Do it
},
copy: { // Remove old html and copy new to dist folder
files: ['app/**/*.html'],
tasks: ['clean:html','newer:copy:html']
}
},
});
// Load all grunt modules with one command
require('load-grunt-tasks')(grunt);
grunt.registerTask('build', [
'clean:dist', // Remove all
'uglify', // Uglify all
'copy' // Copy all HTML
]);
grunt.registerTask('default', [
'build', // Build the project
'connect:dist', // Start server
'watch' // Watch for changes
]);
};
這是的package.json沒有評論 (一切都足夠清晰)
{
"engines": {
"node": ">= 0.10.0"
},
"devDependencies": {
"grunt": "~0.4.5",
"grunt-contrib-clean": "0.5.0",
"grunt-contrib-connect": "~0.8.0",
"grunt-contrib-copy": "~0.5.0",
"grunt-contrib-uglify": "~0.5.0",
"grunt-contrib-watch": "~0.6.1",
"grunt-newer": "~0.7.0",
"load-grunt-tasks": "~0.6.0"
},
"scripts": {
"preinstall": "npm cache clear"
}
}