Terser plugin throws error during prod build for Sentry 8.x
Is there an existing issue for this?
- [x] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
- [x] I have reviewed the documentation https://docs.sentry.io/
- [x] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases
How do you use Sentry?
Sentry Saas (sentry.io)
Which SDK are you using?
@sentry/react
SDK Version
8.35.0
Framework Version
React 17.0.2
Link to Sentry event
No response
Reproduction Example/SDK Setup
export const sentryInit = () => {
Sentry.init({
dsn:
'removed it from here',
release: process.env.RELEASE_HASH,
ignoreErrors: [
// Some errors are ignored here but I guess it's not related
],
denyUrls: [
// Chrome extensions
/extensions\//i,
/^chrome:\/\//i,
/^chrome-extension:\/\//i,
// https://bugs.webkit.org/show_bug.cgi?id=246010
/.*@webkit-masked-url:.*/,
// GTM injected scripts
/bat\.bing\.com\/p\/insights/i,
// Branch.io related errors
/cdn\.branch\.io\/branch-latest/i,
// Translate google com related errors
/translate\.google\.com\/translate_a/i,
// Google tag manager
/googletagmanager\.com\/gtag/i,
],
beforeSend: (event) => {
const errorTraceId = uuidV4()
event.extra = {
...(event?.extra || {}),
errorTraceId,
registeredIdentityId: getSelectedRegisteredIdentityId(),
}
return event
},
beforeBreadcrumb(breadcrumb, hint) {
if (breadcrumb.category?.startsWith('ui')) {
const target = hint?.event?.target
if (target?.ariaLabel)
breadcrumb.message = `${breadcrumb.message} [aria-label="${target.ariaLabel}"]`
}
return breadcrumb
},
integrations: [
Sentry.browserTracingIntegration({ instrumentNavigation: false }),
],
tracesSampleRate: 0.01,
})
}
Steps to Reproduce
- Upgrade Sentry SDK from 7.33 to 8.x (as far as I have checked, this is reproducible in any versions from 8.0 to 8.35)
- Ensure the dev build / runtime works, so the upgrade is successful
- Try to build the project without changing any dependencies other than Sentry SDK.
Webpack version: 5.85.0. We're using TerserPlugin.swcMinify but the same result happens with default minifier and with esbuild minifier as well.
Expected Result
The project prod build runs without issues.
Actual Result
I'm receiving:
ERROR in assets/bundle.63bb6c56f341751cb4b8.js assets/bundle.63bb6c56f341751cb4b8.js from Terser plugin
× Expression expected ╭─[179293:1] 179293 │ 179294 │ /** Exported only for tests. / 179295 │ function instrumentXHR() { 179296 │ if (!types_js__WEBPACK_IMPORTED_MODULE_1_/ .WINDOW */ .m ).XMLHttpRequest) { · ─ 179297 │ return; 179298 │ } ╰────
Caused by: 0: failed to parse input file 1: Syntax Error
Apparently it doesn't like the dot right before XMLHttpRequest.
The error doesn't occur on webpack versions 5.86 and higher. However, we're blocked with the webpack version bump because bumping causes an error in another dependency that we cannot get rid of or fix at the moment, so it would be great to find a fix. Unfortunately, it's hard to understand where does this "exported only for tests" function come from, but it should be from Sentry as it happens after an upgrade attempt from 7 to 8. Also, maybe it's a tree-shaking issue?
Hey @Perchyk yes this file is part of the SDK. Could you share your webpack config?
@chargome, thanks for getting back to me so quickly. It's split into several files and not everything can be related to this problem, so I'll try to combine the important parts to demonstrate the prod config. Please let me know if this is enough.
const { resolve } = require('path')
module.exports = (env) => {
const isDev = env.env === 'dev'
const isProd = env.env === 'prod'
return {
mode: 'production',
entry: './src/app',
output: {
filename: 'assets/bundle.[contenthash].js',
chunkFilename: 'assets/[name].[contenthash].js',
path: resolve(__dirname, '../../public/app'),
publicPath: '/',
},
optimization: {
runtimeChunk: 'single',
// omitting cacheGroups, as Sentry is neither moved into a separate chunk nor is lazy loaded
minimize: true,
minimizer: [
new TerserPlugin({
minify: TerserPlugin.swcMinify,
terserOptions: {
output: {
asciiOnly: false,
},
},
}),
],
},
resolveLoader: {
alias: {
'treeshake-locale-loader': resolve(
__dirname,
'treeshake-locale-loader.js'
),
},
},
module: {
strictExportPresence: true,
rules: [
{
test: /\.(ts|tsx|js|jsx)$/,
include: [resolve(__dirname, '../../src')],
use: [
'thread-loader',
{
loader: 'babel-loader',
options: {
cacheDirectory: !isProd,
plugins: [
isDev && require.resolve('react-refresh/babel'),
].filter(Boolean),
},
},
],
},
{
test: /\.css$/i,
sideEffects: true,
use: ['style-loader', 'css-loader'],
},
{
test: /\.(png|jpg|jpeg|gif)$/, // images in /assets are loaded as files
loader: 'file-loader',
include: [/assets/],
options: {
name: '[path][name].[hash:4].[ext]',
context: resolve(__dirname, '../../src'),
},
},
{
test: /\.(png|jpg|jpeg|gif)$/, // images not in /assets are uri-encoded
use: ['url-loader?name=[hash:8].[ext]'],
exclude: [/assets/],
},
{
test: /\.(svg)$/,
use: ['url-loader?name=[hash:8].[ext]'],
},
{
test: /\.(mp4|webm)$/, // videos
loader: 'file-loader',
options: {
name: '[path][name].[hash:4].[ext]',
context: resolve(__dirname, '../../src'),
},
},
{
test: /\.(woff2?)$/, // fonts
loader: 'file-loader',
options: {
name: '[path][name].[hash:4].[ext]',
context: resolve(__dirname, '../../src'),
},
},
{
test: [/(api[\\/].*index.ts)/i],
sideEffects: false,
},
],
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
alias: {
app: resolve(__dirname, '../../src/'),
assets: resolve(__dirname, '../../src/assets'),
elements: resolve(__dirname, '../../src/elements'),
},
},
plugins: [
// A few HtmlWebpackPlugin instances here - omitting too
new webpack.ProvidePlugin({
process: 'process/browser.js', // had to add .js for Sentry 8 - @sentry/utils was throwing an error saying that this requires an extension
}),
new webpack.DefinePlugin({
'process.env.RELEASE_HASH': JSON.stringify(RELEASE_HASH),
}),
new webpack.LoaderOptionsPlugin({
debug: false,
}),
new CompressionPlugin({
test: /\.js(\?.*)?$/i,
filename: '[path][base].gz',
algorithm: 'gzip',
}),
],
}
}
@Perchyk somehow looks like the terser optimization breaks the multine comment syntax
/** Exported only for tests. /
should be /** Exported only for tests. */
You could check your terser options and maybe tweak the comments attribute in the format options.
@chargome that was a great suggestion but unfortunately it looks like the * was lost during copy-paste but in fact it's still there.
I have tried comments: false for testing purposes but it didn't change anything :( see the screenshot - and note that the dot is highlighted (like before - nothing has changed)
@Perchyk which node version are you on?
@chargome 20.17.0
@Perchyk looking again at this output we just saw that there is one opening bracket and two closing brackets which also seems weird.
The const that we're accessing here is globalThis which might conflict with your setup. Which target are you transpiling your browser code to?
@chargome, esnext according to what I see in tsconfig.json (I guess Babel uses this value?)
Our Babel config is:
presets: [
'@babel/preset-typescript',
[
'@babel/preset-env',
IS_TEST
? { targets: { node: 'current' } }
: { modules: false, useBuiltIns: 'usage', corejs: '3.37.1' },
],
['@babel/preset-react', { runtime: 'automatic' }],
],
// and some plugins but I believe they are not related
I'd like to highlight again that the same setup magically works on any webpack version higher than 5.85. But I haven't been able to find anything related in Webpack release notes...
I just found https://github.com/webpack/webpack/issues/17305, can you try bumping to 5.85.1?
@chargome that's an awesome finding! I didn't know there was a patch version. Yes this fixes the problem for me. However I believe it would be great if it was fixed on Sentry side as well as:
- it adds an implicit peer dependency on webpack >5.85.1 (if the consumer uses webpack of course, but I believe it's still the most popular bundler?)
- it looks like it's a part of the code that is not supposed to slip into the bundle, judging by the comment?
@Perchyk good idea, I'll add that!
For the code - the comment might me slightly misleading. It should end up in the bundle, but the export was added for testing it in isolation.
In the end I have not added the optional peer dependency since this issue seems only be related to very specific webpack configs and we don't want to break e.g. apps that use CRA since they are depending on a lower version as of now.
I have added a small section in our troubleshooting docs on this issue.