Blame view

public/gulpfile.js 5.82 KB
ccbikai authored
1 2 3 4 5 6 7 8
/**
 * Yohobuy 构建脚本
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/4/25
 */

'use strict';
htoooth authored
9 10
const path = require('path');
ccbikai authored
11
const gulp = require('gulp');
htoooth authored
12
const _ = require('lodash');
ccbikai authored
13 14 15 16 17 18
const gutil = require('gulp-util');
const ftp = require('gulp-ftp');

const postcss = require('gulp-postcss');
const sourcemaps = require('gulp-sourcemaps');
const cssnano = require('gulp-cssnano');
htoooth authored
19
const rename = require('gulp-rename');
ccbikai authored
20 21

const webpack = require('webpack');
xuqi authored
22
const WebpackDevServer = require('webpack-dev-server');
ccbikai authored
23 24 25 26 27 28 29 30 31
const webpackConfig = require('./webpack.config.js');

const env = {
    dev: Symbol('development'),
    pro: Symbol('production')
};

const config = require('../package.json');
const ftpConfig = {
毕凯 authored
32
    host: '218.94.75.58',
ccbikai authored
33 34 35 36 37 38 39 40
    user: 'php',
    pass: 'yoho9646'
};
const distRoot = `dist/${config.name}`;
const dist = {
    js: `${distRoot}/${config.version}`,
    css: `${distRoot}/${config.version}`,
    assets: `${distRoot}/assets`,
yyq authored
41 42
    img: `${distRoot}/${config.version}/img`,
    font: `${distRoot}/${config.version}/font`
ccbikai authored
43 44 45 46 47 48 49 50 51 52 53 54
};

/**
 * postcss plugins for both dev and pro
 * @parem et Symbol
 */
const postcssPlugin = (et) => {
    var sprites = {
            spritesmith: {
                padding: 2
            },
            groupBy(file) {
htoooth authored
55 56
                var dir = _.last(path.parse(file.styleFilePath).dir.split(path.sep));
                var group = dir === 'scss' ? path.basename(file.styleFilePath, '.css') : dir;
ccbikai authored
57 58 59 60 61 62 63 64 65 66

                return group ? Promise.resolve(group) : Promise.reject(group);
            }
        },
        assets,
        plugins;

    // assets & sprites config in both dev and pro
    if (et === env.pro) {
        assets = {
毕凯 authored
67 68
            loadPaths: [dist.img, dist.font],
            relativeTo: dist.css
ccbikai authored
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
        };

        Object.assign(sprites, {
            basePath: dist.img,
            stylesheetPath: dist.css,
            spritePath: dist.img
        });
    } else if (et === env.dev) {
        assets = {
            loadPaths: ['img/', 'font/'],
            relativeTo: 'css/'
        };

        Object.assign(sprites, {
            basePath: 'img/',
            stylesheetPath: 'css/',
            spritePath: 'img/'
        });
    }

    plugins = [
        require('autoprefixer')({
            browsers: ['> 1%']
        }),
        require('precss'),
        require('postcss-sprites').default(sprites),
        require('postcss-assets')(assets),
        require('postcss-calc'),
        require('postcss-opacity'),

        // 可选
        require('postcss-use')({
            modules: ['postcss-clearfix', 'postcss-crip', 'postcss-short', 'postcss-center', 'postcss-position']
        })
    ];

    if (et === env.pro) {
        plugins.push(require('postcss-cachebuster')({
            imagesPath: `/${dist.img}`,
            cssPath: `/${dist.css}`
        }));
    }
    return plugins;
};

// default
李奇 authored
115
gulp.task('default', ['postcss-dev', 'postcss-watch', 'webpack-dev-server']);
ccbikai authored
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

// ge
gulp.task('ge', ['postcss', 'webpack']);

// dist
gulp.task('dist', ['ge'], () => {
    var ftpstream = ftp(ftpConfig);

    return gulp.src('dist/**/')
        .pipe(ftpstream)
        .pipe(gutil.noop());
});

