forked from karasatishkumar/whitestorm.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
272 lines (231 loc) · 7.33 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// UTILS
import path from 'path';
import del from 'del';
import {argv} from 'yargs';
// GULP
import gulp from 'gulp';
import loadPlugins from 'gulp-load-plugins';
// WEBPACK & KARMA
import karma from 'karma';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import {config} from './webpack.config.babel.js';
// SETTINGS
const
frameworkSrc = './src/framework',
frameworkDest = './build',
examplesDev = './src/examples',
examplesSrc = `${examplesDev}/**/*`,
examplesDest = './examples',
swigParameters = {
defaults: {
cache: false,
locals: {
assets: '../../_assets'
}
}
};
const $ = loadPlugins({
rename: {
'gulp-browser-basedir': 'gbrowser'
}
});
// COMPILERS
const isProduction = argv.prod ? true : process.env.NODE_ENV === 'production';
const webpackConfiguration = config({
isProduction,
frameworkSrc,
frameworkDest
});
const webpackCompiler = webpack(webpackConfiguration[0]);
const webpackCompilerLight = webpack(webpackConfiguration[1]);
// ENVIRONMENT SETUP
process.env.BABEL_ENV = 'node';
// ALIAS
gulp.task('default', ['build']);
gulp.task('build', ['src:build', 'examples:build']);
gulp.task('src:build', ['src:build:browser', 'src:build:node']);
// BUILD: browser
gulp.task('src:build:browser', ['build:clean'], (callback) => {
webpackCompiler.run((error, stats) => {
if (error) throw new $.util.PluginError('webpack', error);
$.util.log('[webpack]', stats.toString({colors: true}));
webpackCompilerLight.run((error, stats) => {
if (error) throw new $.util.PluginError('webpack', error);
$.util.log('[webpack]', stats.toString({colors: true}));
callback();
});
});
});
gulp.task('src:build:node', () => {
gulp.src(`${frameworkSrc}/**/*`)
.pipe($.cached('babel', {optimizeMemory: true}))
.pipe($.babel())
.on('error', makeBuildErrorHandler('babel'))
.pipe(gulp.dest('./lib/'));
});
// DEV MODE
gulp.task('dev', ['examples:build', 'examples:watch'], () => {
const server = new WebpackDevServer(webpackCompiler, {
contentBase: examplesDest,
publicPath: '/build/',
stats: {colors: true}
});
server.listen(8080, 'localhost', () => {});
});
// EXAMPLES: WATCH
gulp.task('examples:watch', () => {
const watcher = gulp.watch(examplesSrc, (obj) => {
if (obj.type === 'changed') {
if (path.extname(obj.path) === '.js') {
console.log('.js change detected.');
const filePath = path.relative(path.resolve('./'), obj.path);
gulp.src([
obj.path,
`!${examplesDev}/_assets/**/*.js`
])
.pipe($.plumber())
.pipe($.swig(Object.assign({}, swigParameters, {ext: '.js'})))
.pipe($.gbrowser.browserify({basedir: path.resolve(examplesDev)}, {
transform: 'babelify',
options: {presets: ['es2015']}
}))
.pipe(
gulp.dest(
path.join(
path.relative(path.resolve('./'), path.resolve(examplesDest)),
path.relative(path.resolve(examplesDev), path.dirname(obj.path))
)
)
);
console.log(`Swig, babelify & browserify: ${filePath}`);
} else if (path.extname(obj.path) === '.html') {
console.log('.html change detected.');
const filePath = path.relative(path.resolve('./'), obj.path);
if (obj.path.indexOf('layout') > -1) {
gulp.src([
`${examplesDev}/**/*.html`
])
.pipe($.plumber())
.pipe($.swig(swigParameters))
.pipe(
gulp.dest(
path.join(
path.relative(path.resolve('./'), path.resolve(examplesDest)),
path.relative(path.resolve(examplesDev), path.dirname(obj.path))
)
)
);
console.log(`Swig LAYOUT: ${filePath}`);
} else {
gulp.src(filePath)
.pipe($.plumber())
.pipe($.swig(swigParameters))
.pipe(
gulp.dest(
path.join(
path.relative(path.resolve('./'), path.resolve(examplesDest)),
path.relative(path.resolve(examplesDev), path.dirname(obj.path))
)
)
);
}
console.log(`Swig: ${filePath}`);
} else {
console.log('Other file change detected.');
const filePath = path.relative(path.resolve('./'), obj.path);
gulp.src([
filePath,
`!${examplesDev}/**/*.html`,
`!${examplesDev}/!(_libs)/*.js`,
`!${examplesDev}/**/script.js`,
`${examplesDev}/_assets/**/*.js`
])
.pipe($.plumber())
.pipe(
gulp.dest(
path.join(
path.relative(path.resolve('./'), path.resolve(examplesDest)),
path.relative(path.resolve(examplesDev), path.dirname(obj.path))
)
)
);
console.log(`File copied: ${filePath}`);
}
}
});
watcher.on('change', (event) => {
if (event.type === 'deleted') {
// Simulating the {base: 'src'} used with gulp.src in the scripts task
const filePathFromSrc = path.relative(path.resolve(examplesDev), event.path);
// Concatenating the 'build' absolute path used by gulp.dest in the scripts task
const destFilePath = path.resolve(examplesDest, filePathFromSrc);
del.sync(destFilePath);
}
});
});
// EXAMPLES: BUILD
gulp.task('examples:build', ['examples:clean'], () => {
gulp.src([
`${examplesDev}/**/*`,
`!${examplesDev}/**/*.html`,
`!${examplesDev}/!(_libs)/*.js`,
`!${examplesDev}/**/script.js`,
`${examplesDev}/_assets/**/*.js`
])
.pipe($.plumber())
.pipe(gulp.dest(examplesDest));
gulp.src([
`${examplesDev}/**/*.js`,
`!${examplesDev}/**/*.html`,
`!${examplesDev}/_assets/**/*.js`
])
.pipe($.plumber())
.pipe($.swig(Object.assign({}, swigParameters, {ext: '.js'})))
.pipe($.gbrowser.browserify({basedir: path.resolve(examplesDev)}, {
transform: 'babelify',
options: {presets: ['es2015']}
}))
.pipe(gulp.dest(examplesDest));
gulp.src(`${examplesDev}/**/*.html`)
.pipe($.plumber())
.pipe($.swig(swigParameters))
.pipe(gulp.dest(examplesDest));
});
// TESTING
gulp.task('test', ['test:benchmark', 'test:unit']);
gulp.task('test:benchmark', done => {
new karma.Server({
configFile: `${__dirname}/test/karma.benchmark.conf.js`
}, () => {
done();
}).start();
});
gulp.task('test:unit', done => {
new karma.Server({
configFile: `${__dirname}/test/karma.unit.conf.js`
}, () => {
done();
}).start();
});
// LINT
gulp.task('src:lint', () => {
gulp.src(`${frameworkSrc}/**/*`)
.pipe($.cached('lint', {optimizeMemory: true}))
.pipe($.xo())
.on('error', makeBuildErrorHandler('lint'));
});
// CLEANING
gulp.task('examples:clean', (callback) => {
del(examplesDest).then(() => callback());
});
gulp.task('build:clean', (callback) => {
del([`${frameworkDest}/*.js`, `${frameworkDest}/*.map`, './lib/**/*.js', './lib/**/*.map']).then(() => callback());
});
// ERRORS
function makeBuildErrorHandler(taskName) {
return function ({name, message, codeFrame}) {
$.util.log(`[${taskName}]`, `${$.util.colors.red(name)} ${message}${codeFrame ? `\n${codeFrame}` : ''}`);
this.emit('end');
};
}