diff --git a/.changeset/quick-experts-shave.md b/.changeset/quick-experts-shave.md new file mode 100644 index 00000000000..c77153ccf9e --- /dev/null +++ b/.changeset/quick-experts-shave.md @@ -0,0 +1,5 @@ +--- +"@firebase/firestore": patch +--- + +better meta url polyfill diff --git a/packages/firestore/rollup.config.js b/packages/firestore/rollup.config.js index 286b05ce293..7deee0e78d1 100644 --- a/packages/firestore/rollup.config.js +++ b/packages/firestore/rollup.config.js @@ -29,6 +29,22 @@ import { generateBuildTargetReplaceConfig } from '../../scripts/build/rollup_rep 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() { + 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 null; + } + }; +} + const nodePlugins = function () { return [ typescriptPlugin({ @@ -105,7 +121,8 @@ const allBuilds = [ }, plugins: [ ...util.es2017ToEs5Plugins(/* mangled= */ false), - replace(generateBuildTargetReplaceConfig('cjs', 2017)) + replace(generateBuildTargetReplaceConfig('cjs', 2017)), + importMetaUrlPolyfillPlugin() ], external: util.resolveNodeExterns, treeshake: {