vue2,webpack 老项目清除无用的文件

清理无用文件肝的眼睛冒金星,不妨来试试webpack插件。

原理就是获取指定目录的全部文件,排除代码依赖的文件,就是不依赖的文件,也就是无用的文件,使用shelljs删除文件

插件仅限在webpack3、webpack4下使用

插件使用

javascript 复制代码
const UnusedFilesWebpackPlugin = require('./unused-files-webpack-plugin')

plugins: [
//...其他插件
    new UnusedFilesWebpackPlugin({
        root: './src', // 项目目录
        output: './unused-files.json', // 输出文件列表
        clean: true, // 是否删除文件,true,删除;false,不删除
        exclude: ['src/assets/font', 'src/assets/icon-font'], // 排除文件列表

    })
]

插件源码

ini 复制代码
const fs = require("fs");
const glob = require("glob");
const path = require("path");
const shelljs = require("shelljs");

class CleanUnusedFilesPlugin {
    constructor(options) {
        this.opts = options;
    }
    apply(compiler) {
        let _this = this;
        compiler.plugin("after-emit", function(compilation, done) {
            _this.findUnusedFiles(compilation, _this.opts);
            done();
        });
    }

    /**
     * 获取依赖的文件
     */
    getDependFiles(compilation) {
        return new Promise((resolve, reject) => {
            const dependedFiles = [...compilation.fileDependencies].reduce(
                (acc, usedFilepath) => {
                    if (!~usedFilepath.indexOf("node_modules")) {
                        acc.push(usedFilepath);
                    }
                    return acc;
                },
                []
            );
            resolve(dependedFiles);
        });
    }

    /**
     * 获取项目目录所有的文件
     */
    getAllFiles(pattern) {
        return new Promise((resolve, reject) => {
            glob(
                pattern,
                {
                    nodir: true
                },
                (err, files) => {
                    if (err) {
                        throw err;
                    }
                    const out = files.map(item => path.resolve(item));
                    resolve(out);
                }
            );
        });
    }

    dealExclude(path, unusedList) {
        let exclude = [];
        if (Array.isArray(path)) {
            // 兼容传入的是数组路径
            exclude = path;
        } else if (typeof path == "string") {
            try {
                // 读取文件里的数组字符串
                const file = fs.readFileSync(path, "utf-8");
                exclude = JSON.parse(file) || [];
            } catch (error) {
                console.log(error);
            }
        }
        console.log(exclude, "exclude");
        if (process.platform == "win32") {
            // windows 需要兼容路径 D:\\workspace\\src\\App.vue
            unusedList = unusedList.map(item => item.replace(/\\/g, "/"));
        }
        let result = unusedList.filter(unused => {
            return !exclude.some(item => ~unused.indexOf(item));
        });
        return result;
    }

    async findUnusedFiles(compilation, config = {}) {
        const {
            root = "./src",
            output = "./unused-files.json",
            clean = false,
            exclude = false
        } = config;
        const pattern = root + "/**/*";
        try {
            const allChunks = await this.getDependFiles(compilation);
            const allFiles = await this.getAllFiles(pattern);
            let unUsed = allFiles.filter(item => !~allChunks.indexOf(item));
            if (exclude) {
                unUsed = this.dealExclude(exclude, unUsed);
            }
            if (typeof output === "string") {
                fs.writeFileSync(output, JSON.stringify(unUsed, null, 4));
            } else if (typeof output === "function") {
                output(unUsed);
            }
            if (clean) {
                unUsed.forEach(file => {
                    shelljs.rm(file);
                    console.log(`remove file: ${file}`);
                });
            }
            return unUsed;
        } catch (err) {
            throw err;
        }
    }
}

module.exports = CleanUnusedFilesPlugin;
相关推荐
William_Xu31 分钟前
Vue 2 与 Vue 3 生命周期对比
前端
Hilaku1 小时前
为什么你的大文件断点续传,一遇到弱网就崩溃?
前端·javascript·程序员
胡哈2 小时前
React Compiler:让开发者忘掉 memo 这件事
前端·react.js
xiyueyezibile2 小时前
“自迭代” Skill 的 team-pitfalls 的演进
前端·后端·ai编程
林恒smileZAZ2 小时前
`Array(100).map(() => 1)` 为什么全为空?
前端·javascript
小巧的砖头2 小时前
C#会重蹈覆辙吗?系列之2:反射及元数据的性能问题
开发语言·前端·c#
KaMeidebaby2 小时前
卡梅德生物技术快报|蛋白的叠氮基修饰:实操解析:核酸模板耦合蛋白的叠氮基修饰实现靶蛋白定点共价标记
前端·人工智能·物联网·算法·百度
濮水大叔3 小时前
在大型 Vue 项目中,如何有效管理复杂的状态?“散装版” vs “套装版”
前端·vue.js·typescript
玄星啊3 小时前
可修改:代码的生存底线
前端·ai编程
没落英雄3 小时前
3. DeepAgents 实战 - Memory Skills 与上下文工程
前端·人工智能·架构