UNPKG

4.41 kBMarkdownView Raw
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
14A webpack loader for analyzing dependencies. Support TypeScript, JSX, Vue, AMD, CJS, ESM, CSS, Sass, Scss, Less and Stylus.
15
16## Getting Started
17
18To begin, you'll need to install `ana-loader`:
19
20```console
21$ npm install ana-loader --save-dev
22```
23
24Then add the loader to your `webpack` config. For example:
25
26**compile.js**
27
28```js
29const path = require('path');
30
31const webpack = require('webpack');
32
33const 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
49const compiler = webpack(config);
50
51compiler.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
77And run `node compile.js` to get dependencies info.
78
79## Options
80
81### `excludes`
82
83Type: `array`
84Default: `[/node_modules/]`
85
86Exclude finding dependencies in these conditions.
87
88**webpack.config.js**
89
90```js
91module.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
107Get dependencies info from stats.
108
109**index.js**
110
111```js
112import './test.js';
113import './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
143Image.
144
145**compile.js**
146
147```js
148const path = require('path');
149
150const webpack = require('webpack');
151
152const 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
168const compiler = webpack(config);
169
170compiler.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