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 🙏

© 2026 – Pkg Stats / Ryan Hefner

vue3-tree-shaking

v1.1.1

Published

Tree shaking for Vue 3 SFC - remove unused imports, variables and functions from <script setup>

Downloads

19

Readme

vue3-tree-shaking

npm version license

针对 Vue 3 单文件组件(SFC)的 <script setup> 部分执行 tree-shaking,自动移除模板中未使用的 import、变量、函数声明。

适用于低代码平台、代码生成器等场景,清理自动生成代码中的冗余部分。

安装

npm install vue3-tree-shaking

使用

import { treeShakeVueSFC } from "vue3-tree-shaking";

const sfcCode = `
<template>
  <div>{{ msg }}</div>
</template>
<script setup>
import { ref, computed } from 'vue'
import { unusedUtil } from './utils'

const msg = ref('hello')
const unused = ref('removed')
</script>`;

const { code } = treeShakeVueSFC(sfcCode);
// computed、unusedUtil、unused 将被移除

功能

  • 移除未使用的 import 语句(命名导入、默认导入、命名空间导入)
  • 移除未使用的变量声明、函数声明
  • 移除未使用的对象/数组解构属性
  • 保留副作用调用(onMountedwatchwatchEffect 等)
  • 保留编译器宏(definePropsdefineEmitsdefineExposewithDefaults 等)
  • 保留模板中使用的组件对应的 import(支持 PascalCase 和 kebab-case)
  • 识别 v-forv-slot 的局部作用域变量
  • 支持 <script setup lang="ts">
  • 完整保留 <template><style>、自定义块及其属性

API

treeShakeVueSFC(sfcContent: string)

| 参数 | 类型 | 说明 | |------|------|------| | sfcContent | string | Vue SFC 文件内容 |

返回值:

{
  code: string;           // tree-shaking 后的 SFC 代码
  usedVariables: Set<string>;  // 模板中引用的变量集合
  descriptor: SFCDescriptor;   // 解析后的 SFC 描述符
  babelResult: object;         // Babel 转换结果
}

示例

输入:

<template>
  <MyButton @click="handleClick">{{ msg }}</MyButton>
</template>
<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { formatDate, formatNumber } from './utils'
import MyButton from './MyButton.vue'

const msg = ref('hello')
const count = ref(0)
const double = computed(() => count.value * 2)

onMounted(() => {
  console.log('mounted')
})

function handleClick() {
  msg.value = 'clicked'
}

function unusedFn() {
  return 'never used'
}
</script>

输出:

<template>
  <MyButton @click="handleClick">{{ msg }}</MyButton>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import MyButton from './MyButton.vue'

const msg = ref('hello')

onMounted(() => {
  console.log('mounted')
})

function handleClick() {
  msg.value = 'clicked'
}
</script>

移除了:computedformatDateformatNumbercountdoubleunusedFn

环境支持

  • Node.js
  • 浏览器(通过 @babel/standalone

开发

# 安装依赖
npm install

# 启动开发服务器(demo 页面)
npm run dev

# 运行测试
npm test

# 构建
npm run build

License

GPL-3.0