-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathFederationModulesPlugin.ts
62 lines (52 loc) · 2.04 KB
/
FederationModulesPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import type { Compiler, Compilation as CompilationType } from 'webpack';
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
const Compilation = require(
normalizeWebpackPath('webpack/lib/Compilation'),
) as typeof import('webpack/lib/Compilation');
import { SyncHook } from 'tapable';
import ContainerEntryDependency from '../ContainerEntryDependency';
import FederationRuntimeDependency from './FederationRuntimeDependency';
/** @type {WeakMap<import("webpack").Compilation, CompilationHooks>} */
const compilationHooksMap = new WeakMap<CompilationType, CompilationHooks>();
const PLUGIN_NAME = 'FederationModulesPlugin';
/** @typedef {{ header: string[], beforeStartup: string[], startup: string[], afterStartup: string[], allowInlineStartup: boolean }} Bootstrap */
type CompilationHooks = {
addContainerEntryModule: SyncHook<[ContainerEntryDependency], void>;
addFederationRuntimeModule: SyncHook<[FederationRuntimeDependency], void>;
};
class FederationModulesPlugin {
options: any;
/**
* @param {Compilation} compilation the compilation
* @returns {CompilationHooks} the attached hooks
*/
static getCompilationHooks(compilation: CompilationType): CompilationHooks {
if (!(compilation instanceof Compilation)) {
throw new TypeError(
"The 'compilation' argument must be an instance of Compilation",
);
}
let hooks = compilationHooksMap.get(compilation);
if (hooks === undefined) {
hooks = {
addContainerEntryModule: new SyncHook(['dependency']),
addFederationRuntimeModule: new SyncHook(['module']),
};
compilationHooksMap.set(compilation, hooks);
}
return hooks;
}
constructor(options = {}) {
this.options = options;
}
apply(compiler: Compiler) {
compiler.hooks.compilation.tap(
PLUGIN_NAME,
(compilation: CompilationType, { normalModuleFactory }) => {
//@ts-ignore
const hooks = FederationModulesPlugin.getCompilationHooks(compilation);
},
);
}
}
export default FederationModulesPlugin;