getsentry / sentry-javascript-bundler-plugins

JavaScript Bundler Plugins for Sentry
https://sentry.io
BSD 3-Clause "New" or "Revised" License
131 stars 34 forks source link

Should inject banner code after `use strict` #507

Open anothertempore opened 5 months ago

anothertempore commented 5 months ago

Environment

webpack@5.87.0, @sentry/webpack-plugin@2.10.2

Steps to Reproduce

This injection logic should be placed below use strict: https://github.com/getsentry/sentry-javascript-bundler-plugins/blob/febe9ce8dfa34c08ae1b7badc9c9e23182aa2ace/packages/webpack-plugin/src/index.ts#L157-L160

In our case, TerserPlugin removes all the use strict because it is not at the top of the file.

Possible Solution

A possible solution is to do like below:

new BannerPlugin({
  raw: true,
  include: /\.(js|ts|jsx|tsx|mjs|cjs)$/,
  banner: injectionCode,
  footer: true,
})

However, from webpack side, better not to use BannerPlugin to inject code, it's not designed for this. Can refer to https://github.com/webpack/webpack/issues/18202

lforst commented 5 months ago

Hi, thanks for writing in. We will most likely not dedicate much time to this in the short to medium-term future.

For posterity, I used the banner plugin because it was the most reliable way of injecting stuff at the top of bundles while preserving sourcemaps. We would likely want to build something similar ourselves, but I recall that doing non-trivial code modifications and preserving source maps is a major PITA. I legit couldn't get it to work.