Skip to content

Commit 7e256bb

Browse files
author
Denis Bardadym
committed
Instead of unique id use hash based on data
1 parent 9ed44b4 commit 7e256bb

File tree

4 files changed

+45
-25
lines changed

4 files changed

+45
-25
lines changed

plugin/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { version } from "./version";
1010
import { createGzipSizeGetter, createBrotliSizeGetter, SizeGetter } from "./compress";
1111

1212
import { TemplateType } from "./template-types";
13-
import { ModuleMapper } from "./module-mapper";
13+
import { ModuleMapper, replaceHashPlaceholders } from "./module-mapper";
1414
import { addLinks, buildTree, mergeTrees } from "./data";
1515
import { getSourcemapModules } from "./sourcemap";
1616
import { renderTemplate } from "./render-template";
@@ -281,9 +281,11 @@ export const visualizer = (
281281
},
282282
};
283283

284+
const stringData = replaceHashPlaceholders(data);
285+
284286
const fileContent: string = await renderTemplate(template, {
285287
title,
286-
data,
288+
data: stringData,
287289
});
288290

289291
if (opts.emitFile) {

plugin/module-mapper.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { ModuleImport, ModuleMeta, ModulePart, ModuleLengths, ModuleUID } from "../shared/types";
2-
import { getUid } from "./uid";
3-
4-
const nanoid = getUid("1234567890abcdef", 4);
5-
6-
const UNIQUE_PREFIX = nanoid();
7-
let COUNTER = 0;
8-
9-
const uniqueId = (): ModuleUID => `${UNIQUE_PREFIX}-${COUNTER++}`;
1+
import {
2+
ModuleImport,
3+
ModuleMeta,
4+
ModulePart,
5+
ModuleLengths,
6+
ModuleUID,
7+
VisualizerData,
8+
} from "../shared/types";
9+
import * as crypto from "crypto";
10+
11+
const HASH_PLACEHOLDER = "!{ROLLUP_VISUALIZER_HASH_PLACEHOLDER}";
12+
const HASH_PLACEHOLDER_REGEXP = new RegExp(`"${HASH_PLACEHOLDER}-(\\d+)"`, "g");
1013

1114
type ModuleIdStorage = {
1215
uid: ModuleUID;
@@ -16,9 +19,23 @@ type ModuleIdStorage = {
1619
};
1720
};
1821

22+
export const getDataHash = (json: string) => {
23+
const hash = crypto.createHash("sha1").update(json).digest("hex");
24+
const hashSub = hash.substring(0, 8);
25+
return hashSub;
26+
};
27+
28+
export const replaceHashPlaceholders = (data: VisualizerData) => {
29+
const json = JSON.stringify(data);
30+
const hash = getDataHash(json);
31+
const jsonWithHash = json.replace(HASH_PLACEHOLDER_REGEXP, (_, num) => `"${hash}-${num}"`);
32+
return jsonWithHash;
33+
};
34+
1935
export class ModuleMapper {
2036
private nodeParts: Record<ModuleUID, ModulePart> = {};
2137
private nodeMetas: Record<string, ModuleIdStorage> = {};
38+
private counter: number = 0;
2239

2340
constructor(private projectRoot: string | RegExp) {}
2441

@@ -29,10 +46,14 @@ export class ModuleMapper {
2946
return moduleId.replace(this.projectRoot, "");
3047
}
3148

49+
uniqueId(): ModuleUID {
50+
return `${HASH_PLACEHOLDER}-${this.counter++}`;
51+
}
52+
3253
getModuleUid(moduleId: string): ModuleUID {
3354
if (!(moduleId in this.nodeMetas)) {
3455
this.nodeMetas[moduleId] = {
35-
uid: uniqueId(),
56+
uid: this.uniqueId(),
3657
meta: {
3758
id: this.trimProjectRootId(moduleId),
3859
moduleParts: {},
@@ -48,7 +69,7 @@ export class ModuleMapper {
4869
getBundleModuleUid(bundleId: string, moduleId: string): ModuleUID {
4970
if (!(moduleId in this.nodeMetas)) {
5071
this.nodeMetas[moduleId] = {
51-
uid: uniqueId(),
72+
uid: this.uniqueId(),
5273
meta: {
5374
id: this.trimProjectRootId(moduleId),
5475
moduleParts: {},
@@ -58,7 +79,7 @@ export class ModuleMapper {
5879
};
5980
}
6081
if (!(bundleId in this.nodeMetas[moduleId].meta.moduleParts)) {
61-
this.nodeMetas[moduleId].meta.moduleParts[bundleId] = uniqueId();
82+
this.nodeMetas[moduleId].meta.moduleParts[bundleId] = this.uniqueId();
6283
}
6384

6485
return this.nodeMetas[moduleId].meta.moduleParts[bundleId];

plugin/render-template.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ ${script}
5555
`;
5656

5757
export type RenderTemplateOptions = {
58-
data: VisualizerData;
58+
data: string;
5959
title: string;
6060
};
6161

@@ -67,12 +67,16 @@ const buildHtml =
6767
fs.readFile(path.join(__dirname, "..", "lib", `${template}.css`), "utf8"),
6868
]);
6969

70-
return buildHtmlTemplate(title, script, JSON.stringify(data), style);
70+
return buildHtmlTemplate(title, script, data, style);
7171
};
7272

73-
const outputRawData = (data: VisualizerData) => JSON.stringify(data, null, 2);
73+
const outputRawData = (strData: string) => {
74+
const data = JSON.parse(strData) as VisualizerData;
75+
return JSON.stringify(data, null, 2);
76+
};
7477

75-
const outputPlainTextList = (data: VisualizerData) => {
78+
const outputPlainTextList = (strData: string) => {
79+
const data = JSON.parse(strData) as VisualizerData;
7680
const bundles: Record<BundleId, [string, ModuleLengths][]> = {};
7781

7882
for (const meta of Object.values(data.nodeMetas)) {

plugin/uid.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)