Removing Unused Dependencies with grunt-amdcheck

I’ve recently been playing with the grunt-amdcheck plugin to remove unused dependencies from AMD modules. It’s common for define statements in an AMD project to accumulate unused dependencies over time as developers refactor, and it’s a good idea to clean those up from time to time as unused dependencies can:

  • Make it harder to maintain your code
  • Cause the browser to make unnecessary asynchronous requests at run time
  • Increase file size and download times

I worked with Mehdi Shojaei, the plugin’s author, on a couple of pull requests to make the plugin work better for my workflow. Namely I suggested that the plugin try to preserve the whitespace between arguments in define paths and module lists, and that it include an option to not overwrite files and that did not have unused dependencies.

I added the plugin to a couple of projects and ended up removing over 20 from one and 50 from the other. Unfortunately, removing those unused dependencies did not lead to similar decreases in the number of modules loaded at runtime. This is because most of the removed dependencies were required by other modules. However, the code is now more maintainable.

If you’re using this plugin in a Dojo project that uses templated widgets, i18n, and/or xstyle, you need to remember to include the relevant paths in your excludesPaths. For example:

exceptsPaths: [/^dojo\/i18n/, /^dijit\/(form|Dialog|TitlePane|layout)/, /^xstyle/]

All in all, grunt-amdcheck plugin makes it so easy to perform to checks for unused dependencies, that it’s worth bringing into your AMD project and running from time to time.