1 | <div align="center">
|
2 | <a href="https://github.com/webpack/webpack">
|
3 | <img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg">
|
4 | </a>
|
5 | </div>
|
6 |
|
7 | [![npm][npm]][npm-url]
|
8 | [![cover][cover]][cover-url]
|
9 | [![chat][chat]][chat-url]
|
10 | [![size][size]][size-url]
|
11 |
|
12 | # ana-loader
|
13 |
|
14 | A webpack loader for analyzing dependencies. Support TypeScript, JSX, Vue, AMD, CJS, ESM, CSS, Sass, Scss, Less and Stylus.
|
15 |
|
16 | ## Getting Started
|
17 |
|
18 | To begin, you'll need to install `ana-loader`:
|
19 |
|
20 | ```console
|
21 | $ npm install ana-loader --save-dev
|
22 | ```
|
23 |
|
24 | Then add the loader to your `webpack` config. For example:
|
25 |
|
26 | **compile.js**
|
27 |
|
28 | ```js
|
29 | const path = require('path');
|
30 |
|
31 | const webpack = require('webpack');
|
32 |
|
33 | const config = {
|
34 | mode: 'none',
|
35 | devtool: false,
|
36 | entry: path.resolve(__dirname, './index.js'),
|
37 | output: {
|
38 | path: path.resolve(__dirname, './dist'),
|
39 | },
|
40 | module: {
|
41 | rules: [
|
42 | {
|
43 | loader: require.resolve('ana-loader'),
|
44 | },
|
45 | ],
|
46 | },
|
47 | };
|
48 |
|
49 | const compiler = webpack(config);
|
50 |
|
51 | compiler.run((error, stats) => {
|
52 | const { modules } = stats.toJson();
|
53 | const result = modules.map((m) => {
|
54 | return {
|
55 | id: m.name,
|
56 | reasons: m.reasons.map((r) => r.moduleName),
|
57 | };
|
58 | });
|
59 |
|
60 | console.log(result);
|
61 | /*
|
62 | [
|
63 | { id: './index.js', reasons: [ null ] },
|
64 | { id: './test.js', reasons: [ './index.js' ] },
|
65 | { id: './test1.css', reasons: [ './index.js' ] },
|
66 | { id: './test2.css', reasons: [ './test1.css' ] },
|
67 | { id: './test.png', reasons: [ './test1.css' ] },
|
68 | { id: 'webpack/runtime/compat get default export', reasons: [] },
|
69 | { id: 'webpack/runtime/define property getters', reasons: [] },
|
70 | { id: 'webpack/runtime/hasOwnProperty shorthand', reasons: [] },
|
71 | { id: 'webpack/runtime/make namespace object', reasons: [] }
|
72 | ]
|
73 | */
|
74 | });
|
75 | ```
|
76 |
|
77 | And run `node compile.js` to get dependencies info.
|
78 |
|
79 | ## Options
|
80 |
|
81 | ### `excludes`
|
82 |
|
83 | Type: `array`
|
84 | Default: `[/node_modules/]`
|
85 |
|
86 | Exclude finding dependencies in these conditions.
|
87 |
|
88 | **webpack.config.js**
|
89 |
|
90 | ```js
|
91 | module.exports = {
|
92 | module: {
|
93 | rules: [
|
94 | {
|
95 | loader: `ana-loader`,
|
96 | options: {
|
97 | excludes: [/node_modules/, /test/],
|
98 | },
|
99 | },
|
100 | ],
|
101 | },
|
102 | };
|
103 | ```
|
104 |
|
105 | ## [Examples](./example)
|
106 |
|
107 | Get dependencies info from stats.
|
108 |
|
109 | **index.js**
|
110 |
|
111 | ```js
|
112 | import './test.js';
|
113 | import './test1.css';
|
114 |
|
115 | // ...
|
116 | ```
|
117 |
|
118 | **test.js**
|
119 |
|
120 | ```js
|
121 | // ...
|
122 | ```
|
123 |
|
124 | **test1.css**
|
125 |
|
126 | ```css
|
127 | @import './test2.css';
|
128 |
|
129 | .test {
|
130 | background: url('./test.png');
|
131 | }
|
132 | /* ... */
|
133 | ```
|
134 |
|
135 | **test2.css**
|
136 |
|
137 | ```css
|
138 | /* ... */
|
139 | ```
|
140 |
|
141 | **test.png**
|
142 |
|
143 | Image.
|
144 |
|
145 | **compile.js**
|
146 |
|
147 | ```js
|
148 | const path = require('path');
|
149 |
|
150 | const webpack = require('webpack');
|
151 |
|
152 | const config = {
|
153 | mode: 'none',
|
154 | devtool: false,
|
155 | entry: path.resolve(__dirname, './index.js'),
|
156 | output: {
|
157 | path: path.resolve(__dirname, './dist'),
|
158 | },
|
159 | module: {
|
160 | rules: [
|
161 | {
|
162 | loader: require.resolve('ana-loader'),
|
163 | },
|
164 | ],
|
165 | },
|
166 | };
|
167 |
|
168 | const compiler = webpack(config);
|
169 |
|
170 | compiler.run((error, stats) => {
|
171 | const { modules } = stats.toJson();
|
172 | const result = modules.map((m) => {
|
173 | return {
|
174 | id: m.name,
|
175 | reasons: m.reasons.map((r) => r.moduleName),
|
176 | };
|
177 | });
|
178 |
|
179 | console.log(result);
|
180 | /*
|
181 | [
|
182 | { id: './index.js', reasons: [ null ] },
|
183 | { id: './test.js', reasons: [ './index.js' ] },
|
184 | { id: './test1.css', reasons: [ './index.js' ] },
|
185 | { id: './test2.css', reasons: [ './test1.css' ] },
|
186 | { id: './test.png', reasons: [ './test1.css' ] },
|
187 | { id: 'webpack/runtime/compat get default export', reasons: [] },
|
188 | { id: 'webpack/runtime/define property getters', reasons: [] },
|
189 | { id: 'webpack/runtime/hasOwnProperty shorthand', reasons: [] },
|
190 | { id: 'webpack/runtime/make namespace object', reasons: [] }
|
191 | ]
|
192 | */
|
193 | });
|
194 | ```
|
195 |
|
196 | ## License
|
197 |
|
198 | [MIT](./LICENSE)
|
199 |
|
200 | [npm]: https://img.shields.io/npm/v/ana-loader.svg
|
201 | [npm-url]: https://npmjs.com/package/ana-loader
|
202 | [node]: https://img.shields.io/node/v/ana-loader.svg
|
203 | [node-url]: https://nodejs.org
|
204 | [deps]: https://david-dm.org/zjffun/ana-loader.svg
|
205 | [deps-url]: https://david-dm.org/zjffun/ana-loader
|
206 | [cover]: https://codecov.io/gh/zjffun/ana-loader/branch/master/graph/badge.svg
|
207 | [cover-url]: https://codecov.io/gh/zjffun/ana-loader
|
208 | [chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
|
209 | [chat-url]: https://gitter.im/webpack/webpack
|
210 | [size]: https://packagephobia.now.sh/badge?p=ana-loader
|
211 | [size-url]: https://packagephobia.now.sh/result?p=ana-loader
|