Skip to content

Commit f66f93a

Browse files
committed
Restore import.meta
1 parent aa07a32 commit f66f93a

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

packages/firestore/rollup.config.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,34 @@ import { version as grpcVersion } from '@grpc/grpc-js/package.json';
1919
import alias from '@rollup/plugin-alias';
2020
import json from '@rollup/plugin-json';
2121
import copy from 'rollup-plugin-copy';
22+
import replace from 'rollup-plugin-replace';
2223
import { terser } from 'rollup-plugin-terser';
2324
import typescriptPlugin from 'rollup-plugin-typescript2';
2425
import tmp from 'tmp';
2526
import typescript from 'typescript';
26-
import replace from 'rollup-plugin-replace';
2727

2828
import { generateBuildTargetReplaceConfig } from '../../scripts/build/rollup_replace_build_target';
2929

3030
import pkg from './package.json';
3131

3232
const util = require('./rollup.shared');
3333

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() {
38+
return {
39+
name: 'import-meta-url-current-module',
40+
resolveImportMeta(property, { moduleId }) {
41+
if (property === 'url') {
42+
// copied from rollup output
43+
return `new (require('url').URL)('file:' + __filename).href`;
44+
}
45+
return null;
46+
}
47+
};
48+
}
49+
3450
const nodePlugins = function () {
3551
return [
3652
typescriptPlugin({
@@ -107,7 +123,8 @@ const allBuilds = [
107123
},
108124
plugins: [
109125
...util.es2017ToEs5Plugins(/* mangled= */ false),
110-
replace(generateBuildTargetReplaceConfig('cjs', 2017))
126+
replace(generateBuildTargetReplaceConfig('cjs', 2017)),
127+
importMetaUrlPolyfillPlugin()
111128
],
112129
external: util.resolveNodeExterns,
113130
treeshake: {

packages/firestore/src/platform/node/grpc_connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { NodeCallback, nodePromise } from '../../util/node_api';
3535
import { Deferred } from '../../util/promise';
3636

3737
// TODO: Fetch runtime version from grpc-js/package.json instead
38-
// when it becomes possible to use require() in Node ESM without hacks.
38+
// when there's a cleaner way to dynamic require in both Node ESM and CJS
3939
const grpcVersion = '__GRPC_VERSION__';
4040

4141
const LOG_TAG = 'Connection';

0 commit comments

Comments
 (0)