-
-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathFederationModulesPlugin.ts
More file actions
75 lines (64 loc) · 2.64 KB
/
FederationModulesPlugin.ts
File metadata and controls
75 lines (64 loc) · 2.64 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
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 = {
addContainerEntryDependency: SyncHook<[ContainerEntryDependency], void>;
addFederationRuntimeDependency: SyncHook<[FederationRuntimeDependency], void>;
addRemoteDependency: SyncHook<[any], void>;
};
class FederationModulesPlugin {
options: any;
/**
* @param {Compilation} compilation the compilation
* @returns {CompilationHooks} the attached hooks
*/
static getCompilationHooks(compilation: CompilationType): CompilationHooks {
// Avoid cross-realm instanceof checks (e.g., Jest VM modules) by using
// a duck-typed verification of a Webpack Compilation-like object.
const isLikelyCompilation =
compilation &&
typeof compilation === 'object' &&
// @ts-ignore
typeof (compilation as any).hooks === 'object' &&
// A couple of well-known hooks available on Webpack 5 compilations
// @ts-ignore
typeof (compilation as any).hooks.processAssets?.tap === 'function';
if (!isLikelyCompilation) {
throw new TypeError(
"Invalid 'compilation' argument: expected a Webpack Compilation-like object",
);
}
let hooks = compilationHooksMap.get(compilation);
if (hooks === undefined) {
hooks = {
addContainerEntryDependency: new SyncHook(['dependency']),
addFederationRuntimeDependency: new SyncHook(['dependency']),
addRemoteDependency: new SyncHook(['dependency']),
};
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;