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
1114type 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+
1935export 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 ] ;
0 commit comments