Skip to content

Commit 044a8d7

Browse files
Fix Vercel build (#5830)
1 parent b5084fb commit 044a8d7

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

.changeset/eight-cows-push.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/firestore": patch
3+
---
4+
5+
Fix Proto loading for Vercel/Next.js.

packages/firestore/rollup.config.js

+26-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import replace from 'rollup-plugin-replace';
2323
import { terser } from 'rollup-plugin-terser';
2424
import typescriptPlugin from 'rollup-plugin-typescript2';
2525
import tmp from 'tmp';
26+
import { basename } from 'path';
2627
import typescript from 'typescript';
2728

2829
import { generateBuildTargetReplaceConfig } from '../../scripts/build/rollup_replace_build_target';
@@ -31,16 +32,34 @@ import pkg from './package.json';
3132

3233
const util = require('./rollup.shared');
3334

34-
// Customize how import.meta.url is polyfilled in cjs nodejs build. We use it to be able to use require() in esm.
35-
// It only generates the nodejs version of the polyfill, as opposed to the default polyfill which
36-
// 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
37-
function importMetaUrlPolyfillPlugin() {
35+
// Customize how import.meta.url is polyfilled in cjs nodejs build. We use it to
36+
// be able to use require() in esm. It only generates the nodejs version of the
37+
// polyfill, as opposed to the default polyfill which supports both browser and
38+
// nodejs. The browser support doesn't work well with Jest.
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
43+
function importMetaUrlPolyfillPlugin(filename) {
3844
return {
3945
name: 'import-meta-url-current-module',
4046
resolveImportMeta(property, { moduleId }) {
4147
if (property === 'url') {
42-
// copied from rollup output
43-
return `new (require('url').URL)('file:' + __filename).href`;
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
56+
return (
57+
`((typeof document === 'undefined' || (${JEST_CHECK})) ?` +
58+
` new (require('url').URL)` +
59+
`('file:' + __filename).href : (document.currentScript && ` +
60+
`document.currentScript.src || new URL('${filename}', ` +
61+
`document.baseURI).href))`
62+
);
4463
}
4564
return null;
4665
}
@@ -124,7 +143,7 @@ const allBuilds = [
124143
plugins: [
125144
...util.es2017ToEs5Plugins(/* mangled= */ false),
126145
replace(generateBuildTargetReplaceConfig('cjs', 2017)),
127-
importMetaUrlPolyfillPlugin()
146+
importMetaUrlPolyfillPlugin(basename(pkg.main))
128147
],
129148
external: util.resolveNodeExterns,
130149
treeshake: {

0 commit comments

Comments
 (0)