// postcss compile in dev
gulp.task('postcss-dev', () => {
htoooth authored
131
    return gulp.src(['scss/base.css', 'scss/*/_index.css', '!scss/plugin/*', '!scss/common/*'])
ccbikai authored
132 133
        .pipe(sourcemaps.init())
        .pipe(postcss(postcssPlugin(env.dev)))
htoooth authored
134 135 136 137
        .on('error', function(err) {
            gutil.log(err);
            this.emit('end');
        })
htoooth authored
138 139 140 141 142 143 144 145
        .pipe(rename((cssPath) => {
            if (cssPath.dirname === '.') {
                return;
            }

            cssPath.basename = `${cssPath.dirname}`;
            cssPath.dirname = './';
        }))
ccbikai authored
146 147 148 149 150 151
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('css/'));
});

// postcss file watch
gulp.task('postcss-watch', () => {
李奇 authored
152
    gulp.watch('scss/**/*.css', ['postcss-dev']);
ccbikai authored
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
});

// copy assets
gulp.task('assets', ['img', 'font']);

// copy img
gulp.task('img', () => {
    return gulp.src('img/**/*')
        .pipe(gulp.dest(dist.img));
});

// copy font
gulp.task('font', () => {
    return gulp.src('font/*')
        .pipe(gulp.dest(dist.font));
});

// postcss compile in pro
gulp.task('postcss', ['assets'], () => {
htoooth authored
172
    return gulp.src(['scss/base.css', 'scss/*/_index.css', '!scss/plugin/*', '!scss/common/*'])
ccbikai authored
173
        .pipe(postcss(postcssPlugin(env.pro)))
htoooth authored
174
        .pipe(cssnano({zindex: false }))
htoooth authored
175 176 177 178 179 180 181 182
        .pipe(rename((cssPath) => {
            if (cssPath.dirname === '.') {
                return;
            }

            cssPath.basename = `${cssPath.dirname}`;
            cssPath.dirname = './';
        }))
ccbikai authored
183 184 185 186 187 188 189 190 191
        .pipe(gulp.dest(dist.css));
});

// webpack dev server
gulp.task('webpack-dev-server', () => {
    var devConfig = Object.assign({}, webpackConfig, {
        debug: true
    });
xuqi authored
192
    new WebpackDevServer(webpack(devConfig), {
ccbikai authored
193
        contentBase: '.',
biao authored
194
        publicPath: '//localhost:5002/',
ccbikai authored
195 196 197
        hot: true,
        stats: {
            colors: true
毕凯 authored
198 199 200
        },
        headers: {
            'Access-Control-Allow-Origin': '*'
ccbikai authored
201
        }
徐炜 authored
202
    }).listen(5002, '0.0.0.0', (err) => {
ccbikai authored
203 204 205
        if (err) {
            throw new gutil.PluginError('webpack-dev-server', err);
        }
毕凯 authored
206
        gutil.log('[webpack-serve]', 'http://localhost:5002/');
ccbikai authored
207 208 209 210
    });
});

// webpack compile in pro
毕凯 authored
211
gulp.task('webpack', (done) => {
ccbikai authored
212 213 214
    var proConfig = Object.assign({}, webpackConfig);

    proConfig.output.path = dist.js;
毕凯 authored
215 216
    proConfig.plugins.push(new webpack.optimize.UglifyJsPlugin({
        compress: {
yyq authored
217 218
            warnings: false,
            properties: false
yyq authored
219 220 221
        },
        output: {
            keep_quoted_props: true
毕凯 authored
222 223
        }
    }));
biao authored
224
    webpack(proConfig, (err, stats) => {
ccbikai authored
225 226 227
        if (err) {
            throw new gutil.PluginError('webpack', err);
        }
xuqi authored
228
        gutil.log('[webpack compile]:', stats.endTime - stats.startTime, 'ms');
毕凯 authored
229
        done();
ccbikai authored
230 231
    });
});