This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
189 lines (160 loc) · 4.92 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const url = require('url');
const gulp = require('gulp');
const u = require('gulp-util');
const gulpLoadPlugins = require('gulp-load-plugins');
const browserSync = require('browser-sync');
const del = require('del');
const map = require('vinyl-map');
const nunjucks = require('nunjucks');
const quaff = require('quaff');
const runSequence = require('run-sequence');
const stripAnsi = require('strip-ansi');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const yargs = require('yargs');
const $ = gulpLoadPlugins();
const args = yargs.argv;
const bs = browserSync.create();
const webpackConfig = require('./webpack.config');
if (args.production) {
webpackConfig.debug = false;
webpackConfig.devtool = 'source-map';
webpackConfig.plugins = webpackConfig.plugins.concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}),
new webpack.optimize.OccurenceOrderPlugin()
]);
}
const webpackBundle = webpack(webpackConfig);
webpackBundle.plugin('done', (stats) => {
if (stats.hasErrors() || stats.hasWarnings()) {
return bs.sockets.emit('fullscreen:message', {
title: 'Webpack Error:',
body: stripAnsi(stats.toString()),
timeout: 100000
});
}
bs.reload();
});
gulp.task('scripts', (cb) => {
webpackBundle.run((err, stats) => {
if (err) throw new u.PluginError('webpack', err);
u.log('[webpack]', stats.toString({colors: true}));
cb();
});
});
const package = require('./package');
const data = quaff('./data');
const basePath = args.production ? url.resolve('/', package.config.slug) + '/' : '/';
data.PATH_PREFIX = basePath;
const fullPath = url.format({
protocol: 'http',
host: package.config.s3_bucket,
pathname: package.config.slug
}) + '/';
data.PATH_FULL = fullPath;
const env = nunjucks.configure('./app/templates', {
autoescape: false,
noCache: true
});
gulp.task('templates', () => {
const nunjuckify = map((code, filename) => {
return env.renderString(code.toString(), {data: data})
})
// All .html files are valid, unless they are found in templates
return gulp.src(['./app/**/*.html', `!./app/templates/**`])
.pipe(nunjuckify)
.pipe(gulp.dest('./.tmp'))
.pipe($.if(args.production, $.htmlmin({collapseWhitespace: true})))
.pipe($.if(args.production, gulp.dest('./dist')))
.pipe($.size({title: 'templates', showFiles: true}));
});
gulp.task('templates-watch', ['templates'], bs.reload);
gulp.task('styles', () => {
return gulp.src('./app/styles/*.scss')
.pipe($.newer('./.tmp/styles'))
.pipe($.sourcemaps.init())
.pipe($.sass({
includePaths: ['node_modules'],
precision: 10
}).on('error', $.sass.logError))
.pipe($.cssnano({
autoprefixer: {
browsers: ['last 2 versions']
},
discardComments: {
removeAll: true
}
}))
.pipe($.sourcemaps.write(args.production ? '.' : undefined))
.pipe(gulp.dest('./.tmp/styles'))
.pipe($.if(args.production, gulp.dest('./dist/styles')))
.pipe(bs.stream({match: '**/*.css'}))
.pipe($.size({title: 'styles'}));
});
gulp.task('images', () => {
return gulp.src('./app/assets/images/**/*')
.pipe($.cache($.imagemin({
progressive: true,
interlaced: true
})))
.pipe(gulp.dest('./dist/assets/images'))
.pipe($.size({title: 'images'}));
});
gulp.task('fonts', function () {
return gulp.src('./app/assets/fonts/**/*')
.pipe(gulp.dest('./dist/assets/fonts'))
.pipe($.size({title: 'fonts'}))
});
gulp.task('clean', cb => {
return del(['./.tmp/**', './dist/**', '!dist/.git'], {dot: true}, cb);
});
gulp.task('serve', ['styles', 'templates'], () => {
bs.init({
logConnections: true,
logPrefix: 'NEWSAPPS',
middleware: [
webpackDevMiddleware(webpackBundle, {
publicPath: webpackConfig.output.publicPath,
stats: {colors: true}
})
],
notify: false,
open: false,
plugins: ['bs-fullscreen-message'],
port: 3000,
server: {
baseDir: ['.tmp', 'app'],
routes: {
'/node_modules': 'node_modules'
}
}
});
gulp.watch(['./app/styles/**/*.scss'], ['styles']);
gulp.watch('./app/**/*.html', ['templates-watch']);
});
gulp.task('rev', () => {
return gulp.src(['./dist/**/*.css', './dist/**/*.js', './dist/assets/images/**/*'], { base: './dist' })
.pipe($.rev())
.pipe(gulp.dest('./dist'))
.pipe($.rev.manifest())
.pipe(gulp.dest('./dist'));
});
gulp.task('revreplace', ['rev'], () => {
var manifest = gulp.src('./dist/rev-manifest.json');
return gulp.src('./dist/**/*.html')
.pipe($.revReplace({manifest: manifest}))
.pipe(gulp.dest('./dist'));
});
gulp.task('default', ['clean'], (cb) => {
runSequence(['fonts', 'images', 'scripts', 'styles', 'templates'], ['revreplace'], cb);
});
gulp.task('build', ['default']);