1- import { createUnplugin } from "unplugin" ;
1+ import { createUnplugin , UnpluginOptions } from "unplugin" ;
22import MagicString from "magic-string" ;
33import { Options , BuildContext } from "./types" ;
44import {
@@ -26,8 +26,8 @@ import { makeMain } from "@sentry/node";
2626import os from "os" ;
2727import path from "path" ;
2828import fs from "fs" ;
29- import util from "util" ;
30- import { getDependencies , getPackageJson , parseMajorVersion } from "./utils" ;
29+ import { promisify } from "util" ;
30+ import { getDependencies , getPackageJson , parseMajorVersion , stringToUUID } from "./utils" ;
3131import { glob } from "glob" ;
3232import { injectDebugIdSnippetIntoChunk , prepareBundleForDebugIdUpload } from "./debug-id" ;
3333import { SourceMapSource } from "webpack-sources" ;
@@ -39,6 +39,10 @@ const releaseInjectionFilePath = require.resolve(
3939 "@sentry/bundler-plugin-core/sentry-release-injection-file"
4040) ;
4141
42+ const esbuildDebugIdInjectionFilePath = require . resolve (
43+ "@sentry/bundler-plugin-core/sentry-esbuild-debugid-injection-file"
44+ ) ;
45+
4246/**
4347 * The sentry bundler plugin concerns itself with two things:
4448 * - Release injection
@@ -66,7 +70,7 @@ const releaseInjectionFilePath = require.resolve(
6670 *
6771 * This release creation pipeline relies on Sentry CLI to execute the different steps.
6872 */
69- const unplugin = createUnplugin < Options > ( ( options , unpluginMetaContext ) => {
73+ const unplugin = createUnplugin < Options , true > ( ( options , unpluginMetaContext ) => {
7074 const internalOptions = normalizeUserOptions ( options ) ;
7175
7276 const allowedToSendTelemetryPromise = shouldSendTelemetry ( internalOptions ) ;
@@ -113,7 +117,9 @@ const unplugin = createUnplugin<Options>((options, unpluginMetaContext) => {
113117 let transaction : Transaction | undefined ;
114118 let releaseInjectionSpan : Span | undefined ;
115119
116- return {
120+ const plugins : UnpluginOptions [ ] = [ ] ;
121+
122+ plugins . push ( {
117123 name : "sentry-plugin" ,
118124 enforce : "pre" , // needed for Vite to call resolveId hook
119125
@@ -317,7 +323,31 @@ const unplugin = createUnplugin<Options>((options, unpluginMetaContext) => {
317323 } )
318324 ) . filter ( ( p ) => p . endsWith ( ".js" ) || p . endsWith ( ".mjs" ) || p . endsWith ( ".cjs" ) ) ;
319325
320- const sourceFileUploadFolderPromise = util . promisify ( fs . mkdtemp ) (
326+ if ( unpluginMetaContext . framework === "esbuild" ) {
327+ await Promise . all (
328+ debugIdChunkFilePaths . map ( async ( debugIdChunkFilePath ) => {
329+ const chunkFileContents = await promisify ( fs . readFile ) (
330+ debugIdChunkFilePath ,
331+ "utf-8"
332+ ) ;
333+
334+ const debugId = stringToUUID ( chunkFileContents ) ;
335+
336+ const newChunkFileContents = chunkFileContents . replace (
337+ / _ _ S E N T R Y _ D E B U G _ I D _ _ / g,
338+ debugId
339+ ) ;
340+
341+ await promisify ( fs . writeFile ) (
342+ debugIdChunkFilePath ,
343+ newChunkFileContents ,
344+ "utf-8"
345+ ) ;
346+ } )
347+ ) ;
348+ }
349+
350+ const sourceFileUploadFolderPromise = promisify ( fs . mkdtemp ) (
321351 path . join ( os . tmpdir ( ) , "sentry-bundler-plugin-upload-" )
322352 ) ;
323353
@@ -455,7 +485,23 @@ const unplugin = createUnplugin<Options>((options, unpluginMetaContext) => {
455485 } ) ;
456486 }
457487 } ,
458- } ;
488+ } ) ;
489+
490+ if ( unpluginMetaContext . framework === "esbuild" ) {
491+ if ( internalOptions . _experiments . debugIdUpload ) {
492+ plugins . push ( {
493+ name : "sentry-esbuild-debug-id-plugin" ,
494+ esbuild : {
495+ setup ( { initialOptions } ) {
496+ initialOptions . inject = initialOptions . inject || [ ] ;
497+ initialOptions . inject . push ( esbuildDebugIdInjectionFilePath ) ;
498+ } ,
499+ } ,
500+ } ) ;
501+ }
502+ }
503+
504+ return plugins ;
459505} ) ;
460506
461507function handleError (
0 commit comments