@@ -13,6 +13,7 @@ import { logger } from '../utils/logger';
1313import { normalize } from '../utils/normalize' ;
1414import crypto from 'crypto' ;
1515import { DEFAULT_EXTERNAL_LIST } from './default-external-list' ;
16+ import { BuildResult } from './build-adapter' ;
1617
1718export async function bundleShared (
1819 sharedBundles : Record < string , NormalizedSharedConfig > ,
@@ -52,18 +53,7 @@ export async function bundleShared(
5253
5354 const allEntryPoints = packageInfos . map ( ( pi ) => {
5455 const encName = pi . packageName . replace ( / [ ^ A - Z a - z 0 - 9 ] / g, '_' ) ;
55- // const encVersion = pi.version.replace(/[^A-Za-z0-9]/g, '_');
56-
57- // const outName = fedOptions.dev
58- // ? `${encName}-${encVersion}-dev.js`
59- // : `${encName}-${encVersion}.js`;
60-
61- const hash = calcHash ( pi , configState ) ;
62-
63- const outName = fedOptions . dev
64- ? `${ encName } .${ hash } -dev.js`
65- : `${ encName } .${ hash } .js` ;
66-
56+ const outName = createOutName ( pi , configState , fedOptions , encName ) ;
6757 return { fileName : pi . entryPoint , outName } ;
6858 } ) ;
6959
@@ -90,8 +80,10 @@ export async function bundleShared(
9080 const additionalExternals =
9181 platform === 'browser' ? DEFAULT_EXTERNAL_LIST : [ ] ;
9282
83+ let bundleResult : BuildResult [ ] | null = null ;
84+
9385 try {
94- await bundle ( {
86+ bundleResult = await bundle ( {
9587 entryPoints,
9688 tsConfigPath : fedOptions . tsConfig ,
9789 external : [ ...additionalExternals , ...externals ] ,
@@ -103,13 +95,9 @@ export async function bundleShared(
10395 platform,
10496 } ) ;
10597
106- for ( const fileName of exptedResults ) {
107- const outFileName = path . basename ( fileName ) ;
108- const cachedFile = path . join ( cachePath , outFileName ) ;
98+ const cachedFiles = bundleResult . map ( br => path . basename ( br . fileName ) ) ;
99+ copyCacheToOutput ( cachedFiles , cachePath , fullOutputPath ) ;
109100
110- copyFileIfExists ( cachedFile , fileName ) ;
111- copySrcMapIfExists ( cachedFile , fileName ) ;
112- }
113101 } catch ( e ) {
114102 logger . error ( 'Error bundling shared npm package ' ) ;
115103 if ( e instanceof Error ) {
@@ -142,8 +130,73 @@ export async function bundleShared(
142130 throw e ;
143131 }
144132
133+ const resultCacheFile = createCacheFileName (
134+ configState ,
135+ sharedBundles ,
136+ fedOptions ,
137+ cachePath ,
138+ platform
139+ ) ;
140+
141+ if ( fs . existsSync ( resultCacheFile ) ) {
142+ const cachedResult : SharedInfo [ ] = JSON . parse ( fs . readFileSync ( resultCacheFile , 'utf-8' ) ) ;
143+ const cachedFiles = cachedResult . map ( cr => cr . outFileName ) ;
144+ copyCacheToOutput ( cachedFiles , cachePath , fullOutputPath )
145+ return cachedResult ;
146+ }
147+
145148 const outFileNames = [ ...exptedResults ] ;
146149
150+ const result = buildResult (
151+ packageInfos ,
152+ sharedBundles ,
153+ outFileNames ,
154+ fedOptions ) ;
155+
156+ const chunks = bundleResult . filter (
157+ ( br ) => ! result . find ( ( r ) =>
158+ r . outFileName === path . basename ( br . fileName ) )
159+ ) ;
160+
161+ addChunksToResult ( chunks , result , fedOptions . dev ) ;
162+
163+ fs . writeFileSync (
164+ resultCacheFile ,
165+ JSON . stringify ( result , undefined , 2 ) ,
166+ 'utf-8'
167+ ) ;
168+
169+ return result ;
170+ }
171+
172+ function copyCacheToOutput ( cachedFiles : string [ ] , cachePath : string , fullOutputPath : string ) {
173+ for ( const fileName of cachedFiles ) {
174+ const cachedFile = path . join ( cachePath , fileName ) ;
175+ const distFileName = path . join ( fullOutputPath , fileName ) ;
176+ copyFileIfExists ( cachedFile , distFileName ) ;
177+ copySrcMapIfExists ( cachedFile , distFileName ) ;
178+ }
179+ }
180+
181+ function createOutName ( pi : PackageInfo , configState : string , fedOptions : FederationOptions , encName : string ) {
182+ const hashBase = pi . version + '_' + pi . entryPoint + '_' + configState ;
183+ const hash = calcHash ( hashBase ) ;
184+
185+ const outName = fedOptions . dev
186+ ? `${ encName } .${ hash } -dev.js`
187+ : `${ encName } .${ hash } .js` ;
188+ return outName ;
189+ }
190+
191+ function createCacheFileName ( configState : string , sharedBundles : Record < string , NormalizedSharedConfig > , fedOptions : FederationOptions , cachePath : string , platform : string ) {
192+ const resultCacheState = configState + JSON . stringify ( sharedBundles ) ;
193+ const resultHash = calcHash ( resultCacheState ) ;
194+ const dev = fedOptions . dev ? '-dev' : '' ;
195+ const resultCacheFile = path . join ( cachePath , 'result-' + resultHash + '-' + platform + dev + '.json' ) ;
196+ return resultCacheFile ;
197+ }
198+
199+ function buildResult ( packageInfos : PackageInfo [ ] , sharedBundles : Record < string , NormalizedSharedConfig > , outFileNames : string [ ] , fedOptions : FederationOptions ) {
147200 return packageInfos . map ( ( pi ) => {
148201 const shared = sharedBundles [ pi . packageName ] ;
149202 return {
@@ -156,14 +209,32 @@ export async function bundleShared(
156209 dev : ! fedOptions . dev
157210 ? undefined
158211 : {
159- entryPoint : normalize ( pi . entryPoint ) ,
160- } ,
212+ entryPoint : normalize ( pi . entryPoint ) ,
213+ } ,
161214 } as SharedInfo ;
162215 } ) ;
163216}
164217
165- function calcHash ( pi : PackageInfo , configState : string ) {
166- const hashBase = pi . version + '_' + pi . entryPoint + '_' + configState ;
218+ function addChunksToResult ( chunks : BuildResult [ ] , result : SharedInfo [ ] , dev ?: boolean ) {
219+ for ( const item of chunks ) {
220+ const fileName = path . basename ( item . fileName ) ;
221+ result . push ( {
222+ singleton : false ,
223+ strictVersion : false ,
224+ requiredVersion : '0.0.0' ,
225+ version : '0.0.0' ,
226+ packageName : fileName ,
227+ outFileName : fileName ,
228+ dev : dev
229+ ? undefined
230+ : {
231+ entryPoint : normalize ( fileName ) ,
232+ } ,
233+ } ) ;
234+ }
235+ }
236+
237+ function calcHash ( hashBase : string ) {
167238 const hash = crypto
168239 . createHash ( 'sha256' )
169240 . update ( hashBase )
@@ -175,6 +246,7 @@ function calcHash(pi: PackageInfo, configState: string) {
175246 return hash ;
176247}
177248
249+
178250function copyFileIfExists ( cachedFile : string , fullOutputPath : string ) {
179251 fs . mkdirSync ( path . dirname ( fullOutputPath ) , { recursive : true } ) ;
180252
0 commit comments