Skip to content

Commit f5db486

Browse files
author
Luca Forstner
authored
feat(webpack): Add debug ID injection to the webpack plugin (#198)
1 parent db2d0a4 commit f5db486

File tree

5 files changed

+74
-9
lines changed

5 files changed

+74
-9
lines changed

packages/bundler-plugin-core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"find-up": "5.0.0",
4343
"glob": "9.3.2",
4444
"magic-string": "0.27.0",
45-
"unplugin": "1.0.1"
45+
"unplugin": "1.0.1",
46+
"webpack-sources": "3.2.3"
4647
},
4748
"devDependencies": {
4849
"@babel/core": "7.18.5",

packages/bundler-plugin-core/src/debug-id.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { stringToUUID } from "./utils";
99
const DEBUG_ID_INJECTOR_SNIPPET =
1010
';!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="__SENTRY_DEBUG_ID__",e._sentryDebugIdIdentifier="sentry-dbid-__SENTRY_DEBUG_ID__")}catch(e){}}();';
1111

12-
export function injectDebugIdSnippetIntoChunk(code: string) {
12+
export function injectDebugIdSnippetIntoChunk(code: string, filename?: string) {
1313
const debugId = stringToUUID(code); // generate a deterministic debug ID
14-
const ms = new MagicString(code);
14+
const ms = new MagicString(code, { filename });
1515

1616
const codeToInject = DEBUG_ID_INJECTOR_SNIPPET.replace(/__SENTRY_DEBUG_ID__/g, debugId);
1717

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import util from "util";
3030
import { getDependencies, getPackageJson, parseMajorVersion } from "./utils";
3131
import { glob } from "glob";
3232
import { injectDebugIdSnippetIntoChunk, prepareBundleForDebugIdUpload } from "./debug-id";
33+
import { SourceMapSource } from "webpack-sources";
34+
import type { sources } from "webpack";
3335

3436
const ALLOWED_TRANSFORMATION_FILE_ENDINGS = [".js", ".ts", ".jsx", ".tsx", ".mjs"];
3537

@@ -389,6 +391,68 @@ const unplugin = createUnplugin<Options>((options, unpluginMetaContext) => {
389391
}
390392
},
391393
},
394+
webpack(compiler) {
395+
if (options._experiments?.debugIdUpload) {
396+
// Cache inspired by https://github.com/webpack/webpack/pull/15454
397+
const cache = new WeakMap<sources.Source, sources.Source>();
398+
399+
compiler.hooks.compilation.tap("sentry-plugin", (compilation) => {
400+
compilation.hooks.optimizeChunkAssets.tap("sentry-plugin", (chunks) => {
401+
chunks.forEach((chunk) => {
402+
const fileNames = chunk.files;
403+
fileNames.forEach((fileName) => {
404+
const source = compilation.assets[fileName];
405+
406+
if (!source) {
407+
logger.warn(
408+
"Unable to access compilation assets. If you see this warning, it is likely a bug in the Sentry webpack plugin. Feel free to open an issue at https://github.com/getsentry/sentry-javascript-bundler-plugins with reproduction steps."
409+
);
410+
return;
411+
}
412+
413+
compilation.updateAsset(fileName, (oldSource) => {
414+
const cached = cache.get(oldSource);
415+
if (cached) {
416+
return cached;
417+
}
418+
419+
const originalCode = source.source().toString();
420+
421+
// The source map type is very annoying :(
422+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
423+
const originalSourceMap = source.map() as any;
424+
425+
const { code: newCode, map: newSourceMap } = injectDebugIdSnippetIntoChunk(
426+
originalCode,
427+
fileName
428+
);
429+
430+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
431+
newSourceMap.sources = originalSourceMap.sources as string[];
432+
433+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
434+
newSourceMap.sourcesContent = originalSourceMap.sourcesContent as string[];
435+
436+
const newSource = new SourceMapSource(
437+
newCode,
438+
fileName,
439+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
440+
originalSourceMap,
441+
originalCode,
442+
newSourceMap,
443+
false
444+
) as sources.Source;
445+
446+
cache.set(oldSource, newSource);
447+
448+
return newSource;
449+
});
450+
});
451+
});
452+
});
453+
});
454+
}
455+
},
392456
};
393457
});
394458

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export type Options = Omit<IncludeEntry, "paths"> & {
228228
/**
229229
* Configuration for debug ID upload.
230230
*
231-
* Note: Currently only functional for Vite and Rollup.
231+
* Note: Currently only functional for Vite, Webpack and Rollup.
232232
*/
233233
debugIdUpload?: {
234234
/**

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11752,6 +11752,11 @@ webidl-conversions@^6.1.0:
1175211752
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-6.1.0.tgz#9111b4d7ea80acd40f5270d666621afa78b69514"
1175311753
integrity sha512-qBIvFLGiBpLjfwmYAaHPXsn+ho5xZnGvyGvsarywGNc8VyQJUMHJ8OBKGGrPER0okBeMDaan4mNBlgBROxuI8w==
1175411754

11755+
[email protected], webpack-sources@^3.2.3:
11756+
version "3.2.3"
11757+
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-3.2.3.tgz#2d4daab8451fd4b240cc27055ff6a0c2ccea0cde"
11758+
integrity sha512-/DyMEOrDgLKKIG0fmvtz+4dUX/3Ghozwgm6iPp8KRhvn+eQf9+Q7GWxVNMk3+uCPWfdXYC4ExGBckIXdFEfH1w==
11759+
1175511760
webpack-sources@^1.4.0, webpack-sources@^1.4.1:
1175611761
version "1.4.3"
1175711762
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-1.4.3.tgz#eedd8ec0b928fbf1cbfe994e22d2d890f330a933"
@@ -11760,11 +11765,6 @@ webpack-sources@^1.4.0, webpack-sources@^1.4.1:
1176011765
source-list-map "^2.0.0"
1176111766
source-map "~0.6.1"
1176211767

11763-
webpack-sources@^3.2.3:
11764-
version "3.2.3"
11765-
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-3.2.3.tgz#2d4daab8451fd4b240cc27055ff6a0c2ccea0cde"
11766-
integrity sha512-/DyMEOrDgLKKIG0fmvtz+4dUX/3Ghozwgm6iPp8KRhvn+eQf9+Q7GWxVNMk3+uCPWfdXYC4ExGBckIXdFEfH1w==
11767-
1176811768
webpack-virtual-modules@^0.5.0:
1176911769
version "0.5.0"
1177011770
resolved "https://registry.yarnpkg.com/webpack-virtual-modules/-/webpack-virtual-modules-0.5.0.tgz#362f14738a56dae107937ab98ea7062e8bdd3b6c"

0 commit comments

Comments
 (0)