Skip to content

Commit 97f7597

Browse files
committed
add logic to support sentry
1 parent b24b27d commit 97f7597

File tree

4 files changed

+147
-2
lines changed

4 files changed

+147
-2
lines changed

src/bundle.js

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getRNVersion, translateOptions } from './utils';
33
import * as fs from 'fs-extra';
44
import { ZipFile } from 'yazl';
55
import { open as openZipFile } from 'yauzl';
6-
import { question, printVersionCommand } from './utils';
6+
import { question, checkPlugins } from './utils';
77
import { checkPlatform } from './app';
88
import { spawn, spawnSync } from 'node:child_process';
99
import semverSatisfies from 'semver/functions/satisfies';
@@ -86,6 +86,9 @@ async function runReactNativeBundleCommand(
8686
});
8787
}
8888
}
89+
const bundleParams = await checkPlugins();
90+
const minifyOption = bundleParams.minify;
91+
const isSentry = bundleParams.sentry;
8992
const bundleCommand = usingExpo
9093
? 'export:embed'
9194
: platform === 'harmony'
@@ -123,6 +126,8 @@ async function runReactNativeBundleCommand(
123126
'--platform',
124127
platform,
125128
'--reset-cache',
129+
'--minify',
130+
minifyOption,
126131
]);
127132

128133
if (sourcemapOutput) {
@@ -190,7 +195,20 @@ async function runReactNativeBundleCommand(
190195
bundleName,
191196
outputFolder,
192197
sourcemapOutput,
198+
!isSentry,
193199
);
200+
if (isSentry) {
201+
await copyDebugidForSentry(
202+
bundleName,
203+
outputFolder,
204+
sourcemapOutput,
205+
);
206+
await uploadSourcemapForSentry(
207+
bundleName,
208+
outputFolder,
209+
sourcemapOutput,
210+
);
211+
}
194212
}
195213
resolve(null);
196214
}
@@ -253,6 +271,7 @@ async function compileHermesByteCode(
253271
bundleName,
254272
outputFolder,
255273
sourcemapOutput,
274+
shouldCleanSourcemap,
256275
) {
257276
console.log('Hermes enabled, now compiling to hermes bytecode:\n');
258277
// >= rn 0.69
@@ -309,9 +328,65 @@ async function compileHermesByteCode(
309328
},
310329
);
311330
}
331+
if (shouldCleanSourcemap) {
332+
fs.removeSync(path.join(outputFolder, `${bundleName}.txt.map`));
333+
}
334+
}
335+
336+
async function copyDebugidForSentry(bundleName, outputFolder, sourcemapOutput) {
337+
if (sourcemapOutput) {
338+
const copyDebugidPath =
339+
'node_modules/@sentry/react-native/scripts/copy-debugid.js';
340+
if (!fs.existsSync(copyDebugidPath)) {
341+
return;
342+
}
343+
console.log('Copying debugid');
344+
spawnSync(
345+
'node',
346+
[
347+
copyDebugidPath,
348+
path.join(outputFolder, `${bundleName}.txt.map`),
349+
path.join(outputFolder, `${bundleName}.map`),
350+
],
351+
{
352+
stdio: 'ignore',
353+
},
354+
);
355+
}
312356
fs.removeSync(path.join(outputFolder, `${bundleName}.txt.map`));
313357
}
314358

359+
async function uploadSourcemapForSentry(
360+
bundleName,
361+
outputFolder,
362+
sourcemapOutput,
363+
) {
364+
if (sourcemapOutput) {
365+
const uploadSourcemapPath =
366+
'node_modules/@sentry/cli/bin/sentry-cli';
367+
if (!fs.existsSync(uploadSourcemapPath)) {
368+
return;
369+
}
370+
console.log('Uploading sourcemap');
371+
spawnSync(
372+
'node',
373+
[
374+
uploadSourcemapPath,
375+
'sourcemaps',
376+
'upload',
377+
'--debug-id-reference',
378+
'--strip-prefix',
379+
path.join(outputFolder, bundleName),
380+
path.join(outputFolder, `${bundleName}.map`),
381+
],
382+
{
383+
stdio: 'ignore',
384+
},
385+
);
386+
}
387+
fs.removeSync(path.join(outputFolder, `${bundleName}.map`));
388+
}
389+
315390
async function pack(dir, output) {
316391
console.log('Packing');
317392
fs.ensureDirSync(path.dirname(output));
@@ -718,12 +793,15 @@ export const commands = {
718793
options.platform || (await question('平台(ios/android/harmony):')),
719794
);
720795

721-
const { bundleName, entryFile, intermediaDir, output, dev, sourcemap } =
796+
const { bundleName, entryFile, intermediaDir, output, dev } =
722797
translateOptions({
723798
...options,
724799
platform,
725800
});
726801

802+
const bundleParams = await checkPlugins();
803+
const sourcemap = bundleParams.sourcemap;
804+
727805
const sourcemapOutput = path.join(intermediaDir, `${bundleName}.map`);
728806

729807
const realOutput = output.replace(/\$\{time\}/g, `${Date.now()}`);

src/utils/check-plugin.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { plugins } from './plugin-config';
2+
3+
interface BundleParams {
4+
sentry: boolean;
5+
minify: boolean;
6+
sourcemap: boolean;
7+
[key: string]: any;
8+
}
9+
10+
export async function checkPlugins(): Promise<BundleParams> {
11+
const params: BundleParams = {
12+
sentry: true,
13+
minify: true,
14+
sourcemap: false,
15+
};
16+
17+
for (const plugin of plugins) {
18+
try {
19+
const isEnabled = await plugin.detect();
20+
if (isEnabled && plugin.bundleParams) {
21+
Object.assign(params, plugin.bundleParams);
22+
console.log(`检测到 ${plugin.name} 插件,应用相应打包配置`);
23+
}
24+
} catch (err) {
25+
console.warn(`检测 ${plugin.name} 插件时出错:`, err);
26+
}
27+
}
28+
29+
return params;
30+
}

src/utils/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import AppInfoParser from './app-info-parser';
66
import semverSatisfies from 'semver/functions/satisfies';
77
import chalk from 'chalk';
88
import latestVersion from '@badisi/latest-version';
9+
import { checkPlugins } from './check-plugin';
910

1011
import { read } from 'read';
1112

@@ -225,3 +226,5 @@ export async function printVersionCommand() {
225226
}
226227

227228
export const pricingPageUrl = 'https://pushy.reactnative.cn/pricing.html';
229+
230+
export { checkPlugins };

src/utils/plugin-config.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import fs from 'fs-extra';
2+
3+
interface PluginConfig {
4+
name: string;
5+
bundleParams?: {
6+
minify?: boolean;
7+
[key: string]: any;
8+
};
9+
detect: () => Promise<boolean>;
10+
}
11+
12+
export const plugins: PluginConfig[] = [
13+
{
14+
name: 'sentry',
15+
bundleParams: {
16+
sentry: true,
17+
minify: false,
18+
sourcemap: true,
19+
},
20+
detect: async () => {
21+
try {
22+
await fs.access('ios/sentry.properties');
23+
return true;
24+
} catch {
25+
try {
26+
await fs.access('android/sentry.properties');
27+
return true;
28+
} catch {
29+
return false;
30+
}
31+
}
32+
}
33+
}
34+
];

0 commit comments

Comments
 (0)