-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
126 lines (105 loc) · 3.45 KB
/
gulpfile.js
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';
const gulp = require('gulp');
const gutil = require('gulp-util');
gulp.task('scripts', function(callback) {
const webpack = require('webpack');
const fs = require('fs');
const path = require('path');
const prettyBytes = require('pretty-bytes');
const gzipSize = require('gzip-size');
let webpackConfig = require('./webpack.config.js');
webpackConfig.plugins.push(
new webpack.optimize.UglifyJsPlugin({ sourceMap: false }),
new webpack.DefinePlugin({
__DEV__: false,
'process.env': {
NODE_ENV: '"production"'
}
})
);
webpack(webpackConfig, function(err, stats) {
if (err) {
throw new gutil.PluginError('webpack', err);
}
stats = stats.toString();
// Remove dropping unused statements and individual modules built
let tester = /Dropping unused(.*?)\n|\n(.*?)\[built\]/g;
stats = stats.replace(tester, '');
gutil.log('[webpack]', stats);
// Iterate through all of the output files and print their gzipped sizes
fs.readdir(path.resolve(__dirname, './app/scripts/dist/'), function(err, files) {
let filesLength = files.length;
gutil.log('gzipped file sizes:');
files.forEach(function(file) {
if (file.indexOf('.js') !== -1 && file.indexOf('.js') + 3 === file.length) {
gutil.log(file + ': ' + prettyBytes(
gzipSize.sync(fs.readFileSync(path.resolve(__dirname, './app/scripts/dist/', file)))
));
}
});
gutil.log('[webpack]', new Date());
callback();
});
});
});
gulp.task('scripts:watch', function() {
const webpack = require('webpack');
let webpackConfig = require('./webpack.config.js');
webpackConfig.plugins.push(
new webpack.DefinePlugin({
__DEV__: true,
'process.env': {
NODE_ENV: '"development"'
}
})
);
webpackConfig.devtool = 'source-map';
let compiler = webpack(webpackConfig);
compiler.watch(200, function(err, stats) {
if (err) {
throw new gutil.PluginError('webpack', err);
}
gutil.log('[webpack]', stats.toString({}));
gutil.log('[webpack]', new Date());
gutil.log('THIS IS FOR DEVELOPMENT ONLY.');
gutil.log('PLEASE BUILD NORMALLY BEFORE COMMITTING.');
});
});
gulp.task('styles', function() {
const less = require('gulp-less');
const lpAutoprefix = require('less-plugin-autoprefix');
const lpNPMImport = require('less-plugin-npm-import');
const sourcemaps = require('gulp-sourcemaps');
const minify = require('gulp-cssnano');
const autoprefix = new lpAutoprefix({browsers: ['last 2 versions']});
const npmImport = new lpNPMImport();
return gulp.src('./app/styles/less/styles.less')
.pipe(sourcemaps.init())
.pipe(less({
plugins: [npmImport, autoprefix]
}))
.pipe(minify())
.pipe(sourcemaps.write())
.pipe(gulp.dest('./app/styles/dist'));
});
gulp.task('styles:watch', function() {
gulp.watch('./app/styles/less/**/*.less' , ['styles']);
});
gulp.task('server', function() {
const browserSync = require('browser-sync');
const reload = browserSync.reload;
browserSync({
server: {
baseDir: 'app'
}
});
gulp.watch([
'./*.html',
'./scripts/dist/vendor.bundle.js',
'./scripts/dist/app.js',
'./styles/dist/styles.css'
], {cwd: 'app'}, reload);
});
gulp.task('dev', ['scripts:watch', 'styles:watch', 'server']);
gulp.task('build', ['scripts', 'styles']);
gulp.task('default', ['build']);