UNPKG

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