Coder Social home page Coder Social logo

talljack / unplugin-remove Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 2.0 886 KB

Auto remove some unused code like console and debugger

License: MIT License

HTML 5.83% TypeScript 73.27% JavaScript 11.17% CSS 9.73%
build unplugin vite esbuild plugin remove rollup webpack rolldown rspack

unplugin-remove's Introduction

unplugin-remove

Auto remove console[log|warn|error|info|debug] and debugger in production mode.

Install

[npm|pnpm] i unplugin-remove -D

or

yarn add unplugin-remove -D

Demo

Example: playground/

Vite
// vite.config.ts
import viteRemove from 'unplugin-remove/vite'

export default defineConfig({
  plugins: [
    viteRemove({ /* options */ }),
  ],
})


Rollup
// rollup.config.js
import rollupRemove from 'unplugin-remove/rollup'

export default {
  plugins: [
    rollupRemove({ /* options */ }),
  ],
}


Webpack
// webpack.config.js
module.exports = {
  /* ... */
  plugins: [
    process.env.MODE === 'production' ? require('unplugin-remove/webpack')({ /* options */ }) : null
  ].filter(Boolean)
}


esbuild
// esbuild.config.js
import { build } from 'esbuild'
import esbuildRemove from 'unplugin-remove/esbuild'

build({
  plugins: [esbuildRemove()],
})


Rspack ( ⚠️ experimental)
// rspack.config.js
const RspackPlugin = require('unplugin-remove/rspack').default

module.exports = {
  plugins: [
    new rspack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
    }),
    RspackPlugin(),
  ]
}


Rolldown ( ⚠️ experimental)
// rolldown.config.js
import { defineConfig } from 'rolldown'
import Rolldown from 'unplugin-remove/rolldown'

export default defineConfig({
  plugins: [
    process.env.MODE === 'production' ? Rolldown() : null,
  ]
})


Configuration

The following shows the default values of the configuration

Remove({
  // don't remove console.([log|warn|error|info|debug]) and debugger these module
  external: [],

  // remove console type of these module
  // enum: ['log', 'warn', 'error', 'info', 'debug']
  consoleType: ['log'],

  // filters for transforming targets
  include: [/\.[jt]sx?$/, /\.vue\??/],
  exclude: [/node_modules/, /\.git/],
})

CHANGELOG

You can see CHANGELOG here.

License

MIT License © 2022-PRESENT Talljack

unplugin-remove's People

Contributors

dependabot[bot] avatar talljack avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

unplugin-remove's Issues

ERROR Unexpected token (Note that you need plugins to import files that are not JavaScript)

Hi,

Another issue was recently reported in Nuxt-Security : Baroshem/nuxt-security#343

OP is using the following syntax:

false && console.log('.....')

Which results in ERROR Unexpected token (Note that you need plugins to import files that are not JavaScript)

I guess removing the console.log('.....') part results in invalid JavaScript:

false &&

Suggestion: instead of removing the console.log('.....') part, you could maybe replace it with a noop such as (() => {})()

[vite:build-import-analysis] Failed to parse source for import analysis because the content contains invalid JS syntax

Bug reported in Nuxt-Security : Baroshem/nuxt-security#390
Original issue in Nuxt core : nuxt/nuxt#26011

OP had the initial code with console.log statement:

onUnmounted(() => {
    removeCardServiceSubscription(cardServicesSubscription)
    console.log("🚀 ~ onUnmounted ~ removeCardServiceSubscription(cardServicesSubscription):", removeCardServiceSubscription(cardServicesSubscription))
});

The plugin transformed the initial code by cutting out the beginning of the console.log statement :

[vite:build-import-analysis] Failed to parse source for import analysis because the content contains invalid JS syntax. Install @vitejs/plugin-vue to handle .vue files.
file: D:/proj/components/details/Index.vue:205:64
203: onUnmounted(() => {
204:     removeCardServiceSubscription(cardServicesSubscription)
205:     :", removeCardServiceSubscription(cardServicesSubscription))
                                                                     ^
206: });

Line 205 is indeed invalid Javascript

v0.1.5 is not released to npm

This is the current version that can be installed on npm, but according to the github commit, the current version should be v0.1.5

截圖 2023-11-07 晚上11 02 33

[plugin unplugin-remove-console-debugger] Sourcemap is likely to be incorrect

Description
[plugin unplugin-remove-console-debugger] Sourcemap is likely to be incorrect: a plugin (unplugin-remove-console-debugger) was used to transform files, but didn't generate a sourcemap for the transformation. Consult the plugin documentation for help

Ever since version 1.0.0 the recent implementation removeConsoleLogs is returning rollup warnings.

Using the nuxt stack with nuxt-security, we see this warning popping up a lot of times during compilation. As the warning mentioned it occurs with the sourcemaps "on" and hits the line document.getElementById('app')!.innerHTML = __UNPLUGIN__${count}\ Inside the vite playground

Been looking into this issue for a little bit, but maybe this makes instant sense to you @Talljack . Will give it another shot soon.

Workaround
Currently i added a resolution on this package to version 0.1.7

Example:
You can run th build:vite commaned below to see the mentioned rollup warning.

diff --git a/package.json b/package.json
index a26da67..6fb03d7 100644
--- a/package.json
+++ b/package.json
@@ -83,7 +83,9 @@
     "lint": "eslint src",
     "lint:fix": "eslint src --fix",
     "play:vite": "npm -C playground/vite run dev",
+    "build:vite": "npm run build && npm -C playground/vite run build",
     "play:webpack": "npm -C playground/webpack run serve",
+    "build:webpack": "npm run build && npm -C playground/webpack run build",
     "prepublishOnly": "npm run build",
     "release": "bumpp && npm publish",
     "start": "esno src/index.ts",
diff --git a/playground/vite/package.json b/playground/vite/package.json
index c9d0a58..9beaf7b 100644
--- a/playground/vite/package.json
+++ b/playground/vite/package.json
@@ -1,8 +1,8 @@
 {
-  "type": "module",
   "private": true,
   "scripts": {
-    "dev": "nodemon -w '../src/**/*.ts' -e .ts -x vite"
+    "dev": "nodemon -w '../src/**/*.ts' -e .ts -x vite",
+    "build": "vite build"
   },
   "devDependencies": {
     "unplugin-remove": "workspace:*",

Plugin can cause build failure with Vite

Bug initially reported in Nuxt Security Baroshem/nuxt-security#306

Please see repro and details there

The bug happens when console.log is removed from a lambda void-returning arrow-syntax function with a single instruction.

I guess the parser is a bit too aggressive and transforms :

const test = () => console.log('Test')

Into

const test = () =>

Which is invalid Javascript

Deprecated dependency in magic-string

Hey Buddy,

One of the users of Nuxt Security (which uses unplugin-remove under the hood for removing loggers functionality) reported that one of the dependencies of nuxt-security - unplugin-remove and more specifically magic-string has a deprecated dependency and it results in a warning.

More details Baroshem/nuxt-security#342 (comment)

I would really appreciate if you could take a look at it as using deprecated dependencies should be avoided in general.

If you need any assistance from my side, please just let me know and I will do my best to help :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.