|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2020 Google LLC |
| 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 | +const typescriptPlugin = require('rollup-plugin-typescript2'); |
| 28 | +const typescript = require('typescript'); |
| 29 | + |
| 30 | +// The optimization level for the JS compiler. |
| 31 | +// Valid levels: WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS. |
| 32 | +const OPTIMIZATION_LEVEL = 'ADVANCED_OPTIMIZATIONS'; |
| 33 | + |
| 34 | +// For minified builds, wrap the output so we avoid leaking global variables. |
| 35 | +const CJS_WRAPPER_PREFIX = `(function() {`; |
| 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 | + * Generates a closure compiler build of webchannel-wrapper. |
| 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: 'ECMASCRIPT_2017', |
| 87 | + dependency_mode: 'PRUNE', |
| 88 | + define: closureDefines |
| 89 | + }) |
| 90 | + ) |
| 91 | + .pipe(sourcemaps.write('.')) |
| 92 | + .pipe(gulp.dest('dist')); |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +function createRollupTask({ |
| 97 | + inputPath, |
| 98 | + outputExtension, |
| 99 | + compileToES5 = false, |
| 100 | + format = 'es' |
| 101 | +}) { |
| 102 | + return async function rollupBuild() { |
| 103 | + const plugins = [rollupSourcemaps(), commonjs()]; |
| 104 | + if (compileToES5) { |
| 105 | + plugins.push( |
| 106 | + typescriptPlugin({ |
| 107 | + typescript, |
| 108 | + // Uncomment for development only. Prevents caching between builds. |
| 109 | + // clean: true, |
| 110 | + compilerOptions: { allowJs: true }, |
| 111 | + include: ['dist/**/*.js'] |
| 112 | + }) |
| 113 | + ); |
| 114 | + } |
| 115 | + const inputOptions = { |
| 116 | + input: inputPath, |
| 117 | + plugins |
| 118 | + }; |
| 119 | + |
| 120 | + const outputOptions = { |
| 121 | + file: `dist/index${outputExtension ? '.' : ''}${outputExtension}.js`, |
| 122 | + format, |
| 123 | + sourcemap: true, |
| 124 | + // Prevents warning when compiling CJS that there are named and default exports together. |
| 125 | + exports: 'named' |
| 126 | + }; |
| 127 | + |
| 128 | + const bundle = await rollup.rollup(inputOptions); |
| 129 | + return bundle.write(outputOptions); |
| 130 | + }; |
| 131 | +} |
| 132 | + |
| 133 | +async function deleteIntermediateFiles() { |
| 134 | + await del('dist/temp'); |
| 135 | +} |
| 136 | + |
| 137 | +// Closure-generated ES2017 intermediate file (CJS format) |
| 138 | +const intermediateCjsFile = 'temp/cjs.js'; |
| 139 | +const intermediateCjsPath = resolve(__dirname, 'dist/', intermediateCjsFile); |
| 140 | +const cjsBuild = createBuildTask( |
| 141 | + intermediateCjsFile, |
| 142 | + CJS_WRAPPER_PREFIX, |
| 143 | + CJS_WRAPPER_SUFFIX |
| 144 | +); |
| 145 | + |
| 146 | +// Closure-generated ES2017 intermediate file (no wrapper text) |
| 147 | +const intermediateEsmFile = 'temp/esm.js'; |
| 148 | +const intermediateEsmPath = resolve(__dirname, 'dist/', intermediateEsmFile); |
| 149 | +const esmBuild = createBuildTask(intermediateEsmFile, '', ''); |
| 150 | + |
| 151 | +// cjs output |
| 152 | +const rollupCjsTask = createRollupTask({ |
| 153 | + inputPath: intermediateCjsPath, |
| 154 | + outputExtension: '', |
| 155 | + compileToES5: true, |
| 156 | + format: 'cjs' |
| 157 | +}); |
| 158 | +gulp.task('cjs', gulp.series(cjsBuild, rollupCjsTask)); |
| 159 | + |
| 160 | +// esm intermediateEsmPath |
| 161 | +const rollupEsmTask = createRollupTask({ |
| 162 | + inputPath: intermediateEsmPath, |
| 163 | + outputExtension: 'esm', |
| 164 | + compileToES5: true, |
| 165 | + format: 'es' |
| 166 | +}); |
| 167 | +gulp.task('esm', gulp.series(esmBuild, rollupEsmTask)); |
| 168 | + |
| 169 | +// esm2017 output |
| 170 | +const rollup2017Task = createRollupTask({ |
| 171 | + inputPath: intermediateEsmPath, |
| 172 | + outputExtension: 'esm2017', |
| 173 | + compileToES5: false, |
| 174 | + format: 'es' |
| 175 | +}); |
| 176 | +gulp.task('esm2017', gulp.series(esmBuild, rollup2017Task)); |
| 177 | + |
| 178 | +gulp.task( |
| 179 | + 'allEsm', |
| 180 | + gulp.series(esmBuild, gulp.parallel(rollupEsmTask, rollup2017Task)) |
| 181 | +); |
| 182 | + |
| 183 | +gulp.task('buildAll', gulp.parallel('cjs', 'allEsm')); |
| 184 | + |
| 185 | +gulp.task('default', gulp.series('buildAll', deleteIntermediateFiles)); |
0 commit comments