|
1 | 1 | import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
|
| 2 | +const compileBooleanMatcher = require( |
| 3 | + normalizeWebpackPath('webpack/lib/util/compileBooleanMatcher'), |
| 4 | +) as typeof import('webpack/lib/util/compileBooleanMatcher'); |
| 5 | +const { getUndoPath } = require( |
| 6 | + normalizeWebpackPath('webpack/lib/util/identifier'), |
| 7 | +) as typeof import('webpack/lib/util/identifier'); |
2 | 8 | // inspired by react-refresh-webpack-plugin
|
3 | 9 | import getFederationGlobal from './getFederationGlobal';
|
4 | 10 | import { NormalizedRuntimeInitOptionsWithOutShared } from '../../../types/runtime';
|
@@ -27,10 +33,50 @@ class FederationRuntimeModule extends RuntimeModule {
|
27 | 33 | * @returns {string | null} runtime code
|
28 | 34 | */
|
29 | 35 | override generate() {
|
| 36 | + let matcher: string | boolean = false; |
| 37 | + let rootOutputDir: string | undefined; |
| 38 | + if (this.compilation && this.chunk) { |
| 39 | + const jsModulePlugin = |
| 40 | + this.compilation.compiler.webpack?.javascript |
| 41 | + ?.JavascriptModulesPlugin || |
| 42 | + require( |
| 43 | + normalizeWebpackPath( |
| 44 | + 'webpack/lib/javascript/JavascriptModulesPlugin', |
| 45 | + ), |
| 46 | + ); |
| 47 | + const { chunkHasJs } = jsModulePlugin; |
| 48 | + if (this.runtimeRequirements.has(RuntimeGlobals.ensureChunkHandlers)) { |
| 49 | + const conditionMap = this.compilation.chunkGraph.getChunkConditionMap( |
| 50 | + this.chunk, |
| 51 | + chunkHasJs, |
| 52 | + ); |
| 53 | + const hasJsMatcher = compileBooleanMatcher(conditionMap); |
| 54 | + if (typeof hasJsMatcher === 'boolean') { |
| 55 | + matcher = hasJsMatcher; |
| 56 | + } else { |
| 57 | + matcher = hasJsMatcher('chunkId'); |
| 58 | + } |
| 59 | + const outputName = this.compilation.getPath( |
| 60 | + jsModulePlugin.getChunkFilenameTemplate( |
| 61 | + this.chunk, |
| 62 | + this.compilation.outputOptions, |
| 63 | + ), |
| 64 | + { chunk: this.chunk, contentHashType: 'javascript' }, |
| 65 | + ); |
| 66 | + rootOutputDir = getUndoPath( |
| 67 | + outputName, |
| 68 | + this.compilation.outputOptions.path || '', |
| 69 | + false, |
| 70 | + ); |
| 71 | + } |
| 72 | + } |
| 73 | + |
30 | 74 | return Template.asString([
|
31 | 75 | getFederationGlobal(
|
32 | 76 | Template,
|
33 | 77 | RuntimeGlobals,
|
| 78 | + matcher, |
| 79 | + rootOutputDir, |
34 | 80 | this.initOptionsWithoutShared,
|
35 | 81 | ),
|
36 | 82 | ]);
|
|
0 commit comments