-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
348 lines (306 loc) · 12.2 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
const cleanCSS = require('gulp-clean-css');
const closureBuilder = require('closure-builder');
const closureCompiler = require('gulp-closure-compiler');
const concatCSS = require('gulp-concat-css');
const cssInlineImages = require('gulp-css-inline-images');
const connect = require('gulp-connect');
const fse = require('fs-extra');
const flip = require('gulp-css-flip');
const gulp = require('gulp');
const path = require('path');
const sass = require('gulp-sass');
const streamqueue = require('streamqueue');
const util = require('gulp-util');
const glob = closureBuilder.globSupport();
// The optimization level for the JS compiler.
// Valid levels: WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS.
// This can be passed in as a flag:
// $ gulp --compilation_level=WHITESPACE_ONLY
const OPTIMIZATION_LEVEL = util.env.compilation_level ||
'ADVANCED_OPTIMIZATIONS';
// For minified builds, wrap the output so we avoid leaking global variables.
const OUTPUT_WRAPPER = OPTIMIZATION_LEVEL === 'WHITESPACE_ONLY' ?
'%output%' : '(function() { %output% })();';
// Provides missing dialogPolyfill on window in npm environments.
const DIALOG_POLYFILL = 'if(typeof window!==\'undefined\')' +
'{window.dialogPolyfill=require(\'dialog-polyfill\');}';
// Using default import if available.
const DEFAULT_IMPORT_FIX = 'if(typeof firebase.default!==\'undefined\')' +
'{firebase=firebase.default;}';
// Adds the module requirement and exports firebaseui.
const NPM_MODULE_WRAPPER = OPTIMIZATION_LEVEL === 'WHITESPACE_ONLY' ?
'var firebase=require(\'firebase/app\');require(\'firebase/auth\');' +
DEFAULT_IMPORT_FIX + '%output%' + DIALOG_POLYFILL +
'module.exports=firebaseui;' :
'(function() { var firebase=require(\'firebase/app\');' +
'require(\'firebase/auth\');' + DEFAULT_IMPORT_FIX + '%output% ' +
DIALOG_POLYFILL + '})();' + 'module.exports=firebaseui;';
// The path to Closure Compiler.
const COMPILER_PATH = 'node_modules/google-closure-compiler-java/compiler.jar';
// The path to the temporary directory where intermediate results are stored.
const TMP_DIR = 'out';
// The path to the temporary directory where intermediate results are stored.
const DEST_DIR = 'dist';
// The locale that would be produced with no XTBs.
const DEFAULT_LOCALE = 'en';
// The list of all locales that are supported.
const ALL_LOCALES = ['ar-XB', 'ar', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en',
'en-GB', 'en-XA', 'es-419', 'es', 'fa', 'fi', 'fil', 'fr', 'hi', 'hr', 'hu',
'id', 'it', 'iw', 'ja', 'ko', 'lt', 'lv', 'nl', 'no', 'pl', 'pt-PT',
'pt-BR', 'ro', 'ru', 'sk', 'sl', 'sr', 'sv', 'th', 'tr', 'uk', 'vi',
'zh-CN', 'zh-TW'];
// Default arguments to pass into Closure Compiler.
const COMPILER_DEFAULT_ARGS = {
compilation_level: OPTIMIZATION_LEVEL,
language_out: 'ES5'
};
// The external dependencies needed by FirebaseUI.
const JS_DEPS = [
'node_modules/material-design-lite/src/mdlComponentHandler.js',
'node_modules/material-design-lite/src/button/button.js',
'node_modules/material-design-lite/src/progress/progress.js',
'node_modules/material-design-lite/src/spinner/spinner.js',
'node_modules/material-design-lite/src/textfield/textfield.js',
'node_modules/dialog-polyfill/dialog-polyfill.js'
];
// The typescript definitions file path.
const TYPES_FILE = './types/index.d.ts';
// The externs directory files.
const EXTERNS_FILES = './externs/*.js';
// Compiles the Closure templates into JavaScript.
gulp.task('build-soy', () => new Promise((resolve, reject) => {
closureBuilder.build({
name: 'soy_files',
srcs: glob([
'soy/*.soy'
]),
out: TMP_DIR,
options: {
soy: {
shouldGenerateGoogMsgDefs: true,
bidiGlobalDir: 1
}
},
}, resolve);
}));
/**
* Invokes Closure Compiler.
* @param {!Array<string>} srcs The JS sources to compile.
* @param {string} out The path to the output JS file.
* @param {!Object} args Additional arguments to Closure compiler.
* @return {*} A stream that finishes when compliation finishes.
*/
function compile(srcs, out, args) {
// Get the compiler arguments, using the defaults if not specified.
const combinedArgs = Object.assign({}, COMPILER_DEFAULT_ARGS, args);
return gulp
.src(srcs)
.pipe(closureCompiler({
compilerPath: COMPILER_PATH,
fileName: path.basename(out),
compilerFlags: combinedArgs
}))
.pipe(gulp.dest(path.dirname(out)));
}
/**
* Normalizes a locale ID for use in a file name (e.g. en-GB -> en_gb).
* @param {string} locale
* @return {string} The normalized locale ID.
*/
function getLocaleForFileName(locale) {
return locale.toLowerCase().replace(/-/g, '_');
}
/**
* Gets the path to the temporary JS file that contains all FirebaseUI code
* but no external dependencies.
* @param {string} locale
* @return {string} The path of the temporary JS file.
*/
function getTmpJsPath(locale) {
const localeForFileName = getLocaleForFileName(locale);
return `${TMP_DIR}/firebaseui__${localeForFileName}.js`;
}
/**
* Repeats a gulp task for all locales.
* @param {string} taskName The gulp task name to generate. Any $ tokens will be
* replaced with the language code (e.g. build-$ becomes build-fr, build-es,
* etc.).
* @param {!Array<string>} dependencies The gulp tasks that each operation
* depends on. Any $ tokens will be replaced with the language code.
* @param {function()} operation The function to execute.
* @return {!Array<string>} The list of generated task names.
*/
function repeatTaskForAllLocales(taskName, dependencies, operation) {
return ALL_LOCALES.map((locale) => {
// Convert build-js-$ to build-js-fr, for example.
const replaceTokens = (name) => name.replace(/\$/g, locale);
const localeTaskName = replaceTokens(taskName);
const localeDependencies = dependencies.map(replaceTokens);
gulp.task(localeTaskName, gulp.series(
gulp.parallel.apply(null, localeDependencies),
() => operation(locale)
));
return localeTaskName;
});
}
/**
* Builds the core FirebaseUI binary in the given locale.
* @param {string} locale
* @return {*} A stream that finishes when compilation finishes.
*/
function buildFirebaseUiJs(locale) {
const flags = {
closure_entry_point: 'firebaseui.auth.exports',
define: `goog.LOCALE='${locale}'`,
externs: [
'node_modules/firebase/externs/firebase-app-externs.js',
'node_modules/firebase/externs/firebase-auth-externs.js',
'node_modules/firebase/externs/firebase-client-auth-externs.js'
],
only_closure_dependencies: true,
output_wrapper: OUTPUT_WRAPPER,
// This is required to match XTB IDs to the JS/Soy messages.
translations_project: 'FirebaseUI'
};
if (locale !== DEFAULT_LOCALE) {
flags.translations_file = `translations/${locale}.xtb`;
}
return compile([
'node_modules/google-closure-templates/javascript/soyutils_usegoog.js',
'node_modules/google-closure-library/closure/goog/**/*.js',
'node_modules/google-closure-library/third_party/closure/goog/**/*.js',
`${TMP_DIR}/**/*.js`,
'javascript/**/*.js'
], getTmpJsPath(locale), flags);
}
/**
* Concatenates the core FirebaseUI JS with its external dependencies, and
* cleans up comments and whitespace in the dependencies.
* @param {string} locale The desired FirebaseUI locale.
* @param {string} outBaseName The prefix of the output file name.
* @param {string} outputWrapper A wrapper with which to wrap the output JS.
* @return {*} A stream that ends when compilation finishes.
*/
function concatWithDeps(locale, outBaseName, outputWrapper) {
const localeForFileName = getLocaleForFileName(locale);
// Get a list of the FirebaseUI JS and its dependencies.
const srcs = JS_DEPS.concat([getTmpJsPath(locale)]);
const outputPath = `${DEST_DIR}/${outBaseName}__${localeForFileName}.js`;
return compile(srcs, outputPath, {
compilation_level: 'WHITESPACE_ONLY',
output_wrapper: outputWrapper
});
}
/**
* Creates the default FirebaseUI binaries for basic usage without
* localization. For example, it copies firebaseui__en.js to firebaseui.js.
* @param {string} fileName
* @return {!Promise} A promise that resolves on completion.
*/
function makeDefaultFile(fileName) {
const localeForFileName = getLocaleForFileName(DEFAULT_LOCALE);
const path = `${DEST_DIR}/${fileName}__${localeForFileName}.js`;
if (fse.existsSync(path)) {
return fse.copy(path, `${DEST_DIR}/${fileName}.js`);
}
}
// Generates the typescript definitions.
gulp.task('build-ts',
() => gulp.src(TYPES_FILE).pipe(gulp.dest(`${DEST_DIR}/`)));
// Generates the externs definitions.
gulp.task('build-externs',
() => gulp.src(EXTERNS_FILES).pipe(gulp.dest(`${DEST_DIR}/externs/`)));
// Builds the core FirebaseUI JS. Generates the gulp tasks
// build-firebaseui-js-de, build-firebaseui-js-fr, etc.
repeatTaskForAllLocales(
'build-firebaseui-js-$',
['build-externs', 'build-ts', 'build-soy'],
buildFirebaseUiJs
);
// Bundles the FirebaseUI JS with its dependencies as a NPM module. This builds
// the NPM module for all languages.
repeatTaskForAllLocales(
'build-npm-$', ['build-firebaseui-js-$'],
(locale) => concatWithDeps(locale, 'npm', NPM_MODULE_WRAPPER));
// Bundles the FirebaseUI JS with its dependencies for all locales.
// Generates the gulp tasks build-js-de, build-js-fr, etc.
const buildJsTasks = repeatTaskForAllLocales(
'build-js-$', ['build-firebaseui-js-$'],
(locale) => concatWithDeps(locale, 'firebaseui', OUTPUT_WRAPPER));
// Builds the final JS file for the default language.
gulp.task('build-js', gulp.series(
'build-js-' + DEFAULT_LOCALE,
() => makeDefaultFile('firebaseui')
));
// Builds the final JS file for all supported languages.
gulp.task('build-all-js', gulp.series(
gulp.parallel.apply(null, buildJsTasks),
() => makeDefaultFile('firebaseui')
));
// Builds the NPM module for the default language.
gulp.task('build-npm', gulp.series(
'build-npm-' + DEFAULT_LOCALE,
() => makeDefaultFile('npm')
));
/**
* Builds the CSS for FirebaseUI.
* @param {boolean} isRtl Whether to build in right-to-left mode.
* @return {*} A stream that finishes when compilation finishes.
*/
function buildCss(isRtl) {
const mdlSrcs = gulp.src('stylesheet/mdl.scss')
.pipe(sass.sync().on('error', sass.logError))
.pipe(cssInlineImages({
webRoot: 'node_modules/material-design-lite/src',
}));
const dialogPolyfillSrcs = gulp.src(
'node_modules/dialog-polyfill/dialog-polyfill.css');
let firebaseSrcs = gulp.src('stylesheet/*.css');
// Flip left/right, ltr/rtl for RTL languages.
if (isRtl) {
firebaseSrcs = firebaseSrcs.pipe(flip.gulp());
}
const outFile = isRtl ? 'firebaseui-rtl.css' : 'firebaseui.css';
return streamqueue({objectMode: true},
mdlSrcs, dialogPolyfillSrcs, firebaseSrcs)
.pipe(concatCSS(outFile))
.pipe(cleanCSS())
.pipe(gulp.dest(DEST_DIR));
}
// Concatenates and minifies the CSS sources for LTR languages.
gulp.task('build-css', () => buildCss(false));
// Concatenates and minifies the CSS sources for RTL languages.
gulp.task('build-css-rtl', () => buildCss(true));
// Creates a webserver that serves all files from the root of the package.
gulp.task('serve', () => {
connect.server({
port: 4000
});
});
// Deletes intermediate files.
gulp.task('clean', () => fse.remove(TMP_DIR));
// Executes the basic tasks for the default language.
gulp.task('default', gulp.series(
'build-externs', 'build-ts', 'build-js',
'build-npm', 'build-css', 'build-css-rtl',
'clean'
));
// Builds everything (JS for all languages, both LTR and RTL CSS).
gulp.task('build-all', gulp.series(
'build-externs', 'build-ts', 'build-all-js',
'build-npm', 'build-css', 'build-css-rtl',
'clean'
));