This repository was archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgulpfile.babel.js
217 lines (189 loc) · 6.02 KB
/
gulpfile.babel.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
'use strict';
import path from 'path';
import gulp from 'gulp';
import plumber from 'gulp-plumber'; // help to avoid crash if error in a task
import newer from 'gulp-newer';
import babel from 'gulp-babel'; // to distribute the npm module
import watch from 'gulp-watch'; // gulp.watch doesn't detect new files
import merge from 'merge-stream'; // handle multiple gulp.src in one task
import jsoneditor from 'gulp-json-editor'; // edit json in stream
import del from 'del'; // delete files
import imagemin from 'gulp-imagemin';
import runSequence from 'run-sequence';
import notifier from 'node-notifier';
import sourcemaps from 'gulp-sourcemaps';
import uglify from 'gulp-uglify';
import browserify from 'browserify';
import envify from 'envify/custom';
import babelify from 'babelify';
import source from 'vinyl-source-stream'; // helper for browserify text stream to gulp pipeline
import buffer from 'vinyl-buffer'; // helper for browserify
let errored = false;
let errorHandler = function (task) {
return function (error) {
errored = true;
console.log(`Error in ${task}: ${error.message}`);
notifier.notify({
title: `Error in ${task}`,
message: error.message,
icon: path.join(__dirname, 'gulp.png')
});
};
};
// ################
// # Entry points #
// ################
gulp.task('dev', ['buildpublic:dev'], function (done) {
watch('./app/assets/**/*', function (vinyl) {
console.log(`${vinyl.path} was ${vinyl.event}, piping to public/...`);
runSequence('assets');
});
watch('./app/vendor/**/*', function (vinyl) {
console.log(`${vinyl.path} was ${vinyl.event}, piping to public/vendor/...`);
runSequence('vendor');
});
watch('./app/js/**/*.js', function (vinyl) {
console.log(`${vinyl.path} was '${vinyl.event}', running Babel...`);
runSequence('es6-7:dev');
});
});
gulp.task('dist', function (done) {
runSequence(
'dist:clear',
['dist:compileserver', 'dist:copyservermisc', 'dist:copyserverviews', 'dist:copypkg', 'buildpublic:dist'],
'dist:copypublic',
function () {
if (errored) {
console.log('Distribution failed, cleaning dist directory');
runSequence('dist:clear', function () {
process.exit(-1);
});
}
});
});
// ####################
// # public directory #
// ####################
gulp.task('buildpublic:dist', function (done) {
runSequence(
'buildpublic:clear',
['assets', 'vendor', 'es6-7:dist'],
'buildpublic:imagemin',
done);
});
gulp.task('buildpublic:dev', function (done) {
runSequence(
'buildpublic:clear',
['assets', 'vendor', 'es6-7:dev'],
done);
});
gulp.task('buildpublic:clear', function (done) {
del(['./public/**/*']).then(function () {
done();
});
});
gulp.task('buildpublic:imagemin', function () {
return gulp.src('./public/img/**/*.{png,jpg,gif,svg}', {
base: './public'
})
.pipe(plumber(errorHandler('buildpublic:imagemin')))
.pipe(imagemin({
progressive: true
}))
.pipe(gulp.dest('./public'));
});
// Babel
let es67 = () => {
return browserify({ entries: './app/js/app.js', debug: true }) // debug for sourcemaps
.transform(babelify, { presets: ['es2015', 'stage-3', 'react'], plugins: ['transform-decorators-legacy'] });
};
gulp.task('es6-7:dev', function () {
return es67()
.bundle()
.on('error', function (error) {
errorHandler('es6-7')(error);
this.emit('end');
}) // Don't crash if failed, plumber doesn't work with browserify
.pipe(source('bundle.min.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./public/js'));
});
gulp.task('es6-7:dist', function () {
return es67()
.transform(envify({ NODE_ENV: 'production' }), { global: true }) // global: act on node_modules (here react prod mode)
.bundle()
.on('error', function (error) {
errorHandler('es6-7')(error);
this.emit('end');
}) // Don't crash if failed, plumber doesn't work with browserify
.pipe(source('bundle.min.js'))
.pipe(buffer())
.pipe(uglify())
.pipe(gulp.dest('./public/js'));
});
// assets and vendor
gulp.task('assets', function () {
return gulp.src('./app/assets/**/*', {
base: './app/assets'
})
.pipe(plumber(errorHandler('assets')))
.pipe(newer('./public'))
.pipe(gulp.dest('./public'));
});
gulp.task('vendor', function () {
return gulp.src('./app/vendor/**/*', {
base: './app'
})
.pipe(plumber(errorHandler('vendor')))
.pipe(newer('./public'))
.pipe(gulp.dest('./public'));
});
// ################
// # Distribution #
// ################
gulp.task('dist:clear', function (done) {
del(['./dist/**/*']).then(function () {
done();
});
});
gulp.task('dist:compileserver', function () {
return gulp.src(['./index.js', './{bin,lib}/**/*.js'], { base: './' })
.pipe(babel({ 'plugins': ['transform-runtime'] }))
.on('error', function (error) {
errorHandler('dist:compileserver')(error);
this.emit('end');
})
.pipe(gulp.dest('./dist'));
});
gulp.task('dist:copyservermisc', function () {
return gulp.src('./misc/**/*', { base: './' })
.pipe(plumber(errorHandler('dist:copyservermisc')))
.pipe(gulp.dest('./dist'));
});
gulp.task('dist:copyserverviews', function () {
return gulp.src('./views/**/*', { base: './' })
.pipe(plumber(errorHandler('dist:copyservermisc')))
.pipe(gulp.dest('./dist'));
});
gulp.task('dist:copypkg', function () {
let txt = gulp.src(['./README.md', './LICENSE'])
.pipe(plumber(errorHandler('dist:copypkg (txt)')))
.pipe(gulp.dest('./dist'));
let pkg = gulp.src('./package.json')
.pipe(jsoneditor(function (json) {
delete json.scripts;
delete json.devDependencies;
delete json.semistandard;
return json;
}))
.pipe(plumber(errorHandler('dist:copypkg (pkg)')))
.pipe(gulp.dest('./dist'));
return merge(txt, pkg);
});
gulp.task('dist:copypublic', function () {
return gulp.src(['./public/**/*'], { base: './' })
.pipe(plumber(errorHandler('dist:copypublic')))
.pipe(gulp.dest('./dist'));
});