Created
November 3, 2016 13:30
-
-
Save joonas-fi/dbf839f09fdc737eca5f5531de4efa94 to your computer and use it in GitHub Desktop.
Finding unused AMD imports with amdextract without Grunt or Gulp etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/nodejs | |
// prerequisites: $ npm install -g [email protected] | |
// 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