Skip to content

Instantly share code, notes, and snippets.

@joonas-fi
Created November 3, 2016 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joonas-fi/dbf839f09fdc737eca5f5531de4efa94 to your computer and use it in GitHub Desktop.
Save joonas-fi/dbf839f09fdc737eca5f5531de4efa94 to your computer and use it in GitHub Desktop.
Finding unused AMD imports with amdextract without Grunt or Gulp etc.
#!/usr/bin/nodejs
// prerequisites: $ npm install -g amdextract@2.1.14
// Run this:
// $ export NODE_PATH=/usr/local/lib/node_modules # stupid nodejs
// $ find src/ -name '*.js' -not -path '*/vendor/*' -not -path '*/build/*' | bin/find-unused-amd-imports.js
var fs = require('fs');
var amdextract = require('amdextract');
// grab paths to scan from stdin
var paths = fs.readFileSync('/dev/stdin', { encoding: 'utf-8' }).split("\n");
for (var i = 0; i < paths.length; ++i) {
var path = paths[i];
if (!path) { continue; }
var content = fs.readFileSync(path);
var result = amdextract.parse(content);
console.log(path + ':' + result.results.length + ' modules detected.');
if (result.results.length === 0) {
console.error(path, ' no module detected');
}
else if (result.results.length === 1) {
if (result.results[0].unusedPaths.length > 0) {
console.error('ERROR: ' + path + ' has unused: ' + result.results[0].unusedPaths.join(', '));
}
}
else {
throw new Error(path + ': > 1 modules detected');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment