Skip to content

Commit ce39a1a

Browse files
authored
better meta url polyfill (#5738)
* better meta url polyfill * Create quick-experts-shave.md * update
1 parent 6f0049e commit ce39a1a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

.changeset/quick-experts-shave.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/firestore": patch
3+
---
4+
5+
better meta url polyfill

packages/firestore/rollup.config.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ import { generateBuildTargetReplaceConfig } from '../../scripts/build/rollup_rep
2929

3030
const util = require('./rollup.shared');
3131

32+
// Customize how import.meta.url is polyfilled in cjs nodejs build. We use it to be able to use require() in esm.
33+
// It only generates the nodejs version of the polyfill, as opposed to the default polyfill which
34+
// 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
35+
function importMetaUrlPolyfillPlugin() {
36+
return {
37+
name: 'import-meta-url-current-module',
38+
resolveImportMeta(property, { moduleId }) {
39+
if (property === 'url') {
40+
// copied from rollup output
41+
return `new (require('url').URL)('file:' + __filename).href`;
42+
}
43+
return null;
44+
}
45+
};
46+
}
47+
3248
const nodePlugins = function () {
3349
return [
3450
typescriptPlugin({
@@ -105,7 +121,8 @@ const allBuilds = [
105121
},
106122
plugins: [
107123
...util.es2017ToEs5Plugins(/* mangled= */ false),
108-
replace(generateBuildTargetReplaceConfig('cjs', 2017))
124+
replace(generateBuildTargetReplaceConfig('cjs', 2017)),
125+
importMetaUrlPolyfillPlugin()
109126
],
110127
external: util.resolveNodeExterns,
111128
treeshake: {

0 commit comments

Comments
 (0)