|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +const gulp = require('gulp'); |
| 19 | +const rollup = require('rollup'); |
| 20 | +const closureCompiler = require('google-closure-compiler').gulp(); |
| 21 | +const del = require('del'); |
| 22 | +const path = require('path'); |
| 23 | +const sourcemaps = require('gulp-sourcemaps'); |
| 24 | +const { resolve } = require('path'); |
| 25 | +const commonjs = require('rollup-plugin-commonjs'); |
| 26 | +const rollupSourcemaps = require('rollup-plugin-sourcemaps'); |
| 27 | + |
| 28 | +// The optimization level for the JS compiler. |
| 29 | +// Valid levels: WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS. |
| 30 | +// TODO: Add ability to pass this in as a flag. |
| 31 | +const OPTIMIZATION_LEVEL = 'ADVANCED_OPTIMIZATIONS'; |
| 32 | + |
| 33 | +// For minified builds, wrap the output so we avoid leaking global variables. |
| 34 | +// For minified builds, wrap the output so we avoid leaking global variables. |
| 35 | +const CJS_WRAPPER_PREFIX = `(function() {var firebase = require('@firebase/app').default;`; |
| 36 | +const CJS_WRAPPER_SUFFIX = |
| 37 | + `}).apply(typeof global !== 'undefined' ? ` + |
| 38 | + `global : typeof self !== 'undefined' ? ` + |
| 39 | + `self : typeof window !== 'undefined' ? window : {});`; |
| 40 | + |
| 41 | +const closureLibRoot = path.dirname( |
| 42 | + require.resolve('google-closure-library/package.json') |
| 43 | +); |
| 44 | + |
| 45 | +const closureDefines = [ |
| 46 | + // Avoid unsafe eval() calls (https://github.com/firebase/firebase-js-sdk/issues/798) |
| 47 | + 'goog.json.USE_NATIVE_JSON=true', |
| 48 | + // Disable debug logging (saves 8780 bytes). |
| 49 | + 'goog.DEBUG=false', |
| 50 | + // Disable fallbacks for running async code (saves 1472 bytes). |
| 51 | + 'goog.ASSUME_NATIVE_PROMISE=true', |
| 52 | + // Disables IE8-specific event fallback code (saves 523 bytes). |
| 53 | + 'goog.events.CAPTURE_SIMULATION_MODE=0', |
| 54 | + // Disable IE-Specific ActiveX fallback for XHRs (saves 524 bytes). |
| 55 | + 'goog.net.XmlHttpDefines.ASSUME_NATIVE_XHR=true' |
| 56 | +]; |
| 57 | + |
| 58 | +/** |
| 59 | + * Builds the core Firebase-auth JS. |
| 60 | + * @param {string} filename name of the generated file |
| 61 | + * @param {string} prefix prefix to the compiled code |
| 62 | + * @param {string} suffix suffix to the compiled code |
| 63 | + */ |
| 64 | +function createBuildTask(filename, prefix, suffix) { |
| 65 | + return function closureBuild () { |
| 66 | + return gulp |
| 67 | + .src( |
| 68 | + [ |
| 69 | + `${closureLibRoot}/closure/goog/**/*.js`, |
| 70 | + `${closureLibRoot}/third_party/closure/goog/**/*.js`, |
| 71 | + 'src/**/*.js' |
| 72 | + ], |
| 73 | + { base: '.' } |
| 74 | + ) |
| 75 | + .pipe(sourcemaps.init()) |
| 76 | + .pipe( |
| 77 | + closureCompiler({ |
| 78 | + js_output_file: filename, |
| 79 | + output_wrapper: `${prefix}%output%${suffix}`, |
| 80 | + entry_point: 'firebase.webchannel.wrapper', |
| 81 | + compilation_level: OPTIMIZATION_LEVEL, |
| 82 | + externs: [ |
| 83 | + resolve(__dirname, './externs/overrides.js'), |
| 84 | + resolve(__dirname, './externs/module.js') |
| 85 | + ], |
| 86 | + language_out: 'ES5', |
| 87 | + dependency_mode: 'PRUNE', |
| 88 | + define: closureDefines |
| 89 | + // process_common_js_modules: true, |
| 90 | + // module_resolution: 'NODE' |
| 91 | + }) |
| 92 | + ) |
| 93 | + .pipe(sourcemaps.write('.')) |
| 94 | + .pipe(gulp.dest('dist')); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +function createRollupTask(inputPath) { |
| 99 | + return async function rollupBuild() { |
| 100 | + console.log('inputPath', inputPath); |
| 101 | + const inputOptions = { |
| 102 | + input: inputPath, |
| 103 | + plugins: [rollupSourcemaps(), commonjs()] |
| 104 | + }; |
| 105 | + |
| 106 | + const outputOptions = { |
| 107 | + file: 'dist/index.esm.js', |
| 108 | + format: 'es', |
| 109 | + sourcemap: true |
| 110 | + }; |
| 111 | + |
| 112 | + const bundle = await rollup.rollup(inputOptions); |
| 113 | + return bundle.write(outputOptions); |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +async function deleteIntermediateFiles() { |
| 118 | + await del('dist/temp'); |
| 119 | +} |
| 120 | + |
| 121 | +// commonjs build |
| 122 | +const cjsBuild = createBuildTask( |
| 123 | + 'index.js', |
| 124 | + CJS_WRAPPER_PREFIX, |
| 125 | + CJS_WRAPPER_SUFFIX |
| 126 | +); |
| 127 | +gulp.task('cjs', cjsBuild); |
| 128 | + |
| 129 | +// esm build |
| 130 | +const intermediateEsmFile = 'temp/esm.js'; |
| 131 | +const intermediateEsmPath = resolve(__dirname, 'dist/', intermediateEsmFile); |
| 132 | +const esmBuild = createBuildTask( |
| 133 | + intermediateEsmFile, '', '' |
| 134 | +); |
| 135 | +const rollupTask = createRollupTask(intermediateEsmPath); |
| 136 | +gulp.task( |
| 137 | + 'esm', |
| 138 | + gulp.series(esmBuild, rollupTask, deleteIntermediateFiles) |
| 139 | +); |
| 140 | + |
| 141 | +// Deletes intermediate files. |
| 142 | +gulp.task('clean', done => del([resolve(__dirname, intermediateEsmFile)], done)); |
| 143 | + |
| 144 | +gulp.task('default', gulp.parallel('cjs', 'esm')); |
0 commit comments