Skip to content

Commit cefcf2c

Browse files
Fix Vercel build
1 parent 9295bb0 commit cefcf2c

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

packages/firestore/rollup.config.js

Lines changed: 12 additions & 6 deletions
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,21 @@ 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 is unnecessary and doesn't work well with Jest.
39+
// See https://github.com/firebase/firebase-js-sdk/issues/5687
40+
function importMetaUrlPolyfillPlugin(filename) {
3841
return {
3942
name: 'import-meta-url-current-module',
4043
resolveImportMeta(property, { moduleId }) {
4144
if (property === 'url') {
4245
// copied from rollup output
43-
return `new (require('url').URL)('file:' + __filename).href`;
46+
return "(typeof document === 'undefined' ? new (require('url').URL)"
47+
+ "('file:' + __filename).href : (document.currentScript && "
48+
+ `document.currentScript.src || new URL('${filename}', `
49+
+ "document.baseURI).href))";
4450
}
4551
return null;
4652
}
@@ -124,7 +130,7 @@ const allBuilds = [
124130
plugins: [
125131
...util.es2017ToEs5Plugins(/* mangled= */ false),
126132
replace(generateBuildTargetReplaceConfig('cjs', 2017)),
127-
importMetaUrlPolyfillPlugin()
133+
importMetaUrlPolyfillPlugin(basename(pkg.main))
128134
],
129135
external: util.resolveNodeExterns,
130136
treeshake: {

0 commit comments

Comments
 (0)