🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

vite-plugin-deadcodes

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-plugin-deadcodes

Vite plugin to detect unused files. JS, TS, React, Vue projects are supported.

0.0.2
latest
Source
npm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

vite-plugin-deadcodes

A vite plugin to detect unused files, which is similar to webpack-deadcode-plugin.

The detected result be like:

output example

Supported File Types

Javascript, typescript, react, vue projects are supported:

  • ✅ javascript (.js)
  • ✅ typescript (.ts)
  • ✅ react jsx (.jsx|.tsx)
  • ✅ vue SFC or jsx|tsx files (.vue|.jsx|.tsx)
  • ✅ stylesheet (.css|.less|.scss|...)
  • ✅ assets (.svg|.img|.png|.mp4|...)
  • ✅ ...
  • ❌ Typescript files which are composed of types only (see below)
  • ❌ Stylesheet files which are imported by @import statements (see below)

Installation

# npm
npm install vite-plugin-deadcodes --save-dev

# yarn
yarn add -D vite-plugin-deadcodes

# pnpm
pnpm add -D vite-plugin-deadcodes

Usage

Step 1: config plugin

// vite.config.js
import { defineConfig } from 'vite';
import vitePluginDeadcodes from 'vite-plugin-deadcodes';

export default defineConfig({
  plugins: [
    vitePluginDeadcodes(),
  ]
});

Or use with options:

// vite.config.js
import { defineConfig } from 'vite';
import vitePluginDeadcodes from 'vite-plugin-deadcodes';

export default defineConfig({
  plugins: [
    vitePluginDeadcodes({
      src: '/path/to/src',
      emit: '/path/to/result/deadcodes.json',
      excludes: ['**/*.d.ts', '**/*.(stories|spec).(js|jsx)'],
    }),
  ]
});

Step 2: run build command

This plugin will only run in build mode.

npx vite build

Options

interface Options {
    src?: string;
    emit?: boolean | string;
    excludes?: string[];
    console?: boolean;
}

options.src (default: path.join(process.cwd(), 'src'))

The source dir you want to detect.

options.emit (default: path.join(process.cwd(), 'deadcodes.json'))

The path of the result file.

vitePluginDeadcodes({
  emit: '/path/to/result/deadcodes.json'
})

options.excludes (default: [])

The files that should be ignored.

vitePluginDeadcodes({
  excludes: ['**/*.d.ts', '**/*.(stories|spec).(js|jsx)'],
})

options.console (default: false)

Set true to show result in the console.

Addition

Type only files

Typescript files that were composed of types only, these files will be removed by typescript parser. Example:

export type Foo = 'a' | 'b';

export interface Bar {
  name: string;
}

If this file contains some exports that are not type, then it can be detected. Like:

export type Foo = 'a' | 'b';

export interface Bar {
  name: string;
}

// This function will not be removed by ts parser
export const add = (a: number, b: number): number => a + b;

Stylesheet By Import Statement

In the example below, styles/bar.less can not be detected, which is imported by @import statement.

Planing to fix it in the next version.

// foo.less
@import '../styles/bar.less'

.name {
  color: blue;
}
// demo.js
import './foo.less'

Keywords

vite

FAQs

Package last updated on 05 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts