Skip to content

Commit 78abb6e

Browse files
committed
better solution for rollup-vite type conflicts
1 parent fb03f50 commit 78abb6e

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

packages/bundler-plugin-core/src/index.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,9 @@ import SentryCli from "@sentry/cli";
44
import { logger } from "@sentry/utils";
55
import * as fs from "fs";
66
import { glob } from "glob";
7-
import MagicString from "magic-string";
7+
import MagicString, { SourceMap } from "magic-string";
88
import * as path from "path";
9-
import {
10-
createUnplugin,
11-
RollupPlugin,
12-
TransformResult,
13-
UnpluginInstance,
14-
UnpluginOptions,
15-
} from "unplugin";
9+
import { createUnplugin, TransformResult, UnpluginInstance, UnpluginOptions } from "unplugin";
1610
import { createSentryBuildPluginManager } from "./build-plugin-manager";
1711
import { createDebugIdUploadFunction } from "./debug-id-upload";
1812
import { Logger } from "./logger";
@@ -273,8 +267,23 @@ const COMMENT_USE_STRICT_REGEX =
273267
// Note: CodeQL complains that this regex potentially has n^2 runtime. This likely won't affect realistic files.
274268
/^(?:\s*|\/\*(?:.|\r|\n)*?\*\/|\/\/.*[\n\r])*(?:"[^"]*";|'[^']*';)?/;
275269

270+
/**
271+
* Simplified `renderChunk` hook type from Rollup.
272+
* We can't reference the type directly because the Vite plugin complains
273+
* about type mismatches
274+
*/
275+
type RenderChunkHook = (
276+
code: string,
277+
chunk: {
278+
fileName: string;
279+
}
280+
) => {
281+
code: string;
282+
map: SourceMap;
283+
} | null;
284+
276285
export function createRollupDebugIdInjectionHooks(): {
277-
renderChunk: RollupPlugin["renderChunk"];
286+
renderChunk: RenderChunkHook;
278287
} {
279288
return {
280289
renderChunk(code: string, chunk: { fileName: string }) {
@@ -313,7 +322,7 @@ export function createRollupDebugIdInjectionHooks(): {
313322
}
314323

315324
export function createRollupModuleMetadataInjectionHooks(injectionCode: string): {
316-
renderChunk: RollupPlugin["renderChunk"];
325+
renderChunk: RenderChunkHook;
317326
} {
318327
return {
319328
renderChunk(code: string, chunk: { fileName: string }) {
@@ -353,7 +362,10 @@ export function createRollupDebugIdUploadHooks(
353362
_logger: Logger,
354363
createDependencyOnBuildArtifacts: () => () => void
355364
): {
356-
writeBundle: RollupPlugin["writeBundle"];
365+
writeBundle: (
366+
outputOptions: { dir?: string; file?: string },
367+
bundle: { [fileName: string]: unknown }
368+
) => Promise<void>;
357369
} {
358370
const freeGlobalDependencyOnDebugIdSourcemapArtifacts = createDependencyOnBuildArtifacts();
359371
return {

packages/vite-plugin/src/index.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
createComponentNameAnnotateHooks,
1111
Logger,
1212
} from "@sentry/bundler-plugin-core";
13-
import { UnpluginOptions, VitePlugin } from "unplugin";
13+
import { UnpluginOptions } from "unplugin";
1414

1515
function viteReleaseInjectionPlugin(injectionCode: string): UnpluginOptions {
1616
return {
@@ -33,14 +33,17 @@ function viteComponentNameAnnotatePlugin(ignoredComponents?: string[]): Unplugin
3333
function viteDebugIdInjectionPlugin(): UnpluginOptions {
3434
return {
3535
name: "sentry-vite-debug-id-injection-plugin",
36-
vite: createRollupDebugIdInjectionHooks() as Partial<VitePlugin>,
36+
// type cast necessary because the return type of createRollupDebugIdInjectionHooks
37+
// is a pass-through from `rollup` which is not compatible with the VitePlugin type
38+
// for some rason
39+
vite: createRollupDebugIdInjectionHooks(),
3740
};
3841
}
3942

4043
function viteModuleMetadataInjectionPlugin(injectionCode: string): UnpluginOptions {
4144
return {
4245
name: "sentry-vite-module-metadata-injection-plugin",
43-
vite: createRollupModuleMetadataInjectionHooks(injectionCode) as Partial<VitePlugin>,
46+
vite: createRollupModuleMetadataInjectionHooks(injectionCode),
4447
};
4548
}
4649

@@ -51,11 +54,7 @@ function viteDebugIdUploadPlugin(
5154
): UnpluginOptions {
5255
return {
5356
name: "sentry-vite-debug-id-upload-plugin",
54-
vite: createRollupDebugIdUploadHooks(
55-
upload,
56-
logger,
57-
createDependencyOnBuildArtifacts
58-
) as Partial<VitePlugin>,
57+
vite: createRollupDebugIdUploadHooks(upload, logger, createDependencyOnBuildArtifacts),
5958
};
6059
}
6160

0 commit comments

Comments
 (0)