admin管理员组

文章数量:1325408

Hi I deploy my react using vite, however all the source code showing from debugger. I want to hide it and already follow the step:

  1. add "build": "GENERATE_SOURCEMAP=false vite build",
  2. using cross-env
  3. and the last is try to set in vite config:
build: {
      outDir: 'build',
      chunkSizeWarningLimit: 1600,
      assetsDir: './',
      rollupOptions: {
        input: './src/index.jsx'
      },
      sourcemap: 'false'
    },
    sourcemap: {
      server: true,
      client: true,
    },

But all options not working.

Please see this image

I want source code not showing on debugger.

Hi I deploy my react using vite, however all the source code showing from debugger. I want to hide it and already follow the step:

  1. add "build": "GENERATE_SOURCEMAP=false vite build",
  2. using cross-env
  3. and the last is try to set in vite config:
build: {
      outDir: 'build',
      chunkSizeWarningLimit: 1600,
      assetsDir: './',
      rollupOptions: {
        input: './src/index.jsx'
      },
      sourcemap: 'false'
    },
    sourcemap: {
      server: true,
      client: true,
    },

But all options not working.

Please see this image

I want source code not showing on debugger.

Share Improve this question asked Feb 27, 2023 at 8:28 EveEve 1222 gold badges3 silver badges11 bronze badges 1
  • finally I change my vite.config.js. I create new vite react project and copy all my code into new project, because before I use CRA and migrate into VITE. Also using build serve on DO mand. Now all run well, debugger not showing sourcemap. – Eve Commented Mar 8, 2023 at 8:50
Add a ment  | 

3 Answers 3

Reset to default 2

You have to disable source maps. Here's how you can do :

  1. In your package.json, update the build script to include GENERATE_SOURCEMAP=false, like this :
"scripts": {
  "build": "GENERATE_SOURCEMAP=false vite build"
}
  1. Then you have to install the cross-env package :
npm install --save-dev cross-env
  1. THen use cross-env to set the environment variable in the build script :
"scripts": {
  "build": "cross-env GENERATE_SOURCEMAP=false vite build"
}
  1. Finally, in your vite.config.js set the sourcemap option to false :
module.exports = {
  build: {
    sourcemap: false
  }
}

Don't forget to restart your server

You can disable sourcemap in vite.config.ts.

import { defineConfig } from "vite";

export default defineConfig({
  build: { sourcemap: false }
});

For those who are still looking for a resolution, the following config worked for me:

export default defineConfig({
  optimizeDeps: {
    esbuildOptions: {
      sourcemap: false,
    }
  },
  build: {
    sourcemap: false,
    rollupOptions: {
      output: {
        sourcemapExcludeSources: true
      }
    }
  },
  // ... your other config
});

No GENERATE_SOURCEMAP env var is required.

本文标签: javascriptHow to set GENERATESOURCEMAP false in viteStack Overflow