@@ -13,6 +13,7 @@ import { logger } from '../utils/logger';
13
13
import { normalize } from '../utils/normalize' ;
14
14
import crypto from 'crypto' ;
15
15
import { DEFAULT_EXTERNAL_LIST } from './default-external-list' ;
16
+ import { BuildResult } from './build-adapter' ;
16
17
17
18
export async function bundleShared (
18
19
sharedBundles : Record < string , NormalizedSharedConfig > ,
@@ -52,18 +53,7 @@ export async function bundleShared(
52
53
53
54
const allEntryPoints = packageInfos . map ( ( pi ) => {
54
55
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 ) ;
67
57
return { fileName : pi . entryPoint , outName } ;
68
58
} ) ;
69
59
@@ -90,8 +80,10 @@ export async function bundleShared(
90
80
const additionalExternals =
91
81
platform === 'browser' ? DEFAULT_EXTERNAL_LIST : [ ] ;
92
82
83
+ let bundleResult : BuildResult [ ] | null = null ;
84
+
93
85
try {
94
- await bundle ( {
86
+ bundleResult = await bundle ( {
95
87
entryPoints,
96
88
tsConfigPath : fedOptions . tsConfig ,
97
89
external : [ ...additionalExternals , ...externals ] ,
@@ -103,13 +95,9 @@ export async function bundleShared(
103
95
platform,
104
96
} ) ;
105
97
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 ) ;
109
100
110
- copyFileIfExists ( cachedFile , fileName ) ;
111
- copySrcMapIfExists ( cachedFile , fileName ) ;
112
- }
113
101
} catch ( e ) {
114
102
logger . error ( 'Error bundling shared npm package ' ) ;
115
103
if ( e instanceof Error ) {
@@ -142,8 +130,73 @@ export async function bundleShared(
142
130
throw e ;
143
131
}
144
132
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
+
145
148
const outFileNames = [ ...exptedResults ] ;
146
149
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 ) {
147
200
return packageInfos . map ( ( pi ) => {
148
201
const shared = sharedBundles [ pi . packageName ] ;
149
202
return {
@@ -156,14 +209,32 @@ export async function bundleShared(
156
209
dev : ! fedOptions . dev
157
210
? undefined
158
211
: {
159
- entryPoint : normalize ( pi . entryPoint ) ,
160
- } ,
212
+ entryPoint : normalize ( pi . entryPoint ) ,
213
+ } ,
161
214
} as SharedInfo ;
162
215
} ) ;
163
216
}
164
217
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 ) {
167
238
const hash = crypto
168
239
. createHash ( 'sha256' )
169
240
. update ( hashBase )
@@ -175,6 +246,7 @@ function calcHash(pi: PackageInfo, configState: string) {
175
246
return hash ;
176
247
}
177
248
249
+
178
250
function copyFileIfExists ( cachedFile : string , fullOutputPath : string ) {
179
251
fs . mkdirSync ( path . dirname ( fullOutputPath ) , { recursive : true } ) ;
180
252
0 commit comments