A vite plugin that deletes console.log in the production environment

Overview

vite-plugin-remove-console

A vite plugin that deletes console.log in the production environment

English | 中文

Install

npm install vite-plugin-remove-console -D
or 
pnpm add vite-plugin-remove-console -D

Usage

Config plugin in vite.config.ts

  • Vue sample config
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import removeConsole from 'vite-plugin-remove-console';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    removeConsole()
  ]
});
Comments
  • Doesn't work in js version, but works perfectly in ts version

    Doesn't work in js version, but works perfectly in ts version

    使用vite build无法移除console

    已按照README执行npm install vite-plugin-remove-console -D

    vite.config.js如下

    import { resolve } from 'path'
    import vue from '@vitejs/plugin-vue'
    import { defineConfig, loadEnv } from 'vite'
    import Icons from 'unplugin-icons/vite'
    import IconsResolver from 'unplugin-icons/resolver'
    import AutoImport from 'unplugin-auto-import/vite'
    import Components from 'unplugin-vue-components/vite'
    import removeConsole from 'vite-plugin-remove-console'
    import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
    import Inspect from 'vite-plugin-inspect'
    import { viteMockServe } from 'vite-plugin-mock'
    
    const pathResolve = (dir) => {
      return resolve(__dirname, '.', dir)
    }
    
    const mockIgnore = (filename) => {
      // 修改后需重启 vite
      // 正则表达式,忽略以 test 开头的文件夹
      // 返回 false 则忽略
      return !/^test/.test(filename)
    }
    
    // 跨域代理重写
    const regExps = (value, reg) => {
      return value.replace(new RegExp(reg, 'g'), '')
    }
    
    const alias = {
      '@': pathResolve('src'),
    }
    
    // https://vitejs.dev/config/
    export default defineConfig((mode) => {
      const env = loadEnv(mode.mode, process.cwd())
      return {
        resolve: { alias },
        base: env.VITE_PUBLIC_PATH,
        server: {
          host: '0.0.0.0',
          port: env.VITE_PORT,
          open: env.VITE_OPEN,
          proxy:
            env.VITE_PROXY_DOMAIN != null
              ? {
                  [env.VITE_PROXY_DOMAIN]: {
                    target: env.VITE_PROXY_DOMAIN_REAL,
                    ws: true,
                    changeOrigin: true,
                    rewrite: (path) => regExps(path, env.VITE_PROXY_DOMAIN),
                  },
                }
              : null,
        },
        plugins: [
          vue(),
          // 线上环境删除console.log
          removeConsole(),
          AutoImport({
            resolvers: [
              ElementPlusResolver({
                importStyle: 'sass',
                directives: true,
                version: '2.1.1',
              }),
            ],
          }),
          Components({
            resolvers: [
              ElementPlusResolver(),
              // Auto register icon components
              // 自动注册图标组件
              IconsResolver(),
            ],
          }),
          Icons({
            compiler: 'vue3',
            autoInstall: true,
          }),
          // mock支持
          viteMockServe({
            ignore: mockIgnore,
            mockPath: 'mock',
            localEnabled: mode.mode === 'mock',
            logger: true,
          }),
          Inspect(),
        ],
        css: {
          preprocessorOptions: {
            scss: {
              additionalData: `@use "@/style/scss/element/index.scss" as *;`,
            },
          },
        },
        build: {
          sourcemap: false,
          brotliSize: false,
          // 消除打包大小超过500kb警告
          chunkSizeWarningLimit: 2000,
        },
      }
    })
    
    opened by Henvy-Mango 5
  • Can you also remove other console methods?

    Can you also remove other console methods?

    Like console.debug(), console.info(). And I'm thinking of console.time() and all its related methods, and console.profile() and all its related methods, and console.trace().

    All of those methods seem helpful to me in development, but I'd probably want to remove in production. Maybe even console.warn() although I think I'd probably want to keep console.error() in production.

    Let me know what you think.

    opened by Evertt 4
  • Could not find a declaration file for module

    Could not find a declaration file for module

    Not sure if this is reproducable, but I encountered the following error:

    vite.config.ts:10:27 - error TS7016: Could not find a declaration file for module 'vite-plugin-remove-console'. '???/node_modules/.pnpm/[email protected]/node_modules/vite-plugin-remove-console/dist/index.js' implicitly has an 'any' type.
      Try `npm i --save-dev @types/vite-plugin-remove-console` if it exists or add a new declaration (.d.ts) file containing `declare module 'vite-plugin-remove-console';`
    
    10 import removeConsole from 'vite-plugin-remove-console'
                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    
    Found 1 error in vite.config.ts:10
    
    opened by jerryc05 4
  • Changing

    Changing "module.exports" by "export default"

    The export default solve this issue: Could not find a declaration file for module 'vite-plugin-remove-console'. './node_modules/vite-plugin-remove-console/dist/index.js' implicitly has an 'any' type. Try npm i --save-dev @types/vite-plugin-remove-console if it exists or add a new declaration (.d.ts) file containing declare module 'vite-plugin-remove-console';ts(7016)

    opened by AndresSepar 1
  • Bug: Plugin not working with react-ts

    Bug: Plugin not working with react-ts

    Plugin doesn't work with react

    import { defineConfig } from "vite";
    import react from "@vitejs/plugin-react";
    import tsConfigPaths from "vite-tsconfig-paths";
    import removeConsole from "vite-plugin-remove-console";
    
    // https://vitejs.dev/config/
    export default defineConfig({
      plugins: [react(), tsConfigPaths(), removeConsole()],
      envPrefix: "ENV_",
      resolve: {
        alias: [
          {
            find: "./runtimeConfig",
            replacement: "./runtimeConfig.browser",
          },
        ],
      },
    });
    
    opened by ShivamJoker 1
