-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
74 lines (67 loc) · 1.84 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const flow = require('dependency-flow');
const path = require('path');
const cwd = process.cwd();
const normalizePath = (s) => {
let p = s.split('!');
p = p[p.length - 1];
return p.replace(cwd, '');
};
module.exports = function plugin(build = {}, serve = false) {
const buildOptions = build === true ? {} : build;
const serveOptions = serve === true ? {} : serve;
let s;
return {
name: 'dependency-flow',
generateBundle(opts, bundle) {
const links = [];
const modules = {};
let name;
let dir;
Object.keys(bundle).forEach((key) => {
if (opts.dir) {
dir = opts.dir;
} else if (opts.file) {
const p = path.parse(opts.file);
dir = p.dir;
name = `${p.name}-dependency-flow`;
}
Object.keys(bundle[key].modules).forEach((mkey) => {
if (mkey[0] === '\u0000') {
return;
}
const m = bundle[key].modules[mkey];
// keep the module to make it possible to track import paths
// if (!m.renderedLength && !m.renderedExports.length) {
// return;
// }
const mInfo = this.getModuleInfo(mkey);
modules[normalizePath(mInfo.id)] = {
size: m.renderedLength,
};
mInfo.importedIds.forEach((im) => {
if (im[0] !== '\u0000') {
links.push([
normalizePath(mInfo.id),
normalizePath(im),
]);
}
});
});
});
const data = { modules, links };
if (!s && serveOptions && process.env.ROLLUP_WATCH) {
s = flow.serve(serveOptions);
}
if (s) {
s.update(data);
}
if (buildOptions) {
flow.build(data, {
name,
dir,
...buildOptions,
});
}
},
};
};