@@ -28,13 +28,9 @@ function packageSearch(dir, pkg) {
28
28
}
29
29
30
30
// Determine if it's a NodeJS output filesystem or if it's a foreign/virtual one.
31
+ // The internal webpack5 implementation of outputFileSystem is graceful-fs.
31
32
function isNodeOutputFS ( compiler ) {
32
- return (
33
- compiler . outputFileSystem &&
34
- compiler . outputFileSystem . constructor &&
35
- compiler . outputFileSystem . constructor . name &&
36
- compiler . outputFileSystem . constructor . name === 'NodeOutputFileSystem'
37
- ) ;
33
+ return compiler . outputFileSystem && JSON . stringify ( compiler . outputFileSystem ) === JSON . stringify ( fs ) ;
38
34
}
39
35
40
36
// Normalize a filepath to be relative to the webpack context, using forward-slashes, and
@@ -57,7 +53,7 @@ function bundleConst(name) {
57
53
) ;
58
54
}
59
55
60
- function resolveBundle ( dir , context , symlinks , relative ) {
56
+ function resolveBundle ( { dir, context, symlinks, relative, publicPath } ) {
61
57
const bundle = { resolved : dir , path : dir , emit : true } ;
62
58
if ( path . isAbsolute ( bundle . path ) ) {
63
59
bundle . emit = false ;
@@ -73,7 +69,7 @@ function resolveBundle(dir, context, symlinks, relative) {
73
69
if ( relative ) {
74
70
bundle . resolved = JSON . stringify ( transformPath ( context , bundle . path ) ) ;
75
71
} else {
76
- bundle . resolved = '__webpack_require__.p + ' + JSON . stringify ( transformPath ( context , bundle . path ) ) ;
72
+ bundle . resolved = publicPath + JSON . stringify ( transformPath ( context , bundle . path ) ) ;
77
73
}
78
74
}
79
75
return bundle ;
@@ -180,6 +176,26 @@ function emitAsset(compilation, name, data) {
180
176
} ;
181
177
}
182
178
179
+ const iLibPluginHooksMap = new WeakMap ( ) ;
180
+
181
+ function getILibPluginHooks ( compilation ) {
182
+ let hooks = iLibPluginHooksMap . get ( compilation ) ;
183
+
184
+ // Setup the hooks only once
185
+ if ( hooks === undefined ) {
186
+ hooks = createILibPluginHooks ( ) ;
187
+ iLibPluginHooksMap . set ( compilation , hooks ) ;
188
+ }
189
+
190
+ return hooks ;
191
+ }
192
+
193
+ function createILibPluginHooks ( ) {
194
+ return {
195
+ ilibManifestList : new SyncWaterfallHook ( [ 'manifests' ] )
196
+ } ;
197
+ }
198
+
183
199
class ILibPlugin {
184
200
constructor ( options = { } ) {
185
201
this . options = options ;
@@ -239,24 +255,38 @@ class ILibPlugin {
239
255
}
240
256
241
257
// Resolve an accurate basepath for iLib.
242
- const ilib = resolveBundle ( opts . ilib , opts . context , opts . symlinks ) ;
243
- const resources = resolveBundle (
244
- opts . resources || 'resources' ,
245
- opts . context ,
246
- opts . symlinks ,
247
- Boolean ( opts . relativeResources )
248
- ) ;
258
+ const ilib = resolveBundle ( {
259
+ dir : opts . ilib ,
260
+ context : opts . context ,
261
+ symlinks : opts . symlinks ,
262
+ publicPath : opts . publicPath
263
+ } ) ;
264
+ const resources = resolveBundle ( {
265
+ dir : opts . resources || 'resources' ,
266
+ context : opts . context ,
267
+ symlinks : opts . symlinks ,
268
+ relative : Boolean ( opts . relativeResources ) ,
269
+ publicPath : opts . publicPath
270
+ } ) ;
249
271
const definedConstants = {
250
272
ILIB_BASE_PATH : ilib . resolved ,
251
273
ILIB_RESOURCES_PATH : resources . resolved ,
252
274
ILIB_CACHE_ID : '__webpack_require__.ilib_cache_id' ,
253
275
// when `emit` is false and `ilib` is not absolute, can delare no assets
254
276
ILIB_NO_ASSETS : JSON . stringify ( ! opts . emit && ! path . isAbsolute ( opts . ilib ) )
255
277
} ;
278
+ if ( opts . ilibAdditionalResourcesPath ) {
279
+ definedConstants . ILIB_ADDITIONAL_RESOURCES_PATH = '"' + opts . ilibAdditionalResourcesPath + '"' ;
280
+ }
256
281
definedConstants [ bundleConst ( app . name ) ] = definedConstants . ILIB_RESOURCES_PATH ;
257
282
for ( const name in opts . bundles ) {
258
283
if ( opts . bundles [ name ] ) {
259
- const bundle = resolveBundle ( opts . bundles [ name ] , opts . context , opts . symlinks ) ;
284
+ const bundle = resolveBundle ( {
285
+ dir : opts . bundles [ name ] ,
286
+ context : opts . context ,
287
+ symlinks : opts . symlinks ,
288
+ publicPath : opts . publicPath
289
+ } ) ;
260
290
const bundleManifest = path . join ( bundle . path , 'ilibmanifest.json' ) ;
261
291
definedConstants [ bundleConst ( name ) ] = bundle . resolved ;
262
292
if ( opts . emit && bundle . emit && fs . existsSync ( bundleManifest ) ) {
@@ -273,16 +303,13 @@ class ILibPlugin {
273
303
new ContextReplacementPlugin ( / i l i b / , / ^ $ / ) . apply ( compiler ) ;
274
304
275
305
compiler . hooks . compilation . tap ( 'ILibPlugin' , compilation => {
276
- // Define compilation hooks
277
- compilation . hooks . ilibManifestList = new SyncWaterfallHook ( [ 'manifests' ] ) ;
278
-
279
306
// Add a unique ID value to the webpack require-function, so that the value is correctly updated,
280
307
// even when hot-reloading and serving.
281
308
const main = compilation . mainTemplate ;
282
309
main . hooks . requireExtensions . tap ( 'ILibPlugin' , source => {
283
310
const buf = [ source ] ;
284
311
buf . push ( '' ) ;
285
- buf . push ( main . requireFn + ' .ilib_cache_id = ' + JSON . stringify ( '' + new Date ( ) . getTime ( ) ) + ';' ) ;
312
+ buf . push ( '__webpack_require__ .ilib_cache_id = ' + JSON . stringify ( '' + new Date ( ) . getTime ( ) ) + ';' ) ;
286
313
return Template . asString ( buf ) ;
287
314
} ) ;
288
315
} ) ;
@@ -332,11 +359,15 @@ class ILibPlugin {
332
359
)
333
360
) ;
334
361
}
335
- manifests = compilation . hooks . ilibManifestList . call ( manifests ) ;
362
+ manifests = getILibPluginHooks ( compilation ) . ilibManifestList . call ( manifests ) ;
336
363
handleBundles ( compilation , manifests , opts , callback ) ;
337
364
} ) ;
338
365
}
339
366
}
340
367
}
341
368
369
+ // A static helper to get the hooks for this plugin
370
+ // Usage: ILibPlugin.getHooks(compilation).HOOK_NAME.tapAsync('YourPluginName', () => { ... });
371
+ ILibPlugin . getHooks = getILibPluginHooks ;
372
+
342
373
module . exports = ILibPlugin ;
0 commit comments