1 | # CommonJS Tree Shaker
|
2 | [![NPM version](https://badge.fury.io/js/common-shake.svg)](http://badge.fury.io/js/common-shake)
|
3 | [![Build Status](https://secure.travis-ci.org/indutny/common-shake.svg)](http://travis-ci.org/indutny/common-shake)
|
4 |
|
5 | See [webpack-common-shake][0] for [webpack][1] plugin.
|
6 |
|
7 | ## Usage
|
8 |
|
9 | ```js
|
10 | const acorn = require('acorn');
|
11 | const Analyzer = require('common-shake').Analyzer;
|
12 |
|
13 | const a = new Analyzer();
|
14 |
|
15 | a.run(acorn.parse(`
|
16 | 'use strict';
|
17 | const lib = require('./a.js');
|
18 | exports.a = lib.a;
|
19 | `, { locations: true }), 'index.js');
|
20 |
|
21 | a.run(acorn.parse(`
|
22 | 'use strict';
|
23 | exports.a = 42;
|
24 | `, { locations: true }), 'a.js');
|
25 |
|
26 | a.resolve('index.js', './a.js', 'a.js');
|
27 | console.log(a.isSuccess(), a.bailouts);
|
28 | // true false
|
29 |
|
30 | console.log(a.getModule('index.js').getInfo());
|
31 | // { bailouts: false, declarations: [ 'a' ], uses: [] }
|
32 |
|
33 | console.log(a.getModule('a.js').getInfo());
|
34 | // { bailouts: false, declarations: [ 'a' ], uses: [ 'a' ] }
|
35 |
|
36 | const module = a.getModule('a.js');
|
37 | a.getDeclarations().forEach((decl) => {
|
38 | console.log(module.isUsed(decl.name) ? 'used' : 'not used');
|
39 | console.log(decl.name, decl.ast);
|
40 | });
|
41 | ```
|
42 |
|
43 | ## Graphviz
|
44 |
|
45 | For debugging and inspection purposes a graph in [dot][2] format may be
|
46 | generated from the modules hierarchy using following API:
|
47 |
|
48 | ```js
|
49 | const Graph = require('common-shake').Graph;
|
50 | const graph = new Graph('/path/to/working/dir');
|
51 |
|
52 | console.log(graph.generate(analyzer.getModules()));
|
53 | ```
|
54 |
|
55 | ## LICENSE
|
56 |
|
57 | This software is licensed under the MIT License.
|
58 |
|
59 | Copyright Fedor Indutny, 2017.
|
60 |
|
61 | Permission is hereby granted, free of charge, to any person obtaining a
|
62 | copy of this software and associated documentation files (the
|
63 | "Software"), to deal in the Software without restriction, including
|
64 | without limitation the rights to use, copy, modify, merge, publish,
|
65 | distribute, sublicense, and/or sell copies of the Software, and to permit
|
66 | persons to whom the Software is furnished to do so, subject to the
|
67 | following conditions:
|
68 |
|
69 | The above copyright notice and this permission notice shall be included
|
70 | in all copies or substantial portions of the Software.
|
71 |
|
72 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
73 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
74 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
75 | NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
76 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
77 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
78 | USE OR OTHER DEALINGS IN THE SOFTWARE.
|
79 |
|
80 | [0]: https://github.com/indutny/webpack-common-shake
|
81 | [1]: https://webpack.github.io/
|
82 | [2]: http://www.graphviz.org/content/dot-language
|