This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
249 lines (213 loc) · 7.98 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
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
"use strict";
var os = require('os');
var fs = require('fs');
var path = require('path');
var isarray = require('isarray');
var glob = require('glob');
// Build Inputs
var inputs = {
templates: {
sources: {
root: 'views',
html: '**/*.html',
scss: '**/*.scss',
scssCatalog: '_views.scss',
},
},
scripts: {
sources: '**/*.ts',
project: 'tsconfig.json'
},
styles: {
sources: 'scss/**/*.scss',
normalize: 'node_modules/normalize.css/normalize.css'
},
};
// Build Outputs
var outputs = {
temp: ".build",
root: "dist",
templates: 'templates.html',
scripts: 'js',
styles: 'css',
fonts: 'fonts',
images: 'img',
sounds: 'sounds'
};
// Static Content
var content = [
{ source: [ '*.html', 'LICENSE' ]},
{ source: 'images/**/*', destination: outputs.images },
{ source: 'sounds/**/*.ogg', destination: outputs.sounds },
{ source: 'sounds/**/*.m4a', destination: outputs.sounds },
{ source: 'node_modules/font-awesome/fonts/*.*', destination: outputs.fonts },
{ source: [ 'node_modules/jquery/dist/jquery.js', 'node_modules/jquery/dist/jquery.min.*' ], destination: outputs.scripts },
{ source: 'wgo.js/*.js', destination: outputs.scripts },
]
// Supported Browsers and Versions
// See CSV data from: gs.statcounter.com
var browsers = [
'Chrome >= 42',
'Firefox >= 38', // Firefox Extended Support Release (ESR) on 2015-12-22
'Edge >= 12', // EdgeHTML rendering engine version (not the Edge app. version)
'Explorer >= 10',
'iOS >= 8',
'Safari >= 9',
'Opera >= 15'
];
// Gulp: The streaming build system
var gulp = require('gulp');
var rimraf = require('rimraf');
var merge = require('merge-stream');
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');
// Tool-Chain: Scripts
var typescript = require('gulp-typescript');
// Tool-Chain: Styles
var sass = require('gulp-sass');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
// Helper Functions
var multisource = function(p) {
if ((p) && (isarray(p))) {
let sources = [];
for (let j = 0; j < p.length; ++j) {
sources.push(gulp.src(p[j]));
}
return merge(sources);
}
else if (p) return gulp.src(p);
else return null;
}
var multicopy = function(source, destination) {
destination = (destination)? path.join(outputs.root, destination) : outputs.root;
return multisource(source).pipe(gulp.dest(destination));
}
// Task(s): Clean
gulp.task('clean:templates', function(callback) {
rimraf(path.join(outputs.root, outputs.templates), callback);
});
gulp.task('clean:scripts', function(callback) {
rimraf(path.join(outputs.root, outputs.scripts), callback);
});
gulp.task('clean:tests', function(callback) {
rimraf(path.join(outputs.temp, "tests*"), callback);
});
gulp.task('clean:styles', function(callback) {
rimraf(path.join(outputs.root, outputs.styles), () => {
rimraf(path.join(outputs.temp, inputs.templates.sources.scssCatalog), callback);
});
});
gulp.task('clean:fonts', function(callback) {
rimraf(path.join(outputs.root, outputs.fonts), callback);
});
gulp.task('clean:images', function(callback) {
rimraf(path.join(outputs.root, outputs.images), callback);
});
gulp.task('clean:sounds', function(callback) {
rimraf(path.join(outputs.root, outputs.sounds), callback);
});
gulp.task('clean', function(callback) {
rimraf(outputs.temp, () => {
rimraf(outputs.root, callback);
});
});
// Task(s): Build View Templates
gulp.task('build:templates', function() {
return gulp.src([path.join(inputs.templates.sources.root, inputs.templates.sources.html)])
.pipe(concat(outputs.templates))
.pipe(gulp.dest(outputs.root));
});
// Task(s): Build TypeScript Outputs
var initialiseTypeScriptProject = function(excludeTests) {
let settings;
if (!excludeTests) {
settings = { out: "tests.js" };
}
let tsconfig = typescript.createProject(inputs.scripts.project, settings);
if (excludeTests) {
tsconfig.config.exclude.push("**/*.tests.ts");
}
return tsconfig;
}
var buildTypeScriptProject = function(tsconfig, outputPath) {
let ts = tsconfig.src()
.pipe(sourcemaps.init())
.pipe(tsconfig());
return ts.pipe(sourcemaps.write(".", { sourceRoot: function (file) {
return path.relative(path.join(file.cwd, file.path), file.base);
}})).pipe(gulp.dest(outputPath));
}
gulp.task('build:scripts', buildTypeScriptProject.bind(undefined, initialiseTypeScriptProject(true), path.join(outputs.root, outputs.scripts)));
gulp.task('build:tests', buildTypeScriptProject.bind(undefined, initialiseTypeScriptProject(false), outputs.temp));
// Task(s): Build Sass Outputs
gulp.task('build:styles:normalize', function() {
return gulp.src(inputs.styles.normalize)
.pipe(gulp.dest(path.join(outputs.root, outputs.styles)));
});
gulp.task('build:styles:views', function(callback) {
let globOptions = {
cwd: path.resolve(inputs.templates.sources.root),
nodir: true,
nosort: true
};
glob(inputs.templates.sources.scss, globOptions, function (error, matches) {
let fileStreamOptions = {
flags: 'w',
defaultEncoding: 'utf8',
autoClose: true
};
let catalogFilename = path.join(outputs.temp, inputs.templates.sources.scssCatalog);
if (!fs.existsSync(path.dirname(catalogFilename))) fs.mkdirSync(path.dirname(catalogFilename));
let fileStream = fs.createWriteStream(catalogFilename, fileStreamOptions);
let eol = os.EOL;
fileStream.write("// View SASS Fragments...");
fileStream.write(eol);
fileStream.write(eol);
if (matches) {
let extensionLength = path.extname(inputs.templates.sources.scss).length;
for (let j = 0; j < matches.length; ++j) {
let sassFilename = matches[j];
sassFilename = sassFilename.substring(0, sassFilename.length - extensionLength);
fileStream.write("@import \"");
fileStream.write(path.relative(path.dirname(catalogFilename), path.join(globOptions.cwd, sassFilename)).replace(/\\/g, "/"));
fileStream.write("\";");
fileStream.write(eol);
}
}
fileStream.end();
callback();
})
});
gulp.task('build:styles', ['build:styles:normalize', 'build:styles:views'], function() {
return gulp.src(inputs.styles.sources)
.pipe(sourcemaps.init())
.pipe(sass({ includePaths: [] }).on('error', sass.logError))
.pipe(postcss([autoprefixer({ browsers: browsers })]))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(path.join(outputs.root, outputs.styles)));
});
// Task(s): Copy Static Content to Output
gulp.task('build:content', function() {
let streams = [];
for (let j = 0; j < content.length; ++j) {
streams.push(multicopy(content[j].source, content[j].destination));
}
return merge(streams);
});
// Task(s): Build et al.
gulp.task('build', ['build:templates', 'build:scripts', 'build:styles', 'build:content']);
gulp.task('default', ['build']);
gulp.task('rebuild', ['clean'], function() { gulp.start('build') });
// Task(s): Incremental Compilation
var watch = function() {
gulp.watch(path.join(inputs.templates.sources.root, inputs.templates.sources.html), ['build:templates']);
gulp.watch(inputs.scripts.sources, ['build:scripts']);
gulp.watch(path.join(inputs.templates.sources.root, inputs.templates.sources.scss), ['build:styles']);
gulp.watch(inputs.styles.sources, ['build:styles']);
for (let j = 0; j < content.length; ++j) {
gulp.watch(content[j].source, multicopy.bind(null, content[j].source, content[j].destination));
}
};
gulp.task('watch', ['build'], watch);
gulp.task('rewatch', ['rebuild'], watch);