npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-deadcodes

v0.0.2

Published

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

Downloads

376

Readme

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'