pro.build.js 3.9 KB
Newer Older
Y
Your Name 已提交
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
'use strict';
/**
 * @name 编译
 * @author 广州银云信息科技有限公司
 */
/**
 * @version 4.0
 * @description 去掉图片压缩&字体压缩
 */
var gulp = require('gulp');
var path = require('path');
var config = require('./config');
var _ = require('lodash');
var $ = require('gulp-load-plugins')({
    pattern: ['gulp-*', 'main-bower-files', 'uglify-save-license', 'del', 'imagemin-pngquant']
});
var sass = require('gulp-sass');
var replace = require('gulp-replace');
/**
 * [生成Html模版文件]
 */

gulp.task('build:partials', function () {
    return gulp.src([
            path.join(config.paths.src, '/app/**/*.html')
        ])
        .pipe($.minifyHtml({
            empty: true,
            spare: true,
            quotes: true
        }))
        .pipe($.angularTemplatecache('templateCacheHtml.js', {
            module: config.modules.templateModuleName,
            root: 'app'
        }))
        .pipe(gulp.dest(config.paths.tmp + '/partials/'));
});

gulp.task('build:partials:_default', function () {
    return gulp.src([
            path.join(config.paths.src, '/module/_default/**/*.html')
        ])
        .pipe($.minifyHtml({
            empty: true,
            spare: true,
            quotes: true
        }))
        .pipe($.angularTemplatecache('_default_templateCacheHtml.js', {
            module: config.modules.templateModuleName,
            root: 'module/_default'
        }))
        .pipe(gulp.dest(config.paths.tmp + '/partials/'));
});
/**
 * [Html,Js,Css压缩合并]
 */
gulp.task('build:html', ['build:inject', 'build:partials','build:partials:_default'], function () {
    var partialsInjectFile = gulp.src([path.join(config.paths.tmp, '/partials/templateCacheHtml.js'),path.join(config.paths.tmp, '/partials/_default_templateCacheHtml.js')], {
        read: false
    });
    var partialsInjectOptions = {
        starttag: '<!-- inject:partials -->',
        ignorePath: path.join(config.paths.tmp, '/partials'),
        addRootSlash: false
    };

    var htmlFilter = $.filter('*.html', {
        restore: true
    });
    var jsFilter = $.filter('**/*.js', {
        restore: true
    });
    var cssFilter = $.filter('**/*.css', {
        restore: true
    });

    return gulp.src(path.join(config.paths.tmp, '/serve/*.html'))
        //error 
        .pipe($.plumber(config.errorHandler()))
        //inject template
        .pipe($.inject(partialsInjectFile, partialsInjectOptions))
        //js
        .pipe($.useref()) //合并和压缩
        .pipe(jsFilter)
        //修复HTML图片地址
        .pipe($.replace('app/assets/', 'assets/'))
        .pipe($.stripDebug())
        .pipe($.uglify())
        .pipe(jsFilter.restore)
        //css 
        .pipe(cssFilter)
        //修复HTML图片地址
        .pipe($.replace('app/assets/', 'assets/'))
        .pipe($.replace('(assets/', '(../assets/'))
        .pipe($.autoprefixer({
            browsers: ['last 20 versions'],
            cascade: false
        }))
        .pipe($.csso())
        .pipe(cssFilter.restore)
        //md5后缀
        .pipe($.if('*.css', $.rev()))
        .pipe($.if('*.js', $.rev()))
        //替换md5后缀的文件名
        .pipe($.revReplace())
        //html处理
        .pipe(htmlFilter)
        // .pipe($.replace('<base href="/">', '<base href="/eolinker/">'))
        // .pipe($.minifyHtml({
        //     empty: true,
        //     spare: true,
        //     quotes: true,
        //     conditionals: true
        // }))
        .pipe(htmlFilter.restore)
        .pipe(gulp.dest(path.join(config.paths.dist, '/')))
        .pipe($.size({
            title: path.join(config.paths.dist, '/'),
            showFiles: true
        }));

});


gulp.task('build', $.sequence('prod-config', ['clean', 'clear_inject_sass', 'build:html'],'other:vendor', 'other:libs', 'other:assets'));//,'translate'
gulp.task('build:e2e', $.sequence('test-config', ['clean', 'build:html'],'other:vendor', 'other:libs', 'other:assets'));