zoukankan      html  css  js  c++  java
  • [Webpack] Detect Unused Code with Webpack and unused-files-webpack-plugin

    As you refactor and modify applications, it's difficult to manage and keep track of files as they become unused. Keeping this "dead" code around adds noise to your application and reduces clarity. Just as ESLint can tell us when variables become unused, Webpack (with the help of the unused-files-webpack-plugin) can tell us when entire files become unused. First, we'll install the plugin with npm and save it as a devDependency. Next, we'll use npm run scripts to build a new command that will run Webpack with the plugin. Finally, we'll learn how to use Webpack environment variables to conditionally add plugins to your Webpack config. By the end of the lesson, you'll have a useful cli command you can run to check for unused modules in your Webpack build

    Install:

    npm i -D unused-files-webpack-plugin

    Update scripts:

    "check-unused": "webpack --mode production --env.unused=true --display=errors-only",

    Update webpack.config.js:

    /* eslint-env node */
    const UnusedFilesPlugin = require('unused-files-webpack-plugin').default;
    
    module.exports = (env) => {
        const config = {
            entry: './src/index.js'
        };
    
        if (env && env.unused) {
            config.plugins = [
                new UnusedFilesPlugin({
                    failOnUnused: true,
                    patterns: ['src/*.js']
                })
            ]
        }
    
        return config;
    };
  • 相关阅读:
    HDU_2010——水仙花数
    HDU_2000——ASCII码排序sort()
    HDU_2008——数值统计
    HDU_2005——今天是今年的第几天
    HDU_2004——成绩转换
    HDU_2002——计算求的体积
    InnoDB可重复读隔离级别的底层实现原理
    nginx s reload原理
    Redis主从复制原理总结
    HTTP1.0、HTTP1.1 和 HTTP2.0 的区别
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10339389.html
Copyright © 2011-2022 走看看