common-shakeify
Advanced tools
Comparing version 0.1.1 to 0.2.0
31
index.js
@@ -0,1 +1,3 @@ | ||
'use strict' | ||
const relative = require('path').relative | ||
const Analyzer = require('common-shake').Analyzer | ||
@@ -6,6 +8,29 @@ const transformAst = require('transform-ast') | ||
module.exports = function commonShake (b, opts) { | ||
const basedir = b._options.basedir || process.cwd() | ||
const seen = {} | ||
opts = Object.assign({ | ||
onExportDelete () {}, | ||
onModuleBailout () {}, | ||
onGlobalBailout () {} | ||
verbose: false, | ||
onExportDelete (path, name) { | ||
if (opts.verbose || opts.v) { | ||
console.warn('common-shake: removed', `\`${name}\``, 'in', relative(basedir, path)) | ||
} | ||
}, | ||
onModuleBailout (resource, reasons) { | ||
if (opts.verbose || opts.v) { | ||
reasons.forEach((reason) => { | ||
if (seen[resource.resource + reason.reason]) return | ||
seen[resource.resource + reason.reason] = true | ||
const loc = reason.loc.start | ||
console.warn('common-shake: bailed out: ', reason.reason, 'in', `${relative(basedir, resource.resource)}:${loc.line}:${loc.column}`) | ||
}) | ||
} | ||
}, | ||
onGlobalBailout (reasons) { | ||
if (opts.verbose || opts.v) { | ||
reasons.forEach((reason) => { | ||
const loc = reason.loc.start | ||
console.warn('common-shake: GLOBAL BAILOUT:', reason.reason, 'in', `${relative(basedir, resource.resource)}:${loc.line}:${loc.column}`) | ||
}) | ||
} | ||
} | ||
}, opts) | ||
@@ -12,0 +37,0 @@ |
{ | ||
"name": "common-shakeify", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "browserify tree shaking plugin using @indutny common-shake", | ||
@@ -32,4 +32,5 @@ "main": "index.js", | ||
"concat-stream": "^1.6.0", | ||
"tape": "^4.8.0" | ||
"tape": "^4.8.0", | ||
"uglify-js": "^3.0.28" | ||
} | ||
} |
@@ -5,2 +5,5 @@ # common-shakeify | ||
Comments out unused exports from CommonJS modules. | ||
Use a minifier on the output to remove the exports entirely. | ||
## Install | ||
@@ -17,3 +20,5 @@ | ||
```bash | ||
browserify -p common-shakeify /my/app.js | ||
browserify -p common-shakeify /my/app.js > bundle.js | ||
# Minify | ||
uglify-js bundle.js --compress > bundle.min.js | ||
``` | ||
@@ -26,5 +31,12 @@ | ||
browserify({ entries: '/my/app.js' }) | ||
var b = browserify({ entries: '/my/app.js' }) | ||
.plugin(commonShake, { /* options */ }) | ||
.bundle() | ||
// Minify & save | ||
var concat = require('concat-stream') | ||
var uglify = require('uglify-js') | ||
b.pipe(concat(function (source) { | ||
fs.writeFileSync('bundle.min.js', uglify.minify(source.toString(), { compress: true }).code) | ||
})) | ||
``` | ||
@@ -34,4 +46,14 @@ | ||
With the Node API, some options can be passed: | ||
### `verbose`, `v` | ||
When true, print messages to stderr when exports are deleted, or the tree-shaker bails out on a module. | ||
Default false. | ||
The `verbose` flag only works when no custom handlers are passed, so if you're using eg. a custom `onExportDelete` you have to print these messages manually. | ||
```bash | ||
$ browserify -p [ common-shakeify -v ] app.js > bundle.js | ||
common-shake: removed `decode` in node_modules/vlq/dist/vlq.js:10:7 | ||
common-shake: bailed out: `module.exports` assignment in node_modules/process-nextick-args/index.js:20:3 | ||
``` | ||
### `onExportDelete(filename, exportName)` | ||
@@ -38,0 +60,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19563
40
367
74
4
2
25