@@ -35,19 +35,30 @@ const util = require('./rollup.shared');
35
35
// Customize how import.meta.url is polyfilled in cjs nodejs build. We use it to
36
36
// be able to use require() in esm. It only generates the nodejs version of the
37
37
// polyfill, as opposed to the default polyfill which supports both browser and
38
- // nodejs. The browser support is unnecessary and doesn't work well with Jest.
38
+ // nodejs. The browser support doesn't work well with Jest.
39
39
// See https://github.com/firebase/firebase-js-sdk/issues/5687
40
+ // Although this is a cjs Node build and shouldn't require the browser option,
41
+ // Vercel apps using this break on deployment, but work in local development.
42
+ // See https://github.com/firebase/firebase-js-sdk/issues/5823
40
43
function importMetaUrlPolyfillPlugin ( filename ) {
41
44
return {
42
45
name : 'import-meta-url-current-module' ,
43
46
resolveImportMeta ( property , { moduleId } ) {
44
47
if ( property === 'url' ) {
45
- // copied from rollup output
48
+ // Added a check for Jest (see issue 5687 linked above)
49
+ // See https://jestjs.io/docs/environment-variables - apparently
50
+ // these are not always both set.
51
+ const JEST_CHECK =
52
+ `typeof process !== 'undefined' && process.env !== undefined` +
53
+ ` && (process.env.JEST_WORKER_ID !== undefined || ` +
54
+ `process.env.NODE_ENV === 'test')` ;
55
+ // Copied from rollup output
46
56
return (
47
- "(typeof document === 'undefined' ? new (require('url').URL)" +
48
- "('file:' + __filename).href : (document.currentScript && " +
57
+ `((typeof document === 'undefined' || (${ JEST_CHECK } )) ?` +
58
+ ` new (require('url').URL)` +
59
+ `('file:' + __filename).href : (document.currentScript && ` +
49
60
`document.currentScript.src || new URL('${ filename } ', ` +
50
- ' document.baseURI).href))'
61
+ ` document.baseURI).href))`
51
62
) ;
52
63
}
53
64
return null ;
0 commit comments