Skip to content

Commit 668f183

Browse files
authored
ref: No more dynamic requires (#801)
* ref: No more dynamic requires This patch changes all dynamic requires except for a few, 'safe' cases and 'sentry/cli' related ones as it will be removed in a follow up * update PR no in changelog
1 parent 5705509 commit 668f183

File tree

14 files changed

+100
-64
lines changed

14 files changed

+100
-64
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- ref!: Bump main Node.js version to the earliest LTS v18 ([#793](https://github.com/getsentry/sentry-wizard/pull/793))
66
- ref!: Follow up to Node v18 changes ([#797](https://github.com/getsentry/sentry-wizard/pull/797))
77
- ref: Remove obsolete deps (r2, lodash) ([#799](https://github.com/getsentry/sentry-wizard/pull/799))
8+
- ref: No more dynamic requires ([#801](https://github.com/getsentry/sentry-wizard/pull/801))
89

910
## 3.42.1
1011

lib/Steps/ChooseIntegration.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ import { Cordova } from './Integrations/Cordova';
1212
import { Electron } from './Integrations/Electron';
1313
import { hasPackageInstalled } from '../../src/utils/package-json';
1414
import { dim } from '../Helper/Logging';
15+
import { readFileSync } from 'node:fs';
1516

16-
let projectPackage: any = {};
17+
let projectPackage: Record<string, unknown> = {};
1718

1819
try {
1920
// If we run directly in setup-wizard
20-
projectPackage = require('../../package.json');
21+
projectPackage = JSON.parse(
22+
readFileSync('../../package.json', 'utf-8'),
23+
) as Record<string, unknown>;
2124
} catch {
22-
projectPackage = require(`${process.cwd()}/package.json`);
25+
projectPackage = JSON.parse(
26+
readFileSync(`${process.cwd()}/package.json`, 'utf-8'),
27+
) as Record<string, unknown>;
2328
}
2429

2530
type IntegrationPromptAnswer = {

lib/Steps/Initial.ts

+19-10
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,36 @@ import { join, dirname } from 'node:path';
33

44
import { dim } from '../Helper/Logging';
55
import { BaseStep } from './BaseStep';
6+
import { readFileSync } from 'node:fs';
67

78
type PackageJSON = { version?: string };
89
let wizardPackage: PackageJSON = {};
910
let sentryCliPackage: PackageJSON = {};
1011

1112
try {
12-
wizardPackage = require(join(
13-
dirname(require.resolve('@sentry/wizard')),
14-
'..',
15-
'package.json',
16-
));
13+
wizardPackage = process.env.npm_package_version
14+
? { version: process.env.npm_package_version }
15+
: (JSON.parse(
16+
readFileSync(
17+
join(
18+
dirname(require.resolve('@sentry/wizard')),
19+
'..',
20+
'package.json',
21+
),
22+
'utf-8',
23+
),
24+
) as PackageJSON);
1725
} catch {
1826
// We don't need to have this
1927
}
2028

2129
try {
22-
sentryCliPackage = require(join(
23-
dirname(require.resolve('@sentry/cli')),
24-
'..',
25-
'package.json',
26-
));
30+
sentryCliPackage = JSON.parse(
31+
readFileSync(
32+
join(dirname(require.resolve('@sentry/cli')), '..', 'package.json'),
33+
'utf-8',
34+
),
35+
) as PackageJSON;
2736
} catch {
2837
// We don't need to have this
2938
}

lib/Steps/OpenSentry.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import { URL } from 'node:url';
12
import type { Answers } from 'inquirer';
2-
import { URL } from 'url';
33

44
import { mapIntegrationToPlatform } from '../Constants';
55
import { BottomBar } from '../Helper/BottomBar';
66
import { dim, green, l, nl, red } from '../Helper/Logging';
77
import { getCurrentIntegration } from '../Helper/Wizard';
88
import { BaseStep } from './BaseStep';
99

10-
const opn = require('opn');
10+
import opn from 'opn';
1111

1212
export class OpenSentry extends BaseStep {
1313
public async emit(answers: Answers): Promise<Answers> {

src/nextjs/templates.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,8 @@ export default function GlobalError(${chalk.green(
501501
);
502502
}
503503
`;
504-
} else {
505-
return `"use client";
504+
}
505+
return `"use client";
506506
507507
${chalk.green('import * as Sentry from "@sentry/nextjs";')}
508508
${chalk.green('import Error from "next/error";')}
@@ -522,5 +522,4 @@ export default function GlobalError(${chalk.green('{ error }')}) {
522522
);
523523
}
524524
`;
525-
}
526525
}

src/nuxt/sdk-setup.ts

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
13
// @ts-expect-error - clack is ESM and TS complains about that. It works though
24
import * as clack from '@clack/prompts';
35
import * as Sentry from '@sentry/node';
46
import chalk from 'chalk';
5-
import fs from 'fs';
67
// @ts-expect-error - magicast is ESM and TS complains about that. It works though
7-
import { loadFile, generateCode } from 'magicast';
8+
import { generateCode, loadFile } from 'magicast';
89
// @ts-expect-error - magicast is ESM and TS complains about that. It works though
910
import { addNuxtModule } from 'magicast/helpers';
10-
import path from 'path';
11-
import {
12-
getConfigBody,
13-
getDefaultNuxtConfig,
14-
getNuxtModuleFallbackTemplate,
15-
getSentryConfigContents,
16-
} from './templates';
11+
import opn from 'opn';
12+
import { type SemVer, lt } from 'semver';
13+
import { traceStep } from '../telemetry';
1714
import {
1815
abortIfCancelled,
1916
askShouldAddPackageOverride,
2017
askShouldInstallPackage,
2118
featureSelectionPrompt,
2219
installPackage,
2320
isUsingTypeScript,
24-
opn,
2521
} from '../utils/clack-utils';
26-
import { traceStep } from '../telemetry';
27-
import { lt, SemVer } from 'semver';
28-
import { PackageManager, PNPM } from '../utils/package-manager';
29-
import { hasPackageInstalled, PackageDotJson } from '../utils/package-json';
30-
import { deploymentPlatforms, DeploymentPlatform } from './types';
22+
import {
23+
type PackageDotJson,
24+
hasPackageInstalled,
25+
} from '../utils/package-json';
26+
import { PNPM, type PackageManager } from '../utils/package-manager';
27+
import {
28+
getConfigBody,
29+
getDefaultNuxtConfig,
30+
getNuxtModuleFallbackTemplate,
31+
getSentryConfigContents,
32+
} from './templates';
33+
import { type DeploymentPlatform, deploymentPlatforms } from './types';
3134

3235
const possibleNuxtConfig = [
3336
'nuxt.config.js',

src/react-native/expo-metro.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as fs from 'fs';
1+
import * as fs from 'node:fs';
22
// @ts-ignore - clack is ESM and TS complains about that. It works though
33
import * as clack from '@clack/prompts';
44
// @ts-ignore - magicast is ESM and TS complains about that. It works though
5-
import { ProxifiedModule } from 'magicast';
5+
import type { ProxifiedModule } from 'magicast';
66
import chalk from 'chalk';
77
import * as Sentry from '@sentry/node';
88

src/react-native/xcode.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
22
/* eslint-disable @typescript-eslint/no-unsafe-call */
33
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
4-
import * as fs from 'fs';
4+
import * as fs from 'node:fs';
55
// @ts-ignore - clack is ESM and TS complains about that. It works though
66
import clack from '@clack/prompts';
77
import chalk from 'chalk';

src/run.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { runSourcemapsWizard } from './sourcemaps/sourcemaps-wizard';
1616
import { readEnvironment } from '../lib/Helper/Env';
1717
import type { Platform } from '../lib/Constants';
1818
import type { PackageDotJson } from './utils/package-json';
19+
import { readFileSync } from 'node:fs';
1920

2021
type WizardIntegration =
2122
| 'reactNative'
@@ -195,10 +196,16 @@ export async function run(argv: Args) {
195196
* TODO: replace with rollup replace whenever we switch to rollup
196197
*/
197198
function tryGetWizardVersion(): string {
198-
try {
199-
const wizardPkgJson = require('../package.json') as PackageDotJson;
200-
return wizardPkgJson.version ?? '';
201-
} catch {
202-
return '';
199+
let version = process.env.npm_package_version;
200+
if (!version) {
201+
try {
202+
const wizardPkgJson = JSON.parse(
203+
readFileSync('../package.json', 'utf-8'),
204+
) as PackageDotJson;
205+
version = wizardPkgJson.version;
206+
} catch {
207+
// ignore
208+
}
203209
}
210+
return version ?? '';
204211
}

src/sourcemaps/tools/nextjs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
abortIfCancelled,
88
addDotEnvSentryBuildPluginFile,
99
} from '../../utils/clack-utils';
10-
import { WizardOptions } from '../../utils/types';
10+
import type { WizardOptions } from '../../utils/types';
1111

12-
import { SourceMapUploadToolConfigurationOptions } from './types';
12+
import type { SourceMapUploadToolConfigurationOptions } from './types';
1313

1414
import * as Sentry from '@sentry/node';
1515

src/sourcemaps/tools/webpack.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as path from 'path';
2-
import * as fs from 'fs';
1+
import * as path from 'node:path';
2+
import * as fs from 'node:fs';
33

44
// @ts-ignore - clack is ESM and TS complains about that. It works though
55
import * as clack from '@clack/prompts';
@@ -23,7 +23,7 @@ import {
2323
} from '../../utils/clack-utils';
2424
import { hasPackageInstalled } from '../../utils/package-json';
2525

26-
import {
26+
import type {
2727
SourceMapUploadToolConfigurationFunction,
2828
SourceMapUploadToolConfigurationOptions,
2929
} from './types';

src/telemetry.ts

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import {
2-
defaultStackParser,
32
Hub,
43
Integrations,
4+
NodeClient,
5+
type Span,
6+
defaultStackParser,
7+
flush,
58
makeMain,
69
makeNodeTransport,
7-
NodeClient,
810
runWithAsyncContext,
911
setTag,
1012
startSpan,
11-
flush,
12-
Span,
1313
} from '@sentry/node';
14-
import packageJson from '../package.json';
15-
import { WizardOptions } from './utils/types';
14+
import type { WizardOptions } from './utils/types';
15+
import { readFileSync } from 'node:fs';
16+
import { dirname, join } from 'node:path';
1617

1718
export async function withTelemetry<F>(
1819
options: {
@@ -68,6 +69,19 @@ export async function withTelemetry<F>(
6869
}
6970

7071
function createSentryInstance(enabled: boolean, integration: string) {
72+
const { version } = process.env.npm_package_version
73+
? { version: process.env.npm_package_version }
74+
: (JSON.parse(
75+
readFileSync(
76+
join(
77+
dirname(require.resolve('@sentry/wizard')),
78+
'..',
79+
'package.json',
80+
),
81+
'utf-8',
82+
),
83+
) as { version?: string });
84+
7185
const client = new NodeClient({
7286
dsn: 'https://[email protected]/4505425820712960',
7387
enabled: enabled,
@@ -77,7 +91,7 @@ function createSentryInstance(enabled: boolean, integration: string) {
7791
tracesSampleRate: 1,
7892
sampleRate: 1,
7993

80-
release: packageJson.version,
94+
release: version,
8195
integrations: [new Integrations.Http()],
8296
tracePropagationTargets: [/^https:\/\/sentry.io\//],
8397

src/utils/clack-utils.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
// @ts-ignore - clack is ESM and TS complains about that. It works though
2-
import * as clack from '@clack/prompts';
3-
import * as Sentry from '@sentry/node';
4-
import axios from 'axios';
5-
import chalk from 'chalk';
61
import * as childProcess from 'node:child_process';
72
import * as fs from 'node:fs';
83
import * as os from 'node:os';
94
import { basename, dirname, isAbsolute, join, relative } from 'node:path';
5+
import { setInterval } from 'node:timers';
106
import { URL } from 'node:url';
7+
// @ts-ignore - clack is ESM and TS complains about that. It works though
8+
import * as clack from '@clack/prompts';
9+
import * as Sentry from '@sentry/node';
10+
import axios from 'axios';
11+
import chalk from 'chalk';
1112
import opn from 'opn';
12-
import { setInterval } from 'timers';
1313
import { traceStep } from '../telemetry';
1414
import { debug } from './debug';
15-
import { hasPackageInstalled, PackageDotJson } from './package-json';
15+
import { type PackageDotJson, hasPackageInstalled } from './package-json';
1616
import {
17+
type PackageManager,
1718
detectPackageManger,
18-
PackageManager,
1919
packageManagers,
2020
} from './package-manager';
2121
import { fulfillsVersionRange } from './semver';
22-
import { Feature, SentryProjectData, WizardOptions } from './types';
22+
import type { Feature, SentryProjectData, WizardOptions } from './types';
2323

24-
export { opn };
2524
export const SENTRY_DOT_ENV_FILE = '.env.sentry-build-plugin';
2625
export const SENTRY_CLI_RC_FILE = '.sentryclirc';
2726
export const SENTRY_PROPERTIES_FILE = 'sentry.properties';

tsconfig.build.json

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"emitDecoratorMetadata": true,
1515
"sourceMap": true,
1616
"inlineSources": true,
17-
"resolveJsonModule": true,
1817
"esModuleInterop": true
1918
}
2019
}

0 commit comments

Comments
 (0)