Skip to content

Commit 55f6e8d

Browse files
authored
Merge a05a9ab into 8c36958
2 parents 8c36958 + a05a9ab commit 55f6e8d

File tree

8 files changed

+270
-396
lines changed

8 files changed

+270
-396
lines changed

packages/webchannel-wrapper/.npmignore

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
/** @type {!Object} */
19+
const module = {};

packages/webchannel-wrapper/externs/overrides.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@ goog.net.WebChannel.Options.messageHeaders;
3030
/** @type {Object<string, string>|undefined} */
3131
goog.net.WebChannel.Options.initMessageHeaders;
3232

33-
/** @type {stringboolean|undefined} */
33+
/** @type {string|boolean|undefined} */
3434
goog.net.WebChannel.Options.messageContentType;
3535

3636
/** @type {Object<string, string>|undefined|undefined} */
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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));

packages/webchannel-wrapper/package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@
55
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
66
"main": "dist/index.js",
77
"module": "dist/index.esm.js",
8+
"esm2017": "dist/index.esm2017.js",
89
"files": [
910
"dist"
1011
],
1112
"scripts": {
1213
"dev": "watch 'yarn build' src",
13-
"build": "node tools/build.js",
14+
"build": "gulp",
1415
"prepare": "yarn build"
1516
},
1617
"license": "Apache-2.0",
1718
"devDependencies": {
18-
"closure-builder": "2.3.8",
19-
"google-closure-library": "20200224.0.0",
19+
"google-closure-compiler": "20200406.0.0",
20+
"google-closure-library": "20200406.0.0",
21+
"gulp": "4.0.2",
22+
"gulp-sourcemaps": "2.6.5",
2023
"rollup": "2.6.1",
2124
"rollup-plugin-commonjs": "10.1.0",
22-
"rollup-plugin-sourcemaps": "0.5.0"
25+
"rollup-plugin-sourcemaps": "0.5.0",
26+
"rollup-plugin-typescript2": "0.27.0",
27+
"typescript": "3.8.3"
2328
},
2429
"repository": {
2530
"directory": "packages/webchannel-wrapper",

packages/webchannel-wrapper/tools/build.js

Lines changed: 0 additions & 93 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../../config/tsconfig.base.json",
3+
"compilerOptions": {
4+
"declaration": false,
5+
"outDir": "dist",
6+
"downlevelIteration": true,
7+
}
8+
}

0 commit comments

Comments
 (0)