@@ -6,214 +6,7 @@ import SentryCli from "@sentry/cli";
66import { logger } from "@sentry/utils" ;
77import * as fs from "fs" ;
88import { glob } from "glob" ;
9- import { createUnplugin , TransformResult , UnpluginInstance , UnpluginOptions } from "unplugin" ;
10- import { createSentryBuildPluginManager } from "./build-plugin-manager" ;
11- import { createDebugIdUploadFunction } from "./debug-id-upload" ;
12- import { Logger } from "./logger" ;
13- import { Options , SentrySDKBuildFlags } from "./types" ;
14- import {
15- CodeInjection ,
16- containsOnlyImports ,
17- generateReleaseInjectorCode ,
18- generateModuleMetadataInjectorCode ,
19- stripQueryAndHashFromPath ,
20- } from "./utils" ;
21-
22- type InjectionPlugin = (
23- injectionCode : CodeInjection ,
24- debugIds : boolean ,
25- logger : Logger
26- ) => UnpluginOptions ;
27- type LegacyPlugins = {
28- releaseInjectionPlugin : ( injectionCode : string ) => UnpluginOptions ;
29- moduleMetadataInjectionPlugin : ( injectionCode : string ) => UnpluginOptions ;
30- debugIdInjectionPlugin : ( logger : Logger ) => UnpluginOptions ;
31- } ;
32-
33- interface SentryUnpluginFactoryOptions {
34- injectionPlugin : InjectionPlugin | LegacyPlugins ;
35- componentNameAnnotatePlugin ?: (
36- ignoredComponents : string [ ] ,
37- injectIntoHtml : boolean
38- ) => UnpluginOptions ;
39- debugIdUploadPlugin : (
40- upload : ( buildArtifacts : string [ ] ) => Promise < void > ,
41- logger : Logger ,
42- createDependencyOnBuildArtifacts : ( ) => ( ) => void ,
43- webpack_forceExitOnBuildComplete ?: boolean
44- ) => UnpluginOptions ;
45- bundleSizeOptimizationsPlugin : ( buildFlags : SentrySDKBuildFlags ) => UnpluginOptions ;
46- getBundlerMajorVersion ?: ( ) => string | undefined ;
47- }
48-
49- /**
50- * Creates an unplugin instance used to create Sentry plugins for Vite, Rollup, esbuild, and Webpack.
51- */
52- export function sentryUnpluginFactory ( {
53- injectionPlugin,
54- componentNameAnnotatePlugin,
55- debugIdUploadPlugin,
56- bundleSizeOptimizationsPlugin,
57- getBundlerMajorVersion,
58- } : SentryUnpluginFactoryOptions ) : UnpluginInstance < Options | undefined , true > {
59- return createUnplugin < Options | undefined , true > ( ( userOptions = { } , unpluginMetaContext ) => {
60- const sentryBuildPluginManager = createSentryBuildPluginManager ( userOptions , {
61- loggerPrefix :
62- userOptions . _metaOptions ?. loggerPrefixOverride ??
63- `[sentry-${ unpluginMetaContext . framework } -plugin]` ,
64- buildTool : unpluginMetaContext . framework ,
65- buildToolMajorVersion : getBundlerMajorVersion ?.( ) ,
66- } ) ;
67-
68- const {
69- logger,
70- normalizedOptions : options ,
71- bundleSizeOptimizationReplacementValues,
72- } = sentryBuildPluginManager ;
73-
74- if ( options . disable ) {
75- return [
76- {
77- name : "sentry-noop-plugin" ,
78- } ,
79- ] ;
80- }
81-
82- if ( process . cwd ( ) . match ( / \\ n o d e _ m o d u l e s \\ | \/ n o d e _ m o d u l e s \/ / ) ) {
83- logger . warn (
84- "Running Sentry plugin from within a `node_modules` folder. Some features may not work."
85- ) ;
86- }
87-
88- const plugins : UnpluginOptions [ ] = [ ] ;
89-
90- // Add plugin to emit a telemetry signal when the build starts
91- plugins . push ( {
92- name : "sentry-telemetry-plugin" ,
93- buildStart ( ) {
94- // Technically, for very fast builds we might miss the telemetry signal
95- // but it's okay because telemetry is not critical for us.
96- // We cannot await the flush here because it would block the build start
97- // which in turn would break module federation builds, see
98- // https://github.com/getsentry/sentry-javascript-bundler-plugins/issues/816
99- void sentryBuildPluginManager . telemetry . emitBundlerPluginExecutionSignal ( ) . catch ( ( ) => {
100- // Nothing for the users to do here. If telemetry fails it's acceptable.
101- } ) ;
102- } ,
103- } ) ;
104-
105- if ( Object . keys ( bundleSizeOptimizationReplacementValues ) . length > 0 ) {
106- plugins . push ( bundleSizeOptimizationsPlugin ( bundleSizeOptimizationReplacementValues ) ) ;
107- }
108-
109- const injectionCode = new CodeInjection ( ) ;
110-
111- if ( ! options . release . inject ) {
112- logger . debug (
113- "Release injection disabled via `release.inject` option. Will not inject release."
114- ) ;
115- } else if ( ! options . release . name ) {
116- logger . debug (
117- "No release name provided. Will not inject release. Please set the `release.name` option to identify your release."
118- ) ;
119- } else {
120- const code = generateReleaseInjectorCode ( {
121- release : options . release . name ,
122- injectBuildInformation : options . _experiments . injectBuildInformation || false ,
123- } ) ;
124- if ( typeof injectionPlugin !== "function" ) {
125- plugins . push ( injectionPlugin . releaseInjectionPlugin ( code . code ( ) ) ) ;
126- } else {
127- injectionCode . append ( code ) ;
128- }
129- }
130-
131- if ( Object . keys ( sentryBuildPluginManager . bundleMetadata ) . length > 0 ) {
132- const code = generateModuleMetadataInjectorCode ( sentryBuildPluginManager . bundleMetadata ) ;
133- if ( typeof injectionPlugin !== "function" ) {
134- plugins . push ( injectionPlugin . moduleMetadataInjectionPlugin ( code . code ( ) ) ) ;
135- } else {
136- injectionCode . append ( code ) ;
137- }
138- }
139-
140- if (
141- typeof injectionPlugin === "function" &&
142- ( ! injectionCode . isEmpty ( ) || options . sourcemaps ?. disable !== true )
143- ) {
144- plugins . push ( injectionPlugin ( injectionCode , options . sourcemaps ?. disable !== true , logger ) ) ;
145- }
146-
147- // Add plugin to create and finalize releases, and also take care of adding commits and legacy sourcemaps
148- const freeGlobalDependencyOnBuildArtifacts =
149- sentryBuildPluginManager . createDependencyOnBuildArtifacts ( ) ;
150- plugins . push ( {
151- name : "sentry-release-management-plugin" ,
152- async writeBundle ( ) {
153- try {
154- await sentryBuildPluginManager . createRelease ( ) ;
155- } finally {
156- freeGlobalDependencyOnBuildArtifacts ( ) ;
157- }
158- } ,
159- } ) ;
160-
161- if ( options . sourcemaps ?. disable !== true ) {
162- if ( typeof injectionPlugin !== "function" ) {
163- plugins . push ( injectionPlugin . debugIdInjectionPlugin ( logger ) ) ;
164- }
165-
166- if ( options . sourcemaps ?. disable !== "disable-upload" ) {
167- // This option is only strongly typed for the webpack plugin, where it is used. It has no effect on other plugins
168- const webpack_forceExitOnBuildComplete =
169- typeof options . _experiments [ "forceExitOnBuildCompletion" ] === "boolean"
170- ? options . _experiments [ "forceExitOnBuildCompletion" ]
171- : undefined ;
172-
173- plugins . push (
174- debugIdUploadPlugin (
175- createDebugIdUploadFunction ( {
176- sentryBuildPluginManager,
177- } ) ,
178- logger ,
179- sentryBuildPluginManager . createDependencyOnBuildArtifacts ,
180- webpack_forceExitOnBuildComplete
181- )
182- ) ;
183- }
184- }
185-
186- if ( options . reactComponentAnnotation ) {
187- if ( ! options . reactComponentAnnotation . enabled ) {
188- logger . debug (
189- "The component name annotate plugin is currently disabled. Skipping component name annotations."
190- ) ;
191- } else if ( options . reactComponentAnnotation . enabled && ! componentNameAnnotatePlugin ) {
192- logger . warn (
193- "The component name annotate plugin is currently not supported by '@sentry/esbuild-plugin'"
194- ) ;
195- } else {
196- componentNameAnnotatePlugin &&
197- plugins . push (
198- componentNameAnnotatePlugin (
199- options . reactComponentAnnotation . ignoredComponents || [ ] ,
200- ! ! options . reactComponentAnnotation . _experimentalInjectIntoHtml
201- )
202- ) ;
203- }
204- }
205-
206- // Add plugin to delete unwanted artifacts like source maps after the uploads have completed
207- plugins . push ( {
208- name : "sentry-file-deletion-plugin" ,
209- async writeBundle ( ) {
210- await sentryBuildPluginManager . deleteArtifacts ( ) ;
211- } ,
212- } ) ;
213-
214- return plugins ;
215- } ) ;
216- }
9+ import { CodeInjection , containsOnlyImports , stripQueryAndHashFromPath } from "./utils" ;
21710
21811/**
21912 * Determines whether the Sentry CLI binary is in its expected location.
@@ -283,18 +76,17 @@ export function globFiles(outputDir: string): Promise<string[]> {
28376 ) ;
28477}
28578
79+ // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
28680export function createComponentNameAnnotateHooks (
28781 ignoredComponents : string [ ] ,
28882 injectIntoHtml : boolean
289- ) : {
290- transform : UnpluginOptions [ "transform" ] ;
291- } {
83+ ) {
29284 type ParserPlugins = NonNullable <
29385 NonNullable < Parameters < typeof transformAsync > [ 1 ] > [ "parserOpts" ]
29486 > [ "plugins" ] ;
29587
29688 return {
297- async transform ( this : void , code : string , id : string ) : Promise < TransformResult > {
89+ async transform ( this : void , code : string , id : string ) {
29890 // id may contain query and hash which will trip up our file extension logic below
29991 const idWithoutQueryAndHash = stripQueryAndHashFromPath ( id ) ;
30092
0 commit comments