grunt構建專案打包上傳
全域性安裝grunt:
npm install -g grunt-cli
生成package.json檔案:
npm init
安裝grunt和所需要的外掛:
npm install grunt –save-dev
npm install –save-dev grunt-contrib-concat grunt-contrib-jshint grunt-contrib-sass grunt-contrib-uglify grunt-contrib-watch grunt-contrib-connect
配置gruntfile.js語法:
module.exports = function(grunt) {
grunt.initConfig({
pkg : grunt.file.readJSON('package.json'),
clean : {
sit : {
src : [ './build/sit/zyb/**' ]
}
},
uglify : {
sit : {
files : [ {
expand : true ,
cwd : './js',
src : [ '**/*.js', '!**/*.mini.js', '!**/*.min.js' ],
dest : 'build/sit/zyb/js'
} ]
}
},
cssmin : {
sit : {
files : [ {
expand : true,
cwd : './css' ,
src : [ '*.css', '**/*.css' ],
dest : 'build/sit/zyb/css'
} ]
}
},
imagemin : {
sit : {
options : {
optimizationLevel : 3
},
files : [ {
expand : true,
cwd : 'images/',
src : [ '**/*.{png,jpg,gif}' ],
dest : 'build/sit/zyb/images'
} ]
}
},
copy : {
sit : {
files : [ {
expand : true,
cwd : './',
src : [ 'datePicker/**/*', 'ui/**/*', 'js/**/*/*.min.js',
'js/**/*/*.mini.js', 'js/**/*/*.css' ],
dest : 'build/sit/zyb'
} ]
},
prod : {
files : [ {
expand : true,
cwd : './',
src : [ 'datePicker/**/*', 'ui/**/*', 'js/**/*',
'css/**/*', 'images/**/*', 'js/pages/renew/payInsurance.js' ],
dest : 'build/sit/zyb'
} ]
}
},
replace : {
sit : {
options : {
patterns : [ {
match : /version=(\w+)/g,
replacement : 'version=ZYB2018' + new Date().getTime()
} ]
},
files : [ {
expand : true,
src : [ 'pages/**/*.html' ],
dest : 'build/sit/zyb'
} ]
},
sit_001 : {
options : {
patterns : [ {
match : /\/\/(\s*)payAmt(\s*)=(\s*)0.01;/g,
replacement : 'payAmt = 0.01;'
} ]
},
files : [ {
expand : true,
src : [ 'js/pages/renew/payInsurance.js' ],
dest : 'build/sit/zyb'
} ]
}
},
"sftp-deploy" : {
it : {
auth : {
host : '10.196.112.41',
port : 22,
authKey : {
"username" : "user1",
"password" : "kfuser1123"
}
},
src : './build/sit/zyb',
dest : '/home/user1/app/project/zyb',
// exclusions: ['build', '*.bat', '*.json', 'node_modules',
// 'Gruntfile.js'],
serverSep : '/',
localSep : '/',
concurrency : 6
},
sit1 : {
auth : {
host : '10.196.112.22',
port : 22,
authKey : {
"username" : "user1",
"password" : "U6JNEbRJ"
}
},
src : './build/sit/zyb',
dest : '/home/user1/app/project/zyb',
// exclusions: ['build', '*.bat', '*.json', 'node_modules',
// 'Gruntfile.js'],
serverSep : '/',
localSep : '/',
concurrency : 6
},
sit2 : {
auth : {
host : '10.196.112.16',
port : 22,
authKey : {
"username" : "user1",
"password" : "stuser1123"
}
},
src : './build/sit/zyb',
dest : '/home/user1/app/project/zyb',
// exclusions: ['build', '*.bat', '*.json', 'node_modules',
// 'Gruntfile.js'],
serverSep : '/',
localSep : '/',
concurrency : 6
}
}
});
// 載入外掛
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-replace');
grunt.loadNpmTasks('grunt-sftp-deploy');
// 任務列表
grunt.registerTask('test', [ 'cssmin:sit' ]);
grunt.registerTask('all', ['clean:sit', 'copy:sit', 'replace:sit'])
grunt.registerTask('default', [ 'copy:sit', 'replace:sit']);
grunt.registerTask('sit001', ['replace:sit_001']);
grunt.registerTask('prod', ['copy:prod', 'replace:sit']);
grunt.registerTask('it', [ 'sftp-deploy:it' ]);
grunt.registerTask('st', [ 'sftp-deploy:sit1', 'sftp-deploy:sit2' ]);
}
每個目標的具體設定,需要參考該模板的文件。就cssmin來講,minify目標的引數具體含義如下:
expand:如果設為true,就表示下面檔名的佔位符(即*號)都要擴充套件成具體的檔名。
cwd:需要處理的檔案(input)所在的目錄。
src:表示需要處理的檔案。如果採用陣列形式,陣列的每一項就是一個檔名,可以使用萬用字元。
dest:表示處理後的檔名或所在目錄。
ext:表示處理後的檔案字尾名。
grunt常用模組:
grunt-contrib-clean:刪除檔案。
grunt-contrib-compass:使用compass編譯sass檔案。
grunt-contrib-concat:合併檔案。
grunt-contrib-copy:複製檔案。
grunt-contrib-cssmin:壓縮以及合併CSS檔案。
grunt-contrib-imagemin:影象壓縮模組。
grunt-contrib-jshint:檢查JavaScript語法。
grunt-contrib-uglify:壓縮以及合併JavaScript檔案。
grunt-contrib-watch:監視檔案變動,做出相應動作。
執行grunt
執行所有任務
grunt
執行指定任務
grunt prod
常見問題
解決:npm install grunt –save-dev
成功:
build目錄下有打包好的檔案,直接用就好。