单纯是为了做一些小的页面,没必要引入vue/react,但是如果不压缩代码,显得很臃肿,所以引入grunt进行打包,记录一下。
项目结构:
js:放第三方js的一些压缩包。例如:jquery、jsencrypt
public:静态资源,图片之类的
Gruntfile.js
module.exports = function (grunt) {
// 配置任务
grunt.initConfig({
copy: {
dist: {
files: [
{ expand: true, cwd: './', src: ['public/**', 'js/**'], dest: 'dist/' }
]
}
},
htmlmin: {
dist: {
options: {
removeComments: true,
collapseWhitespace: true,
collapseInlineTagWhitespace: true,
minifyCSS: true,
minifyJS: true
},
files: {
'dist/index.html': './index.html'
}
}
}
});
// 加载插件任务
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-htmlmin');
// 注册任务
grunt.registerTask('build', ['copy', 'htmlmin']);
};
index.html
package.json
{
"name": "demo",
"version": "1.0.0",
"description": "",
"main": "Gruntfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"grunt": "^1.0.0",
"grunt-contrib-copy": "^1.0.0",
"grunt-contrib-cssmin": ">=1.0.0",
"grunt-contrib-htmlmin": "^2.1.0"
}
}