Is there a tool to remove unused methods in javascript?

前端 未结 7 1908
一生所求
一生所求 2020-12-02 19:50

I\'ve got a collection of javascript files from a 3rd party, and I\'d like to remove all the unused methods to get size down to a more reasonable level.

Does anyone

7条回答
  •  遥遥无期
    2020-12-02 20:47

    I'd like to remove all the unused methods to get size down to a more reasonable level.

    There are a couple of tools available:

    npm install -g fixmyjs
    fixmyjs 
    

    A configurable module that uses JSHint (Github, docs) to flag functions that are unused and perform clean up as well.

    I'm not sure that it removes undefined functions as opposed to flagging them. though it is a great tool for cleanup, it appears to lack compatibility with later versions of ECMAScript (more info below).

    There is also the Google Closure Compiler which claims to remove dead JS but this is more of a build tool.

    Updated

    If you are using something like Babel, consider adding ESLint to your text editor, which can trigger a warning on unused methods and even variables and has a --fix CLI option for autofixing some errors and style issues.

    I like ESLint because it contains multiple plugins for alternate libs (like React warnings if you're missing a prop), allowing you to catch bugs in advance. They have a solid ecosystem.

    As an example: on my NodeJS projects, the config I use is based off of the Airbnb Style Guide.

提交回复
热议问题