-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
171 lines (128 loc) · 5.4 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
'use strict';
const autoprefixer = require('autoprefixer');
const cprocess = require('child_process');
const del = require('del');
const dotenv = require('dotenv');
const fs = require('fs-extra');
const gulp = require('gulp');
const jspm = require('jspm');
const path = require('path');
const postcssModules = require('postcss-modules');
const precss = require('precss')
const babel = require('gulp-babel');
const concat = require('gulp-concat');
const cssnano = require('gulp-cssnano');
const postcss = require('gulp-postcss');
const preprocess = require('gulp-preprocess');
const replace = require('gulp-rev-replace');
const rev = require('gulp-rev');
const sourcemaps = require('gulp-sourcemaps');
const ts = require('gulp-typescript');
const typescript = require('typescript');
const uglify = require('gulp-uglify');
dotenv.load();
const buildTasks = gulp.parallel(buildServer, buildClient, buildCss, copyAssets);
const watchTasks = gulp.parallel(watchServer, watchClient, watchCss, watchAssets);
gulp.task('default', gulp.series(clean, buildTasks, gulp.parallel(startWeb, startWorker), watchTasks));
gulp.task('release', gulp.series(clean, buildTasks, gulp.parallel(bundleClient, minifyCss), revPublic, revPublic));
function clean() {
return del('dist');
}
// Start
var webProcess = null;
var workerProcess = null;
function startWeb(done) {
if (!webProcess) {
webProcess = cprocess.spawn('node', ['--harmony_destructuring', '--harmony_default_parameters', '--harmony_rest_parameters', 'bin/web'], { stdio: 'inherit' });
}
done();
}
function killWeb(done) {
if (webProcess) {
webProcess.once('close', done);
webProcess.kill();
}
webProcess = null;
}
function startWorker(done) {
if (!workerProcess) {
workerProcess = cprocess.spawn('node', ['--harmony_destructuring', '--harmony_default_parameters', '--harmony_rest_parameters', 'bin/worker'], { stdio: 'inherit' });
}
done();
}
function killWorker(done) {
if (workerProcess) {
workerProcess.once('close', done);
workerProcess.kill();
}
workerProcess = null;
}
// Server
const serverProject = ts.createProject('app/server/tsconfig.json', { typescript: typescript });
function buildServer() {
const source = ['{app/server,lib}/**/*.{ts,tsx}', 'typings/main.d.ts'];
const result = gulp.src(source).pipe(sourcemaps.init()).pipe(preprocess({ context: { NODE_BUILD: true }, includeBase: __dirname })).pipe(ts(serverProject));
return result.js.pipe(sourcemaps.write()).pipe(gulp.dest('dist/server'));
}
function watchServer() {
gulp.watch('{app/server,lib}/**/*.{ts,tsx,css}', gulp.series(gulp.parallel(buildServer, killWeb, killWorker), gulp.parallel(startWeb, startWorker)));
}
// Client
const clientProject = ts.createProject('app/client/tsconfig.json', { typescript: typescript });
function buildClient() {
const source = ['{app/client,lib}/**/*.{ts,tsx}', 'typings/browser.d.ts'];
const result = gulp.src(source).pipe(sourcemaps.init()).pipe(preprocess({ includeBase: __dirname })).pipe(ts(clientProject));
return result.js.pipe(babel({ presets: ['es2015'] })).pipe(sourcemaps.write()).pipe(gulp.dest('dist/client'));
}
function watchClient() {
gulp.watch('{app/client,lib}/**/*.{ts,tsx}', buildClient);
}
function bundleClient() {
return jspm.bundleSFX('babel-polyfill + dist/client/app/client/client', 'dist/public/app.js', { runtime: false, minify: true, sourceMaps: true});
}
// Public
function buildCss() {
const paths = (css, opts) => {
const STYLES_REGEX = '@styles';
const relativeStylesPath = `${path.relative(path.dirname(css.source.input.file), '.')}/lib/styles`;
css.walkAtRules('value', rule => {
rule.params = rule.params.replace(STYLES_REGEX, relativeStylesPath);
});
css.walkDecls('composes', decl => {
decl.value = decl.value.replace(STYLES_REGEX, relativeStylesPath);
});
};
const modules = postcssModules({
getJSON: (cssPath, json) => {
const relativeCssPath = path.relative('.', cssPath);
const resolveDestPath = (kind) => path.resolve('./', `${path.join(kind, relativeCssPath)}.json`);
if (relativeCssPath.startsWith('app/server')) {
fs.outputJsonSync(resolveDestPath('dist/server'), json);
}
if (relativeCssPath.startsWith('app/client')) {
fs.outputJsonSync(resolveDestPath('dist/client'), json);
}
if (relativeCssPath.startsWith('lib/components')) {
fs.outputJsonSync(resolveDestPath('dist/client'), json);
fs.outputJsonSync(resolveDestPath('dist/server'), json);
}
}
});
const cssMasks = ['{app/client,app/server,lib/components,public/css}/**/*.css', '!**/_*.css'];
return gulp.src(cssMasks).pipe(sourcemaps.init()).pipe(postcss([paths, precss, modules, autoprefixer])).pipe(sourcemaps.write()).pipe(gulp.dest('dist/public/css'));
}
function minifyCss() {
return gulp.src('dist/**/*.css').pipe(concat('app.css')).pipe(sourcemaps.init()).pipe(cssnano()).pipe(sourcemaps.write('.')).pipe(gulp.dest('dist/public'));
}
function watchCss() {
gulp.watch('{app,public,lib}/**/*.css', buildCss);
}
function copyAssets() {
return gulp.src('public/{images,fonts}/**/*.{jpg,png,woff,ico}').pipe(gulp.dest('dist/public'));
}
function watchAssets() {
gulp.watch('public/{images,fonts}/**/*.{jpg,png,woff,ico}', copyAssets);
}
function revPublic() {
return gulp.src('dist/public/**').pipe(rev()).pipe(gulp.dest('dist/public')).pipe(rev.manifest({ path: 'manifest.json' })).pipe(gulp.dest('dist/public'));
}