Releases(v1.2.0)
Owner
啝裳
从技术到产品是个很享受的过程
啝裳
A Vite plugin for projecting your application onto a remote production page during development.

vite-plugin-proxy-page A Vite plugin for developing an SPA in the context of a deployed production page. What's the Problem? It's an increasingly comm

Alex MacArthur 13 Nov 13, 2022
Vite-plugin-web-extension - A vite plugin for generating cross browser platform, ES module based web extensions.

vite-plugin-web-extension A vite plugin for generating cross browser platform, ES module based web extensions. Features Manifest V2 & V3 Support Compl

Ruben Medina 81 Dec 31, 2022
Vite Svelte plugin to remove console logs in prod.

vite-plugin-svelte-console-remover A Vite plugin that removes all console statements (log, group, dir, error, etc) from Svelte, JS, and TS files durin

Josh Hubbard 29 Oct 13, 2022
✅ Vite plugin for validating your environment variables

This Vite plugin allows you to validate your environment variables at build or dev time. This allows your build/dev-server to fail-fast if your setup

Julien Ripouteau 61 Dec 23, 2022
Vue 3 + Vite + SSR template based on Vite Plugin SSR and inspired by Vitesse

Vite Vue SSR Starter Vue 3 + Vite + SSR template based on Vite Plugin SSR and inspired by Vitesse Features ⚡️ Vue 3, Vite 2, TypeScript ?? Domain-Driv

Oleg Koval 10 Aug 2, 2022
Production-ready fullstack Nuxt 3 starter with a well-working, opinionated configuration

sidebase sidebase is a modern, best-practice, batteries-included fullstack-app starter based on Nuxt 3 and TypeScript. With this nuxt 3 starter you ge

sidestream 392 Jan 1, 2023
A Marko plugin for Vite

@marko/vite A Marko plugin for Vite. Installation npm install @marko/vite Example config import { defineConfig } from "vite"; import marko from "@mark

Marko 49 Nov 26, 2022
A progress bar plugin for Vite.

vite-plugin-progress Display with progress bar when building ?? Install npm i vite-plugin-progress -D # yarn yarn add vite-plugin-progress -D # pn

Jeddy Gong 137 Dec 17, 2022
Laravel plugin for Vite.

Laravel Vite Plugin Introduction Vite is a modern frontend build tool that provides an extremely fast development environment and bundles your code fo

The Laravel Framework 580 Jan 7, 2023
🐝 A vite plugin automatically export files & HMR support

vite-plugin-hot-export Automatically export files with HMR English|简体中文 Why ? When developing, we often need to download some images or svg from the i

Frozen FIsh 54 Nov 12, 2022
Rust dbg! in js powered by rollup/vite plugin system

rollup-plugin-dbg This plugin is also compatible with vite use with rollup import { defineConfig } from "rollup"; import config from "./package.json";

Jason 17 Aug 18, 2022
⚡️🌱 Vite plugin for Twig, transforms twig templates into HTML

⚡️ ?? ViteTwig import twig from '@vituum/vite-plugin-twig' export default { plugins: [ twig({ reload: true, root: null, filte

Vituum 7 Dec 15, 2022
🎉 基于 vite 2.0 + vue 3.0 + vue-router 4.0 + vuex 4.0 + element-plus 的后台管理系统vue3-element-admin

vue3-element-admin ?? 基于 Vite 2.0 + Vue3.0 + Vue-Router 4.0 + Vuex 4.0 + element-plus 的后台管理系统 简介 vue3-element-admin 是一个后台前端解决方案,它基于 vue3 和 element-plu

雪月欧巴 84 Nov 28, 2022
Vite template with TypeScript, Chakra UI, Eslint Airbnb, Prettier

Vite + Typescript + ChakraUI = ❤️ This is a vite template that combines several technologies: Vite React TypeScript ChakraUI Eslint with eslint-config

Lorenzo Rapetti 3 Mar 26, 2022
Integrate Tauri in a Vite project to build cross-platform apps.

vite-plugin-tauri Integrate Tauri in a Vite project to build cross-platform apps Install Make sure to setup your environment for Tauri development. Th

Amr Bashir 95 Dec 15, 2022
A template repository / quick start to build Azure Static Web Apps with a Node.js function. It uses Vue.js v3, Vue Router, Vuex, and Vite.js.

Azure Static Web App Template with Node.js API This is a template repository for creating Azure Static Web Apps that comes pre-configured with: Vue.js

Marc Duiker 6 Jun 25, 2022
⏳ vue3 + electron + ts + vite = mini template

v3-electron ?? Electron16 + Vue3 + Vite2 运行项目 # enter the project directory cd v3-electron # install dependency yarn # develop yarn dev # build exe

UNPany 8 Nov 11, 2022
Using Cypress with Vite, React, TypeScript, MSW and react-query

Vie + Cypress + MSW + react-query Demo Example of using Cypress with Vite, MSW and react-query. Uses the appReady pattern to signal to Cypress when th

Rob Caldecott 9 Jul 16, 2022
Fastify boilerplate with Vite & Vitest

Fastify boilerplate with Vite & Vitest Enhance your Fastify DX with the power of Vite & Vitest. Features ⚡ All the power of Vite (Next Generation Fron

Emmanuel Salomon 31 Dec 13, 2022