Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.