本文目录

Gulp 压缩 JS.CSS 修改 config 时间戳,生成 ZIP

🌙
手机阅读
本文目录结构

需要注意任务的顺序问题;

代码如下

1、依赖的包

'use strict';
var gulp = require('gulp');
var scp = require('gulp-scp2');//上传到服务器,相当于全部文档全部复制
var uglify = require('gulp-uglify');//获取 uglify 模块(用于压缩 JS);
var cleanCSS = require('gulp-clean-css');//gulp-minify-css 已经不推荐使用 https://github.com/scniro/gulp-clean-css
const zip = require('gulp-zip');
var gulpRreplace = require('gulp-replace');

2、时间戳生成、文件来源配置

var nowDate = new Date(),
    nowYear=nowDate.getFullYear(),
    nowMonth=unshiftZero(nowDate.getMonth()+1),
    nowDay=unshiftZero(nowDate.getDate()),
    fullHours=unshiftZero(nowDate.getHours()),
    fullMinutes=unshiftZero(nowDate.getMinutes());
var nowTimeStr=nowYear+nowMonth+nowDay+fullHours+fullMinutes;
var ibeaconAgentJsAry=[
    'wifisong.resource/project/ibeacon-agent/src/payopen-special-merchant-statis.js',
    'wifisong.resource/project/ibeacon-agent/src/payopen-special-merchants.js',
    'wifisong.resource/project/ibeacon-agent/src/payopen-service_providers.js',
    'wifisong.resource/project/ibeacon-agent/src/payopen-statistic-wechat-bill.js',
    'wifisong.resource/project/ibeacon-agent/src/payopen-apps-index.js'
];
var ibeaconAgentCssAry=[
    // 'wifisong.resource/project/ibeacon-agent/css/shakemoney-device-bind.css'
];
var ibeaconAgentImgAry=[
    // 'wifisong.resource/project/ibeacon-agent/img/user-header.png'
];

3、任务配置

gulp.task('ibeaconAgent-Build',['ibeaconAgent-miniJs','ibeaconAgent-miniCss','ibeaconAgent-miniImg','ibeaconAgent-updateConfig'], function () {
    console.log("文件压缩/转移成功...");
    gulp.src('../update-pack/ibeaconAgent/wifisong.resource/**/*',{ base: '../update-pack/ibeaconAgent' }) //待压缩的文件是存放在这个文件夹下的所有文件
        .pipe(zip('wifisong.resource.zip')) //输出的文件名
        .pipe(gulp.dest('../update-pack/ibeaconAgent/')); //输出到当前目录
    console.log("已生成ZIP压缩包");
});

gulp.task('ibeaconAgent-miniJs', function() {
    gulp.src(ibeaconAgentJsAry)
        .pipe(uglify({mangle: {reserved: ['require' ,'exports' ,'module' ,'$']}}))
        .pipe(gulp.dest('../update-pack/ibeaconAgent/wifisong.resource/project/ibeacon-agent/dist/'))
});
gulp.task('ibeaconAgent-miniCss', function () {
    gulp.src(ibeaconAgentCssAry)
        // .pipe(cleanCSS({compatibility: 'ie8'}))
        .pipe(cleanCSS())
        .pipe(gulp.dest('../update-pack/ibeaconAgent/wifisong.resource/project/ibeacon-agent/css/'))
});
gulp.task('ibeaconAgent-miniImg', function () {
    gulp.src(ibeaconAgentImgAry)
        .pipe(gulp.dest('../update-pack/ibeaconAgent/wifisong.resource/project/ibeacon-agent/img/'))
});
gulp.task('ibeaconAgent-updateConfig', function(){
    gulp.src(
        ['wifisong.resource/project/ibeacon-agent/config.js']
        // { base:'wifisong.resource/ssl/a/' }
    )
        .pipe(gulpRreplace('dev = true', 'dev = false'))
        .pipe(gulpRreplace('201604190444fffdddffff', nowTimeStr))
        .pipe(gulp.dest('../update-pack/ibeaconAgent/wifisong.resource/project/ibeacon-agent/'))
});

4、执行任务,webstorm 集合了 gulp;直接确定即可运行;

AXIHE / 精选资源

浏览全部教程

面试题

学习网站

前端培训
自己甄别

前端书籍

关于朱安邦

我叫 朱安邦,阿西河的站长,在杭州。

以前是一名平面设计师,后来开始接接触前端开发,主要研究前端技术中的JS方向。

业余时间我喜欢分享和交流自己的技术,欢迎大家关注我的 Bilibili

关注我: Github / 知乎

于2021年离开前端领域,目前重心放在研究区块链上面了

我叫朱安邦,阿西河的站长

目前在杭州从事区块链周边的开发工作,机械专业,以前从事平面设计工作。

2014年底脱产在老家自学6个月的前端技术,自学期间几乎从未出过家门,最终找到了满意的前端工作。更多>

于2021年离开前端领域,目前从事区块链方面工作了