Skip to content

Shunjun/rollup-plugin-code-deleter

Repository files navigation

rollup-plugin-code-deleter

NPM Version NPM Downloads bundle JSDocs License

A rollup plugin to delete code by Comments WIP

Why

Maybe you have a common package that is used by multiple packages,like this:

// package A
function renderForPackageB() {
  // ...more codes
}
function renderForPackageC() {
  // ...more codes
}

export const obj = {
  renderForPackageB,
  renderForPackageC,
  ...someOtherProperties
}
// package B
import { obj } from 'A'
obj.renderForPackageB()
// package C
import { obj } from 'A'
obj.renderForPackageC()

If you only build package B, the renderForPackageC will also be in your bundle. The tree-shaking will not work, all codes used in renderForPackageC will bu in bundle.

If you know renderForPackageC does't need in your bundle. you can delete it from obj, to help you shrink the size of your bundle.

Install

npm install rollup-plugin-code-deleter --save-dev

Usage

In Rollup

import codeDeleter from 'rollup-plugin-code-deleter'

export default {
  input: 'src/index.js',
  output: {
    file: 'dist/app.js',
    format: 'umd',
    name: 'app'
  },
  plugins: [
    codeDeleter()
  ]
}

In Vite

import { defineConfig } from 'vite'
import codeDeleter from 'rollup-plugin-code-deleter'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [CodeDeleter()],
})

In Code

you can comment the code where you want to delete

const obj = {
  /** code-deleter */
  a: 1 // the property will be deleted
}

also you can specify which piece of code is deleted

const obj = {
  /** code-deleter: pc */
  a: 1,
  /** code-deleter: mobile */
  b: 2
}

with config:

import codeDeleter from 'rollup-plugin-code-deleter'
export default {
  plugins: [
    codeDeleter({
      delete: ['pc']
    })
  ]
}

the code will output

const obj = {
  /** code-deleter: mobile */
  b: 2
}

License

MIT License © 2023-PRESENT ShunJun