-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
137 lines (116 loc) · 4.1 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
'use strict';
const gulp = require('gulp');
const runSequence = require('run-sequence');
const rimraf = require('gulp-rimraf');
const plumber = require('gulp-plumber');
const webserver = require('gulp-webserver');
const sourcemaps = require('gulp-sourcemaps');
const sweetjs = require('gulp-sweetjs');
const uglify = require('gulp-uglify');
const identity = require('gulp-identity');
const webpack = require('webpack-stream');
const babel = require('gulp-babel');
const print = require('gulp-print');
const changed = require('gulp-changed');
const glob = require('glob');
const argv = require('yargs').argv;
const path = require('path');
const fs = require('fs');
/* The build config file is config.json */
const CONFIG = JSON.parse(fs.readFileSync('config.json'));
/* Running 'gulp' with no argument starts a development environment */
gulp.task('default', (callback) => {
runSequence('clean', 'build', 'stream', 'serve', callback);
});
/* Watch the source directory, running development task after changes */
gulp.task('stream', () => {
return gulp.watch(`{${CONFIG.SOURCE_DIR},${CONFIG.STATIC_DIR},${CONFIG.MACRO_DIR}}/**`, ['compile', 'static']);
});
/* Compile code and run build scripts, leaving result in stage directory */
gulp.task('build', (callback) => {
runSequence(['static', 'images', 'compile'], callback);
});
/* Copy static files to output directory */
gulp.task('static', () => {
return gulp.src(`${CONFIG.STATIC_DIR}/**`)
.pipe(gulp.dest(CONFIG.OUTPUT_DIR));
});
/* Copy images to output directory */
gulp.task('images', () => {
return gulp.src(`${CONFIG.IMAGE_DIR}/**`)
.pipe(gulp.dest(path.join(CONFIG.OUTPUT_DIR, CONFIG.IMAGE_DIR)));
});
/* Turn multiple files of ES6 into a single js file */
gulp.task('compile', (callback) => {
runSequence('transpile', 'resolve', callback);
});
/* Compile ES6 to javascript */
gulp.task('transpile', () => {
const babelStep = () => babel({
plugins: ["transform-async-to-generator"],
presets: ['es2015']
});
const sweetjsStep = () => sweetjs({
modules: glob.sync(`./${CONFIG.MACRO_DIR}/**/*.js`),
sourceMap: true
});
const minify = CONFIG.DEBUG ? identity() : uglify();
let src = `${CONFIG.SOURCE_DIR}/**/*.js`;
let dest = path.join(CONFIG.STAGE_DIR);
return gulp.src(src)
.pipe(plumber({
handleError: (err) => {
console.log(err.toString())
this.emit('end')
}
}))
.pipe(changed(dest))
.pipe(sourcemaps.init())
.pipe(print())
.pipe(babelStep()) // turn es6 into es5, so the macro engine doesn't see es6
.pipe(sweetjsStep()) // resolve macros on resulting es5
.pipe(babelStep()) // turn es6 produced by macros into es5
.pipe(minify) // minify each file
.pipe(sourcemaps.write())
.pipe(gulp.dest(dest))
});
/* Resolve commonjs dependencies produced by babel */
gulp.task('resolve', () => {
return gulp.src(path.join(CONFIG.STAGE_DIR, CONFIG.ENTRY_FILE))
.pipe(webpack({
devtool: '#source-map',
module: {
preLoaders: [
{
test: /\.js$/,
loader: 'source-map-loader'
}
]
},
output: {
filename: CONFIG.OUTPUT_FILE
},
resolve: {
root: [
path.resolve(CONFIG.STAGE_DIR)
]
}
}))
.pipe(gulp.dest(path.join(CONFIG.OUTPUT_DIR, CONFIG.COMPILED_DIR)));
});
/* Start a webserver in the output directory */
gulp.task('serve', () => {
const port = argv.port === undefined ? CONFIG.DEFAULT_SERVER_PORT : parseInt(argv.port);
return gulp.src(CONFIG.OUTPUT_DIR)
.pipe(webserver({
host: '0.0.0.0',
port: port,
fallback: CONFIG.INDEX_FILE,
livereload: false
}));
});
/* Deletes the output directory */
gulp.task('clean', () => {
return gulp.src(`{${CONFIG.OUTPUT_DIR},${CONFIG.STAGE_DIR}}`)
.pipe(rimraf());
});