gulp压缩构建代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | // 获取 gulp var gulp = require( 'gulp' ); // 获取 uglify 模块(用于压缩 JS) var uglify = require( 'gulp-uglify' ); // 获取 htmlminify 模块(用于压缩 html) var htmlminify = require( "gulp-html-minify" ); // 获取 minify-css 模块(用于压缩 CSS) var minifyCSS = require( 'gulp-minify-css' ); // 版本号 var assetRev = require( 'gulp-asset-rev' ); var exec = require( 'child_process' ).exec; // 压缩 js 文件 // 在命令行使用 gulp script 启动此任务 gulp.task( 'script' , function () { gulp.src( 'develop/jingcai/js/controller/*.js' ) .pipe(assetRev()) .pipe(uglify()) .pipe(gulp.dest( 'src/jingcai/js/controller' )); gulp.src( 'develop/renren/js/controller/*.js' ) .pipe(assetRev()) .pipe(uglify()) .pipe(gulp.dest( 'src/renren/js/controller' )); gulp.src( 'develop/libjs/etc/*.js' ) .pipe(assetRev()) .pipe(uglify()) .pipe(gulp.dest( 'src/libjs/etc' )); gulp.src( 'develop/libjs/lib/*.js' ) .pipe(assetRev()) .pipe(uglify()) .pipe(gulp.dest( 'src/libjs/lib' )); gulp.src( 'develop/libjs/service/*.js' ) .pipe(assetRev()) .pipe(uglify()) .pipe(gulp.dest( 'src/libjs/service' )); }); // 压缩 html 文件 gulp.task( 'html' , [ 'script' ], function () { gulp.src( "develop/jingcai/c2c/*.html" ) .pipe(assetRev()) .pipe(htmlminify()) .pipe(gulp.dest( "src/jingcai/c2c" )); gulp.src( "develop/renren/views/*.html" ) .pipe(assetRev()) .pipe(htmlminify()) .pipe(gulp.dest( "src/renren/views" )); gulp.src( "develop/jingcai/userCenter/*.html" ) .pipe(assetRev()) .pipe(htmlminify()) .pipe(gulp.dest( "src/jingcai/userCenter" )); gulp.src( "develop/jingcai/doc/*.html" ) .pipe(assetRev()) .pipe(htmlminify()) .pipe(gulp.dest( "src/jingcai/doc" )); gulp.src( "develop/renren/doc/*.html" ) .pipe(assetRev()) .pipe(htmlminify()) .pipe(gulp.dest( "src/renren/doc" )); gulp.src( "develop/*.html" ) .pipe(assetRev()) .pipe(htmlminify()) .pipe(gulp.dest( "src" )); }); // 压缩 css 文件 gulp.task( 'css' , [ 'html' ], function () { gulp.src( 'develop/style/*.css' ) .pipe(assetRev()) .pipe(minifyCSS()) .pipe(gulp.dest( 'src/style' )); }); // cp image gulp.task( 'mvImgageDir' , [ 'css' ], function (cb) { exec( 'mkdir src;cd src;mkdir images;mkdir activity' , function (err) { if (err) { console.log( '图片文件夹创建失败' ); exec( 'cp develop/images/* src/images' , function (err) { if (err) { console.log(err); } else { console.log( '图片文件夹移动完毕' ) cb(); } }); } else { console.log( '图片文件夹创建完毕' ); exec( 'cp develop/images/* src/images' , function (err) { if (err) { console.log(err); } else { console.log( '图片文件夹移动完毕' ); cb(); } }); } }); }); //cp activity gulp.task( 'cpActivity' , [ 'mvImgageDir' ], function () { return gulp.src([ './develop/activity/**/*' ]) .pipe(gulp.dest( './src/activity' )); }) gulp.task( 'default' , [ 'cpActivity' ]); |
gulp-asset-rev
node-models 目录下 gulp-asset-rev 目录下 index.js
// 默认格式是style-323923.css
// var verStr = (options.verConnecter || "-") + md5;
// src = src.replace(verStr, '').replace(/(\.[^\.]+)$/, verStr + "$1");
// 新增改变gulp格式问题style?v=xxx
var verStr = (options.verConnecter || "") + md5;
src = src + "?v=" + verStr;
pakeage.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | { "name" : "wap_betting" , "version" : "1.0.0" , "description" : "Betting web application with VueJS" , "scripts" : { "start" : "node src/node_modules/http-server/bin/http-server -o" }, "keywords" : [], "repository" : { "type" : "git" , "url" : "git+ssh://git@github.com/caiqr/webLottery.git" }, "author" : "CaiqrWebFront" , "dependencies" : { "forever" : "^0.15.2" , "gulp" : "^3.9.1" , "gulp-html-minify" : "0.0.14" , "gulp-minify-css" : "^1.2.4" , "gulp-uglify" : "^2.0.0" }, "bugs" : { "url" : "https://github.com/caiqr/webLottery/issues" }, "homepage" : "https://github.com/caiqr/webLottery#readme" , "main" : "gulpfile.js" , "devDependencies" : { "gulp-asset-rev" : "0.0.15" }, "license" : "ISC" } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步