Skip to content

Commit a6bf3e6

Browse files
timfishAbhiPrasad
andauthored
test(node): Remove import.meta.url hacks (#15490)
Jest didn't like usages of `import.meta.url` but now we're using Vitest, these hacks can be removed. Our Rollup config already converts this correctly when building our CommonJs output. --------- Co-authored-by: Abhijeet Prasad <[email protected]>
1 parent 63193ef commit a6bf3e6

File tree

3 files changed

+3
-31
lines changed

3 files changed

+3
-31
lines changed

dev-packages/rollup-utils/npmHelpers.mjs

+1-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { defineConfig } from 'rollup';
1515
import {
1616
makeCleanupPlugin,
1717
makeDebugBuildStatementReplacePlugin,
18-
makeImportMetaUrlReplacePlugin,
1918
makeNodeResolvePlugin,
2019
makeRrwebBuildPlugin,
2120
makeSucrasePlugin,
@@ -39,7 +38,6 @@ export function makeBaseNPMConfig(options = {}) {
3938
const nodeResolvePlugin = makeNodeResolvePlugin();
4039
const sucrasePlugin = makeSucrasePlugin({}, sucrase);
4140
const debugBuildStatementReplacePlugin = makeDebugBuildStatementReplacePlugin();
42-
const importMetaUrlReplacePlugin = makeImportMetaUrlReplacePlugin();
4341
const cleanupPlugin = makeCleanupPlugin();
4442
const rrwebBuildPlugin = makeRrwebBuildPlugin({
4543
excludeShadowDom: undefined,
@@ -85,14 +83,7 @@ export function makeBaseNPMConfig(options = {}) {
8583
interop: 'esModule',
8684
},
8785

88-
plugins: [
89-
nodeResolvePlugin,
90-
sucrasePlugin,
91-
debugBuildStatementReplacePlugin,
92-
importMetaUrlReplacePlugin,
93-
rrwebBuildPlugin,
94-
cleanupPlugin,
95-
],
86+
plugins: [nodeResolvePlugin, sucrasePlugin, debugBuildStatementReplacePlugin, rrwebBuildPlugin, cleanupPlugin],
9687

9788
// don't include imported modules from outside the package in the final output
9889
external: [

dev-packages/rollup-utils/plugins/npmPlugins.mjs

-13
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,6 @@ export function makeDebugBuildStatementReplacePlugin() {
125125
});
126126
}
127127

128-
/**
129-
* Because jest doesn't like `import.meta` statements but we still need it in the code base we instead use a magic
130-
* string that we replace with import.meta.url in the build.
131-
*/
132-
export function makeImportMetaUrlReplacePlugin() {
133-
return replace({
134-
preventAssignment: false,
135-
values: {
136-
__IMPORT_META_URL_REPLACEMENT__: 'import.meta.url',
137-
},
138-
});
139-
}
140-
141128
/**
142129
* Creates a plugin to replace build flags of rrweb with either a constant (if passed true/false) or with a safe statement that:
143130
* a) evaluates to `true`

packages/node/src/sdk/initOtel.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import { SentryContextManager } from '../otel/contextManager';
1717
import { isCjs } from '../utils/commonjs';
1818
import type { NodeClient } from './client';
1919

20-
declare const __IMPORT_META_URL_REPLACEMENT__: string;
21-
2220
// About 277h - this must fit into new Array(len)!
2321
const MAX_MAX_SPAN_WAIT_DURATION = 1_000_000;
2422

@@ -45,15 +43,11 @@ export function maybeInitializeEsmLoader(): void {
4543

4644
// Register hook was added in v20.6.0 and v18.19.0
4745
if (nodeMajor >= 22 || (nodeMajor === 20 && nodeMinor >= 6) || (nodeMajor === 18 && nodeMinor >= 19)) {
48-
// We need to work around using import.meta.url directly because jest complains about it.
49-
const importMetaUrl =
50-
typeof __IMPORT_META_URL_REPLACEMENT__ !== 'undefined' ? __IMPORT_META_URL_REPLACEMENT__ : undefined;
51-
52-
if (!GLOBAL_OBJ._sentryEsmLoaderHookRegistered && importMetaUrl) {
46+
if (!GLOBAL_OBJ._sentryEsmLoaderHookRegistered) {
5347
try {
5448
const { addHookMessagePort } = createAddHookMessageChannel();
5549
// @ts-expect-error register is available in these versions
56-
moduleModule.register('import-in-the-middle/hook.mjs', importMetaUrl, {
50+
moduleModule.register('import-in-the-middle/hook.mjs', import.meta.url, {
5751
data: { addHookMessagePort, include: [] },
5852
transferList: [addHookMessagePort],
5953
});

0 commit comments

Comments
 (0)