New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(webpack): Add debug ID injection to the webpack plugin #198
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,8 @@ import util from "util"; | |
import { getDependencies, getPackageJson, parseMajorVersion } from "./utils"; | ||
import { glob } from "glob"; | ||
import { injectDebugIdSnippetIntoChunk, prepareBundleForDebugIdUpload } from "./debug-id"; | ||
import { SourceMapSource } from "webpack-sources"; | ||
import type { sources } from "webpack"; | ||
|
||
const ALLOWED_TRANSFORMATION_FILE_ENDINGS = [".js", ".ts", ".jsx", ".tsx", ".mjs"]; | ||
|
||
|
@@ -389,6 +391,56 @@ const unplugin = createUnplugin<Options>((options, unpluginMetaContext) => { | |
} | ||
}, | ||
}, | ||
webpack(compiler) { | ||
if (options._experiments?.debugIdUpload) { | ||
compiler.hooks.compilation.tap("sentry-plugin", (compilation) => { | ||
compilation.hooks.optimizeChunkAssets.tap("sentry-plugin", (chunks) => { | ||
chunks.forEach((chunk) => { | ||
const fileNames = chunk.files; | ||
fileNames.forEach((fileName) => { | ||
const source = compilation.assets[fileName]; | ||
|
||
if (!source) { | ||
logger.warn( | ||
"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." | ||
); | ||
return; | ||
} | ||
|
||
compilation.updateAsset(fileName, () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to cache here? So we only inject once? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the cache inspired by webpack/webpack#15454 |
||
const originalCode = source.source().toString(); | ||
|
||
// The source map type is very annoying :( | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any | ||
const originalSourceMap = source.map() as any; | ||
|
||
const { code: newCode, map: newSourceMap } = injectDebugIdSnippetIntoChunk( | ||
originalCode, | ||
fileName | ||
); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
newSourceMap.sources = originalSourceMap.sources as string[]; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
newSourceMap.sourcesContent = originalSourceMap.sourcesContent as string[]; | ||
|
||
return new SourceMapSource( | ||
newCode, | ||
fileName, | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
originalSourceMap, | ||
originalCode, | ||
newSourceMap, | ||
false | ||
) as sources.Source; | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
}, | ||
}; | ||
}); | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: should we hard pin? Also this doesn't change
yarn.lock
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point. Added. The yarn lock didn't change because unplugin itself depended on the exact same version.