vite icon indicating copy to clipboard operation
vite copied to clipboard

`plugin-react`: Fails to bundle `@sentry/react` if `mode` is not `production`

Open BeeeQueue opened this issue 1 year ago • 4 comments

Describe the bug

I am using Vite in a project using @sentry/react, and when I vite build the project with a --mode other than production it fails with the message seen in the Logs section.

I've tried to use the exclude option to exclude /node_modules/ and various other paths but they did not make a difference.

This only happens with plugin-react included and using jsxRuntime: 'automatic'

Reproduction

https://github.com/BeeeQueue/vite-sentry-repro/actions/runs/2732156122

System Info

System:
    OS: macOS 11.6.7
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 214.50 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.16.0 - ~/nvm-rust/shims/node
    Yarn: 1.22.15 - ~/nvm-rust/shims/yarn
    npm: 8.11.0 - ~/nvm-rust/shims/npm
  Browsers:
    Chrome: 103.0.5060.134
    Firefox: 102.0
    Safari: 15.5
  npmPackages:
    @vitejs/plugin-react: 2.0.0 => 2.0.0
    vite: 3.0.2 => 3.0.2

Used Package Manager

pnpm

Logs

Click to expand!
[commonjs--resolver] /Users/ahaglund/projects/vite-sentry-repro/node_modules/.pnpm/@[email protected][email protected]/node_modules/@sentry/react/esm/profiler.js: Duplicate __self prop found. You are most likely using the deprecated transform-react-jsx-self Babel plugin. Both __source and __self are automatically set when using the automatic runtime. Please remove transform-react-jsx-source and transform-react-jsx-self from your Babel config. (This is an error on an internal node. Probably an internal error. Location has been estimated.)
  114 |   var Wrapped = (props) => (
  115 |     React.createElement(Profiler, { ...options, name: componentDisplayName, updateProps: props, __self: this, __source: {fileName: _jsxFileName, lineNumber: 133}}
> 116 |       , React.createElement(WrappedComponent, { ...props, __self: this, __source: {fileName: _jsxFileName, lineNumber: 134}} )
      |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  117 |     )
  118 |   );
  119 |
file: /Users/ahaglund/projects/vite-sentry-repro/node_modules/.pnpm/@[email protected][email protected]/node_modules/@sentry/react/esm/profiler.js

Validations

BeeeQueue avatar Jul 25 '22 12:07 BeeeQueue

From looking into it more, it seems like using --mode staging that the docs recommend for building "production-like behavior, but with slightly different env variables" also changes import.meta.env.PROD to be false - meaning it produces a development build.

BeeeQueue avatar Jul 25 '22 12:07 BeeeQueue

Which probably would be why it also seems to cause plugin-react to use the development react version

BeeeQueue avatar Jul 25 '22 12:07 BeeeQueue

Had the same problem. What helps is to set the environment variables NODE_ENV=production, e.g., with cross-env in your package.json script build:

// package.json
{
  // ...
   "scripts": {
     // ...
     "build": "cross-env NODE_ENV=production tsc && vite build",
     // ...
   },
   // ...
}

I also think the mode should not alter the PROD environment variable in Vite, i.e., sometimes not making a production build.

pantajoe avatar Jul 26 '22 05:07 pantajoe

Had the same problem. Solved by adding NODE_ENV=production to .env.staging file.

// package.json
{
  // ...
   "scripts": {
     // ...
     "build:staging": "NODE_ENV=production tsc && vite build --mode staging",
     // ...
   },
   // ...
}
# .env.staging
NODE_ENV=production
VITE_APP_TITLE=My App (staging)

borivojevic avatar Jul 26 '22 07:07 borivojevic

Had the same problem. Solved by adding NODE_ENV=production to .env.staging file.

// package.json
{
  // ...
   "scripts": {
     // ...
     "build:staging": "NODE_ENV=production tsc && vite build --mode staging",
     // ...
   },
   // ...
}
# .env.staging
NODE_ENV=production
VITE_APP_TITLE=My App (staging)

I think it can be simplified by just adding this.

"build:staging": "tsc && NODE_ENV=production vite build --mode staging",

rohmanhm avatar Aug 09 '22 02:08 rohmanhm

I am still having the issue with --mode staging with vite 3.0.9. Issue is only fixed running dev in production mode

pietrofxq avatar Sep 05 '22 19:09 pietrofxq