-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (108 loc) · 3.62 KB
/
index.js
File metadata and controls
137 lines (108 loc) · 3.62 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"use strict";
const fs = require("fs");
const path = require("path");
const { promisify } = require("util");
const mkdirp = require("mkdirp");
const mkdir = promisify(mkdirp);
const writeFile = promisify(fs.writeFile);
const opn = require("open");
const TEMPLATE = require("./template-types");
const ModuleMapper = require("./module-mapper");
const buildStats = require("./build-stats");
const JSON_VERSION = require("./version");
const {
buildTree,
mergeTrees,
addLinks,
removeCommonPrefix
} = require("./data");
const getSourcemapModules = require("./sourcemap");
const warn = require("./warn");
const WARN_SOURCEMAP_DISABLED =
"rollup output configuration missing sourcemap = true. You should add output.sourcemap = true or disable sourcemap in this plugin";
const WARN_SOURCEMAP_MISSING = id => `${id} missing source map`;
module.exports = function(opts) {
opts = opts || {};
const json = !!opts.json;
const filename = opts.filename || (json ? "stats.json" : "stats.html");
const title = opts.title || "RollUp Visualizer";
const open = !!opts.open;
const openOptions = opts.openOptions || {};
const template = opts.template || "treemap";
if (!TEMPLATE.includes(template)) {
throw new Error(`Unknown template type ${template}. Known: ${TEMPLATE}`);
}
let extraStylePath = opts.extraStylePath;
if ("styleOverridePath" in opts) {
warn(
"`styleOverridePath` was renamed to `extraStylePath`, but will be removed in next major version"
);
extraStylePath = opts.styleOverridePath;
}
const chartParameters = opts.chartParameters || {};
return {
name: "visualizer",
async generateBundle(outputOptions, outputBundle) {
if (opts.sourcemap && !outputOptions.sourcemap) {
this.warn(WARN_SOURCEMAP_DISABLED);
}
const roots = [];
const mapper = new ModuleMapper();
const links = [];
// collect trees
for (const [id, bundle] of Object.entries(outputBundle)) {
if (bundle.isAsset) continue; //only chunks
let tree;
if (opts.sourcemap) {
if (!bundle.map) {
this.warn(WARN_SOURCEMAP_MISSING(id));
}
const modules = await getSourcemapModules(
id,
bundle,
outputOptions.dir || path.dirname(outputOptions.file)
);
tree = buildTree(id, Object.entries(modules), mapper);
} else {
tree = buildTree(id, Object.entries(bundle.modules), mapper);
}
roots.push(tree);
}
// after trees we process links (this is mostly for uids)
for (const bundle of Object.values(outputBundle)) {
if (bundle.isAsset || bundle.facadeModuleId == null) continue; //only chunks
addLinks(
bundle.facadeModuleId,
this.getModuleInfo.bind(this),
links,
mapper
);
}
const tree = mergeTrees(roots);
const { nodes, nodeIds } = mapper;
removeCommonPrefix(nodes, nodeIds);
for (const [id, uid] of Object.entries(nodeIds)) {
if (nodes[uid]) {
nodes[uid].id = id;
} else {
this.warn(`Could not find mapping for node ${id} ${uid}`);
}
}
const data = { version: JSON_VERSION, tree, nodes, links };
const fileContent = json
? JSON.stringify(data, null, 2)
: await buildStats(
title,
data,
template,
extraStylePath,
chartParameters
);
await mkdir(path.dirname(filename));
await writeFile(filename, fileContent);
if (open) {
return opn(filename, openOptions);
}
}
};
};