From cefcf2caad8c24470d5bbb8cca7e1de14d982ae8 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 21 Dec 2021 14:20:53 -0700 Subject: [PATCH 1/4] Fix Vercel build --- packages/firestore/rollup.config.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/firestore/rollup.config.js b/packages/firestore/rollup.config.js index 6e0638284cb..26010dd0c3d 100644 --- a/packages/firestore/rollup.config.js +++ b/packages/firestore/rollup.config.js @@ -23,6 +23,7 @@ import replace from 'rollup-plugin-replace'; import { terser } from 'rollup-plugin-terser'; import typescriptPlugin from 'rollup-plugin-typescript2'; import tmp from 'tmp'; +import { basename } from 'path'; import typescript from 'typescript'; import { generateBuildTargetReplaceConfig } from '../../scripts/build/rollup_replace_build_target'; @@ -31,16 +32,21 @@ import pkg from './package.json'; const util = require('./rollup.shared'); -// Customize how import.meta.url is polyfilled in cjs nodejs build. We use it to be able to use require() in esm. -// It only generates the nodejs version of the polyfill, as opposed to the default polyfill which -// supports both browser and nodejs. The browser support is unnecessary and doesn't work well with Jest. See https://github.com/firebase/firebase-js-sdk/issues/5687 -function importMetaUrlPolyfillPlugin() { +// Customize how import.meta.url is polyfilled in cjs nodejs build. We use it to +// be able to use require() in esm. It only generates the nodejs version of the +// polyfill, as opposed to the default polyfill which supports both browser and +// nodejs. The browser support is unnecessary and doesn't work well with Jest. +// See https://github.com/firebase/firebase-js-sdk/issues/5687 +function importMetaUrlPolyfillPlugin(filename) { return { name: 'import-meta-url-current-module', resolveImportMeta(property, { moduleId }) { if (property === 'url') { // copied from rollup output - return `new (require('url').URL)('file:' + __filename).href`; + return "(typeof document === 'undefined' ? new (require('url').URL)" + + "('file:' + __filename).href : (document.currentScript && " + + `document.currentScript.src || new URL('${filename}', ` + + "document.baseURI).href))"; } return null; } @@ -124,7 +130,7 @@ const allBuilds = [ plugins: [ ...util.es2017ToEs5Plugins(/* mangled= */ false), replace(generateBuildTargetReplaceConfig('cjs', 2017)), - importMetaUrlPolyfillPlugin() + importMetaUrlPolyfillPlugin(basename(pkg.main)) ], external: util.resolveNodeExterns, treeshake: { From c889163b618ae855d826c4b7fb306af7720c153e Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 21 Dec 2021 14:22:43 -0700 Subject: [PATCH 2/4] Create eight-cows-push.md --- .changeset/eight-cows-push.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/eight-cows-push.md diff --git a/.changeset/eight-cows-push.md b/.changeset/eight-cows-push.md new file mode 100644 index 00000000000..cb549259387 --- /dev/null +++ b/.changeset/eight-cows-push.md @@ -0,0 +1,5 @@ +--- +"@firebase/firestore": patch +--- + +Fix Proto loading for Vercel/Next.js. From bf651465067d68e0f092a93c691523db239822d7 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 21 Dec 2021 14:31:07 -0700 Subject: [PATCH 3/4] Format --- packages/firestore/rollup.config.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/firestore/rollup.config.js b/packages/firestore/rollup.config.js index 26010dd0c3d..34e75ba0274 100644 --- a/packages/firestore/rollup.config.js +++ b/packages/firestore/rollup.config.js @@ -43,10 +43,12 @@ function importMetaUrlPolyfillPlugin(filename) { resolveImportMeta(property, { moduleId }) { if (property === 'url') { // copied from rollup output - return "(typeof document === 'undefined' ? new (require('url').URL)" - + "('file:' + __filename).href : (document.currentScript && " - + `document.currentScript.src || new URL('${filename}', ` - + "document.baseURI).href))"; + return ( + "(typeof document === 'undefined' ? new (require('url').URL)" + + "('file:' + __filename).href : (document.currentScript && " + + `document.currentScript.src || new URL('${filename}', ` + + 'document.baseURI).href))' + ); } return null; } From b333c0a23e8aff3049cbbcf6e5bbd8c3b8905720 Mon Sep 17 00:00:00 2001 From: Christina Holland Date: Fri, 7 Jan 2022 17:12:21 -0800 Subject: [PATCH 4/4] Add jest exception (#5858) --- packages/firestore/rollup.config.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/firestore/rollup.config.js b/packages/firestore/rollup.config.js index 34e75ba0274..ab20bf7a96f 100644 --- a/packages/firestore/rollup.config.js +++ b/packages/firestore/rollup.config.js @@ -35,19 +35,30 @@ const util = require('./rollup.shared'); // Customize how import.meta.url is polyfilled in cjs nodejs build. We use it to // be able to use require() in esm. It only generates the nodejs version of the // polyfill, as opposed to the default polyfill which supports both browser and -// nodejs. The browser support is unnecessary and doesn't work well with Jest. +// nodejs. The browser support doesn't work well with Jest. // See https://github.com/firebase/firebase-js-sdk/issues/5687 +// Although this is a cjs Node build and shouldn't require the browser option, +// Vercel apps using this break on deployment, but work in local development. +// See https://github.com/firebase/firebase-js-sdk/issues/5823 function importMetaUrlPolyfillPlugin(filename) { return { name: 'import-meta-url-current-module', resolveImportMeta(property, { moduleId }) { if (property === 'url') { - // copied from rollup output + // Added a check for Jest (see issue 5687 linked above) + // See https://jestjs.io/docs/environment-variables - apparently + // these are not always both set. + const JEST_CHECK = + `typeof process !== 'undefined' && process.env !== undefined` + + ` && (process.env.JEST_WORKER_ID !== undefined || ` + + `process.env.NODE_ENV === 'test')`; + // Copied from rollup output return ( - "(typeof document === 'undefined' ? new (require('url').URL)" + - "('file:' + __filename).href : (document.currentScript && " + + `((typeof document === 'undefined' || (${JEST_CHECK})) ?` + + ` new (require('url').URL)` + + `('file:' + __filename).href : (document.currentScript && ` + `document.currentScript.src || new URL('${filename}', ` + - 'document.baseURI).href))' + `document.baseURI).href))` ); } return null;