In order to refactor a client-side project, i'm looking for a safe way to find (and delete) unused code.
What tools do you use to find unused/dead code in large react projects? Our product has been in development for some years, and it is getting very hard to manually detect code that is no longer in use. We do however try to delete as much unused code as possible.
Suggestions for general strategies/techniques (other than specific tools) are also appreciated.
Thank you
Solution:
For node projects, run the following command in your project root:
npx unimported
If you're using flow type annotations, you need to add the --flow flag:
npx unimported --flow
Source & docs: https://github.com/smeijer/unimported
Outcome:
Background
Just like the other answers, I've tried a lot of different libraries but never had real success.
I needed to find entire files that aren't being used. Not just functions or variables. For that, I already have my linter.
I've tried deadfile, unrequired, trucker, but all without success.
After searching for over a year, there was one thing left to do. Write something myself.
unimported starts at your entry point, and follows all your import/require statements. All code files that exist in your source folder, that aren't imported, are being reported.
Note, at this moment it only scans for source files. Not for images or other assets. As those are often "imported" in other ways (through tags or via css).
Also, it will have false positives. For example; sometimes we write scripts that are meant to simplify our development process, such as build steps. Those aren't directly imported.
Also, sometimes we install peer dependencies and our code doesn't directly import those. Those will be reported.
But for me, unimported is already very useful. I've removed a dozen of files from my projects. So it's definitely worth a shot.
If you have any troubles with it, please let me know. Trough github issues, or contact me on twitter: https://twitter.com/meijer_s
Solution for Webpack: UnusedWebpackPlugin
I work on a big front-end React project (1100+ js files) and stumbled upon the same problem: how to find out which files are unused anymore?
I've tested the next tools so far:
findead
deadfile
unrequired
None of them really worked. One of the reason is that we use "not standard" imports. In additional to the regular relative paths in our imports we also use paths resolved by the webpack resolve feature which basically allows us to use neat import 'pages/something' rather than cumbersome import '../../../pages/something'.
UnusedWebpackPlugin
So here is the solution I've finally come across thanks to Liam O'Boyle (elyobo) #GitHub:
https://github.com/MatthieuLemoine/unused-webpack-plugin
It's a webpack plugin so it's gonna work only if your bundler is webpack.
I personaly find it good that you don't need to run it separately but instead it's built into your building process throwing warnings when something is not ok.
Our research topic: https://github.com/spencermountain/unrequired/issues/6
Libraries such as unrequired and deadcode only support legacy code.
In order to find the unused assets, to remove manually, you can use deadfile
library:https://m-izadmehr.github.io/deadfile/
Out of box support for ES5, ES6, React, Vue, ESM, CommonJs.
It supports import/require and even dynamic import.
It can simply find unused files, in any JS project.
Without any config, it supports ES6, React, JSX, and Vue files:
First of all, very good question, in large project coders usually try many lines of code test and at the end of result, hard to find the unused code.
There is two possible that must be work for you - i usually do whenever i need to remove and reduce the unused code into my project.
1st way WebStorm IDE:
If you're using the web-storm IDE for JS development or React JS / React Native or Vue js etc it's tell us and indicate us alote of mention with different color or red warning as unused code inside the editor
but it's not works in your particular scenario there is another way to remove the unused code .
2nd Way unrequired Library: (JSX is not supported)
The second way to remove the unused code inside the project is unrequired library you can visit here : unrequired github
another library called depcheck under NPM & github here
Just follow their appropriate method - how to use them you will fix this unused issue easily
Hopefully that helps you
I think the easiest solution for a create-react-app bootstrapped application is to use ESLint. Tried using various webpack plugins, but ran into out of memory issues with each plugin.
Use the no-unused-modules which is now a part of eslint-plugin-import.
After setting up eslint, installing eslint-plugin-import, add the following to the rules:
"rules: {
...otherRules,
"import/no-unused-modules": [1, {"unusedExports": true}]
}
My approach is an intensive use of ESlint and make it run both on IDE ad before every push.
It points out unused variables, methods, imports and so on.
Webpack (which has too nice plugins for dead code detection) take care about avoiding to bundle unimported code.
findead
With findead you can find all unused components in your project. Just install and run:
Install
npm i -g findead
Usage
findead /path/to/search
This question recalls me that react by default removes the deadcode from the src when you run the build command.
Notes:
you need to run build command only when you want to ship your app to production.
Im trying to uglify a script with code kit that contains references to async functions but I'm having to transpile it with babel in order for it to work. However after checking the console on my site I get smacked with an error 'regeneratorRuntime is not defined' where the arrow functions are supposed to be.
Now I've done a lot of googling and everyone points to do something with npm which I'm not familiar with at all. Is there any settings in codeKit I am missing or require to do in order for it to work.
If not, please could you explain like I'm five how to fix this issue through npm.
Big thanks in advance!
Your Codekit Babel configuration seems to be wrong.
Arrow functions are implemented since ES2015 and async functions since ES2017.
You should activate related plugins in case you targeted old browsers.
I have written a bunch of Javascript code. I was never aware of the fact that there are multiple JS 'versions', like ES5 and ES6.
I now have this project hosted on Github, and somebody pointed out that because i'm using ES6 code, I might need to convert it to ES5 with Babel.
However, I have no idea which parts of my code use ES6. I could read all of the ES6 specifications, but is there some kind of tool/checker which marks all of the ES6 code in my project?
http://jshint.com/ or http://www.jslint.com/ - will detect ES6 specific specifications by just adding your code in the console
Add it into the Babel repl and see if it changes:
https://babeljs.io/repl/
:-) Hope that helps
Other than that it might be best to setup es6 with babel using webpack, gulp, rollup etc
So that if you write es6 or es5 it will automatically get converted and you can learn some new features on the way while still supporting es5 only browsers
Ive seen other questions regarding this issue - Is there a way to turn on ES6/ES7 syntax support in vscode? - but this has now been addressed by Microsoft and implemented in VSCode.
However, I have downloaded the latest version of VSCode, and cloned the example es6 repo from GitHub, and it seems to not be working
To turn ES6/ES7 support, the best way is to use ESLint with dedicated parser ( babel, etc...). I try it if you're interested: VSCode Linter ES6 ES7 Babel linter.
If you're using eslint with npm, you may need the .eslintrc file in the root folder of your project. See my answer to this question and the docs for the ESLint VSCode extension.
I'm working on a Dojo project that would like to start using Babel (aka 6to5) to transpile ES6/ES7 features to ES5. (I think the same question would apply to anyone using Dojo and CoffeeScript or TypeScript.)
Everything works fine if we run the Babel transpiler before opening pages on our website, but that's really tedious for development. We'd like to configure Dojo's AMD loader to automatically transpile JS files as they're loaded.
Is this possible? It seems like it might be possible if we define/declare an AMD plugin, but then we'd have to put a prefix on literally all of our dependencies, e.g. require["babel!myfile.js", "babel!anotherfile.js"] which seems unnecessary when we know that all of our JS should be transpiled.
Is there a way to tell Dojo/requirejs to automatically transpile everything?