@@ -37,17 +37,32 @@ const OPTIMIZATION_LEVEL = util.env.compilation_level ||
3737
3838// For minified builds, wrap the output so we avoid leaking global variables.
3939const OUTPUT_WRAPPER = OPTIMIZATION_LEVEL === 'WHITESPACE_ONLY' ?
40- '%output%' : '(function() { %output% })();' ;
40+ '%output%' : '(function() { %output% }).apply(' +
41+ 'typeof global !== \'undefined\' ? global : typeof self !== \'undefined\' ' +
42+ '? self : window );' ;
4143
42- // Provides missing dialogPolyfill on window in npm environments.
44+ // Provides missing dialogPolyfill on window in cjs environments.
4345const DIALOG_POLYFILL = 'if(typeof window!==\'undefined\')' +
4446 '{window.dialogPolyfill=require(\'dialog-polyfill\');}' ;
4547
48+ // Provides missing dialogPolyfill on window for esm.
49+ const ESM_DIALOG_POLYFILL = 'if(typeof window!==\'undefined\')' +
50+ '{window.dialogPolyfill=dialogPolyfill;}' ;
51+
4652// Using default import if available.
4753const DEFAULT_IMPORT_FIX = 'if(typeof firebase.default!==\'undefined\')' +
4854 '{firebase=firebase.default;}' ;
4955
50- // Adds the module requirement and exports firebaseui.
56+ // Import esm modules.
57+ const ESM_IMPORT = 'import * as firebase from \'firebase/app\';' +
58+ 'import \'firebase/auth\';' +
59+ 'import dialogPolyfill from \'dialog-polyfill\';' ;
60+
61+ // Export firebaseui.auth module.
62+ const ESM_EXPORT = 'const auth = firebaseui.auth;' +
63+ 'export { auth } ;' ;
64+
65+ // Adds the cjs module requirement and exports firebaseui.
5166const NPM_MODULE_WRAPPER = OPTIMIZATION_LEVEL === 'WHITESPACE_ONLY' ?
5267 'var firebase=require(\'firebase/app\');require(\'firebase/auth\');' +
5368 DEFAULT_IMPORT_FIX + '%output%' + DIALOG_POLYFILL +
@@ -56,6 +71,16 @@ const NPM_MODULE_WRAPPER = OPTIMIZATION_LEVEL === 'WHITESPACE_ONLY' ?
5671 'require(\'firebase/auth\');' + DEFAULT_IMPORT_FIX + '%output% ' +
5772 DIALOG_POLYFILL + '})();' + 'module.exports=firebaseui;' ;
5873
74+ // Adds the module requirement and exports firebaseui.
75+ const ESM_MODULE_WRAPPER = OPTIMIZATION_LEVEL === 'WHITESPACE_ONLY' ?
76+ ESM_IMPORT + '%output%' +
77+ ESM_DIALOG_POLYFILL + ESM_EXPORT :
78+ ESM_IMPORT +
79+ '(function() {' + '%output%' + '}).apply(' +
80+ 'typeof global !== \'undefined\' ? global : typeof self !== \'undefined\' ' +
81+ '? self : window );' +
82+ ESM_DIALOG_POLYFILL + ESM_EXPORT ;
83+
5984// The path to Closure Compiler.
6085const COMPILER_PATH = 'node_modules/google-closure-compiler-java/compiler.jar' ;
6186
@@ -265,6 +290,12 @@ repeatTaskForAllLocales(
265290 'build-npm-$' , [ 'build-firebaseui-js-$' ] ,
266291 ( locale ) => concatWithDeps ( locale , 'npm' , NPM_MODULE_WRAPPER ) ) ;
267292
293+ // Bundles the FirebaseUI JS with its dependencies as a ESM module. This builds
294+ // the NPM module for all languages.
295+ repeatTaskForAllLocales (
296+ 'build-esm-$' , [ 'build-firebaseui-js-$' ] ,
297+ ( locale ) => concatWithDeps ( locale , 'esm' , ESM_MODULE_WRAPPER ) ) ;
298+
268299// Bundles the FirebaseUI JS with its dependencies for all locales.
269300// Generates the gulp tasks build-js-de, build-js-fr, etc.
270301const buildJsTasks = repeatTaskForAllLocales (
@@ -289,6 +320,12 @@ gulp.task('build-npm', gulp.series(
289320 ( ) => makeDefaultFile ( 'npm' )
290321) ) ;
291322
323+ // Builds the ESM module for the default language.
324+ gulp . task ( 'build-esm' , gulp . series (
325+ 'build-esm-' + DEFAULT_LOCALE ,
326+ ( ) => makeDefaultFile ( 'esm' )
327+ ) ) ;
328+
292329/**
293330 * Builds the CSS for FirebaseUI.
294331 * @param {boolean } isRtl Whether to build in right-to-left mode.
@@ -336,13 +373,13 @@ gulp.task('clean', () => fse.remove(TMP_DIR));
336373// Executes the basic tasks for the default language.
337374gulp . task ( 'default' , gulp . series (
338375 'build-externs' , 'build-ts' , 'build-js' ,
339- 'build-npm' , 'build-css' , 'build-css-rtl' ,
376+ 'build-npm' , 'build-esm' , 'build- css', 'build-css-rtl' ,
340377 'clean'
341378) ) ;
342379
343380// Builds everything (JS for all languages, both LTR and RTL CSS).
344381gulp . task ( 'build-all' , gulp . series (
345382 'build-externs' , 'build-ts' , 'build-all-js' ,
346- 'build-npm' , 'build-css' , 'build-css-rtl' ,
383+ 'build-npm' , 'build-esm' , 'build- css', 'build-css-rtl' ,
347384 'clean'
348385) ) ;
0 commit comments