question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react switch to dev mode when use custom mode during vite build

See original GitHub issue

Describe the bug

Using a .env.staging as my environment file, and I use rollup-plugin-visualizer to analyze the bundle. In the visualizer page I found react switch to development mode in the bundle files.

If I use .env.production with exactly the same content, react will switch to production mode.

Reproduction

npm init @vitejs/app vite-project -- --template react-ts
npm install rollup-plugin-visualizer --save-dev

package.json:

{
  "name": "vite-project",
  "version": "0.0.0",
  "scripts": {
    "dev": "vite",
    "build": "tsc && vite build",
    "serve": "vite preview",
    "dist:prod-staging": "tsc && vite build --mode staging"
  },
  "dependencies": {
    "react": "^17.0.0",
    "react-dom": "^17.0.0"
  },
  "devDependencies": {
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "@vitejs/plugin-react-refresh": "^1.3.1",
    "typescript": "^4.1.2",
    "rollup-plugin-visualizer": "^4.2.2",
    "vite": "^2.1.3"
  }
}

vite config

import { defineConfig } from "vite";
import reactRefresh from "@vitejs/plugin-react-refresh";
import visualizer from "rollup-plugin-visualizer";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [reactRefresh(), visualizer({ open: true })],
});

.env.staging

NODE_ENV=production
VITE_SERVE_KIND=manage

after run npm run dist:prod-staging:

image

System Info

System:
    OS: Windows 10 10.0.19042
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
    Memory: 11.64 GB / 31.52 GB
  Binaries:
    Node: 14.15.5 - C:\Program Files\nodejs\node.EXE
    npm: 6.14.11 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    vite: ^2.1.3 => 2.1.3

Logs

C:\Users\alan\projects\vite-test\vite-project>npm run dist:prod-staging

> vite-project@0.0.0 dist:prod-staging C:\Users\alan\projects\vite-test\vite-project
> tsc && vite build --mode staging --debug

  vite:config bundled config file loaded in 119ms +0ms
  vite:config using resolved config: {
  vite:config   plugins: [
  vite:config     'alias',
  vite:config     'react-refresh',
  vite:config     'vite:dynamic-import-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'visualizer',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'rollup-plugin-dynamic-import-variables',
  vite:config     'vite:import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:terser',
  vite:config     'vite:reporter'
  vite:config   ],
  vite:config   mode: 'staging',
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge18', 'firefox60', 'chrome61', 'safari11' ],
  vite:config     polyfillDynamicImport: true,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {},
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     minify: 'terser',
  vite:config     terserOptions: {},
  vite:config     cleanCssOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     brotliSize: true,
  vite:config     chunkSizeWarningLimit: 500
  vite:config   },
  vite:config   configFile: 'C:/Users/alan/projects/vite-test/vite-project/vite.config.ts',
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: 'staging',
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     build: {}
  vite:config   },
  vite:config   root: 'C:/Users/alan/projects/vite-test/vite-project',
  vite:config   base: '/',
  vite:config   resolve: { dedupe: undefined, alias: [ [Object] ] },
  vite:config   publicDir: 'C:\\Users\\alan\\projects\\vite-test\\vite-project\\public',
  vite:config   command: 'build',
  vite:config   isProduction: true,
  vite:config   optimizeCacheDir: 'C:\\Users\\alan\\projects\\vite-test\\vite-project\\node_modules\\.vite',   
  vite:config   server: {},
  vite:config   env: {
  vite:config     VITE_SERVE_KIND: 'manage',
  vite:config     BASE_URL: '/',
  vite:config     MODE: 'staging',
  vite:config     DEV: false,
  vite:config     PROD: true
  vite:config   },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen]
  vite:config   },
  vite:config   createResolver: [Function: createResolver]
  vite:config } +8ms
vite v2.1.3 building for staging...
✓ 25 modules transformed.
dist/assets/favicon.17e50649.svg   1.49kb
dist/assets/logo.ecc203fb.svg      2.61kb
dist/index.html                    0.51kb
dist/assets/index.76acfdae.css     0.76kb / brotli: 0.40kb
dist/assets/index.12a4e508.js      1.52kb / brotli: 0.63kb
dist/assets/vendor.9c34ef57.js     270.05kb / brotli: 69.33kb

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
FPG-Alancommented, Mar 30, 2021

Thank you @anncwb ! after reading your pull request, I found a workaround for this issue:

# vite.config.ts
import { defineConfig } from "vite";
import reactRefresh from "@vitejs/plugin-react-refresh";

export default defineConfig(({ mode }) => ({
  define: {
    "process.env.NODE_ENV": (mode === "staging" && `"production"`) || `"${mode}"`,
  },
  plugins: [reactRefresh()],
}));
0reactions
JackyMaicommented, Apr 12, 2021

@FPG-Alan thanks a lot for getting back to me. I guess I’ll have to wait for the new release containing the merged PR for it to be fixed. May I ask if there’s any ETA for the next release?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to turn on/off ReactJS 'development mode'?
The proper way to disable "dev mode" is through your bundler of choice. webpack. Use the DefinePlugin in your webpack config like so:...
Read more >
Use Vite for React Apps instead of CRA - DEV Community ‍ ‍
Instead of using CRA for creating React App, we can migrate to Vite. Vite is the next generation frontend tooling to build the...
Read more >
Getting Started - Vite
You can try Vite online on StackBlitz. It runs the Vite-based build setup directly in the browser, so it is almost identical to...
Read more >
Strict Mode - React
Strict mode checks are run in development mode only; they do not impact the production build. You can enable strict mode for any...
Read more >
How To Set Up a React Project with Vite - DigitalOcean
Step 1 — Creating a Vite Project ... In this step, you will create a new React project using the Vite tool from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found