sentry-javascript-bundler-plugins icon indicating copy to clipboard operation
sentry-javascript-bundler-plugins copied to clipboard

Esbuild Javascript bundle replaced by a small sentry script

Open dragoon opened this issue 1 year ago • 10 comments

Environment

"@sentry/esbuild-plugin": "^2.5.0", "esbuild": "^0.18.17",

Steps to Reproduce

Here is my build script

import { build } from 'esbuild';
import  { sentryEsbuildPlugin } from "@sentry/esbuild-plugin";

const devMode = (process.env.NODE_ENV === 'development');
console.log(`${devMode ? 'development' : 'production'} mode bundle`);

// Initialize an empty array for plugins
const plugins = [];

// If not in devMode, add the Sentry plugin
if (!devMode) {
    plugins.push(sentryEsbuildPlugin({
        org: "XXX",
        project: "XXX",
        disable: false,
        authToken: process.env.SENTRY_AUTH_TOKEN,
        sourcemaps: {
            deleteFilesAfterUpload: 'dist/**/*.map',
        },
    }));
}

const base = {
    bundle: true,
    sourcemap: devMode,
    target: ['edge100', 'firefox100', 'chrome87', 'safari15'],
    minify: !devMode,
    entryNames: '[dir]/[name]-[hash]',
    outdir: 'dist',
    plugins: plugins,
    format: "esm"
};

async function buildAndMinify(entryPoint, outDir, target) {
    await build({
        ...base,
        entryPoints: [entryPoint],
        write: true,
        outdir: `dist/${outDir}`,
        target: target || base.target, // use provided target, or default to base.target if no target is provided
    });
}

buildAndMinify('src/index.js', '.').catch(() => process.exit(1));

Expected Result

My actual bundle is generated.

Actual Result

Instead of my usual index.js bundle, here is the script that was generated with sentry esbuild plugin enabled:

var f = (e, d) => () => (e && (d = e(e = 0)), d);
var a, n = f(() => {
    a = typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {};
    a.SENTRY_RELEASE = {id: "XXXX"}
});
n();
n();
(function () {
    try {
        var e = typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {},
            d = new Error().stack;
        d && (e._sentryDebugIds = e._sentryDebugIds || {}, e._sentryDebugIds[d] = "XXXX", e._sentryDebugIdIdentifier = "sentry-dbid-XXXX")
    } catch {
    }
})();
var r = void 0;
export {r as default};

I have no idea what is this script, but my usual bundle is nowhere to be found.

dragoon avatar Aug 03 '23 09:08 dragoon

Hi, thanks for writing in. Unfortunately, I cannot reproduce your issue. Would you mind sharing a Stackblitz or a repo we could pull to reproduce this? Thanks!

lforst avatar Aug 03 '23 09:08 lforst

Thank you for the quick reply. I cannot share the repo unfortunately, I will try to create a sample repo or a stackblitz

dragoon avatar Aug 03 '23 10:08 dragoon

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

github-actions[bot] avatar Sep 08 '23 00:09 github-actions[bot]

@dragoon Do you remember if this issue was resolved, and if so, how you did it? Coming across the same thing and I'm stumped.

jam-fran avatar Mar 05 '24 02:03 jam-fran

This issue was closed due to inactivity. We need reproduction of this issue to be able to resolve it!

lforst avatar Mar 05 '24 08:03 lforst

@jam-fran no, I wasn't able to solve it, just not shipping source maps to sentry at the moment. I didn't have time to create a reproducible repo so far.

dragoon avatar Mar 05 '24 08:03 dragoon

@lforst I was working on a sample repo when I discovered this appears to be related to #471 -- when I added a default export to my app.ts file it worked as expected.

Before:

const app = express()

Sentry.init({
  dsn: env.SENTRY_DSN,
  environment: env.APP_ENV,
  release: env.SENTRY_RELEASE,
})

app.use(Sentry.Handlers.requestHandler())

// Controllers

app.use(Sentry.Handlers.errorHandler())

app.listen(env.PORT, () => {
  console.log(`📡 Server started on port ${env.PORT}`)
})

After:

const app = express()

Sentry.init({
  dsn: env.SENTRY_DSN,
  environment: env.APP_ENV,
  release: env.SENTRY_RELEASE,
})

app.use(Sentry.Handlers.requestHandler())

// Controllers

app.use(Sentry.Handlers.errorHandler())

app.listen(env.PORT, () => {
  console.log(`📡 Server started on port ${env.PORT}`)
})

export default app

jam-fran avatar Mar 05 '24 14:03 jam-fran

Great to know @jam-fran, it works indeed, I don't use any framework, my entry point is a plain javascript file, when I add export default somefunction, js file is generated correctly.

dragoon avatar Mar 05 '24 15:03 dragoon

That is odd... Maybe we can fix this somehow.

lforst avatar Mar 05 '24 17:03 lforst

Also ran into this with @sentry/esbuild-plugin, Sentry replaced my entire script file with:

import { createRequire as _createRequire } from "module"
const require = _createRequire(import.meta.url)
var n =
  typeof window < "u"
    ? window
    : typeof global < "u"
    ? global
    : typeof self < "u"
    ? self
    : {}
n.SENTRY_RELEASE = { id: "x" }
;(function () {
  try {
    var e =
        typeof window < "u"
          ? window
          : typeof global < "u"
          ? global
          : typeof self < "u"
          ? self
          : {},
      d = new Error().stack
    d &&
      ((e._sentryDebugIds = e._sentryDebugIds || {}),
      (e._sentryDebugIds[d] = "x"),
      (e._sentryDebugIdIdentifier =
        "sentry-dbid-x"))
  } catch {}
})()
var b = void 0
export { b as default }

Adding an export to the file, even though I don't need it, fixed the issue. This was a tough one to debug 😄

codybrouwers avatar May 17 '24 20:05 codybrouwers