@module-federation/nextjs-mf 8.8.55 → 8.8.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/LICENSE +21 -0
- package/dist/_virtual/_rolldown/runtime.js +29 -0
- package/dist/_virtual/_rolldown/runtime.mjs +8 -0
- package/dist/client/UrlNode.js +89 -147
- package/dist/client/UrlNode.js.map +1 -1
- package/dist/client/UrlNode.mjs +91 -0
- package/dist/client/UrlNode.mjs.map +1 -0
- package/dist/src/federation-noop.d.mts +1 -0
- package/dist/src/federation-noop.d.ts +1 -0
- package/dist/src/federation-noop.js +17 -0
- package/dist/src/federation-noop.js.map +1 -1
- package/dist/src/federation-noop.mjs +23 -0
- package/dist/src/federation-noop.mjs.map +1 -0
- package/dist/src/index.d.mts +2 -0
- package/dist/src/index.d.ts +2 -3
- package/dist/src/index.js +13 -11
- package/dist/src/index.js.map +1 -1
- package/dist/src/index.mjs +8 -0
- package/dist/src/index.mjs.map +1 -0
- package/dist/src/internal.js +140 -243
- package/dist/src/internal.js.map +1 -1
- package/dist/src/internal.mjs +152 -0
- package/dist/src/internal.mjs.map +1 -0
- package/dist/src/loaders/fixImageLoader.d.mts +27 -0
- package/dist/src/loaders/fixImageLoader.d.ts +8 -3
- package/dist/src/loaders/fixImageLoader.js +91 -103
- package/dist/src/loaders/fixImageLoader.js.map +1 -1
- package/dist/src/loaders/fixImageLoader.mjs +101 -0
- package/dist/src/loaders/fixImageLoader.mjs.map +1 -0
- package/dist/src/loaders/fixUrlLoader.d.mts +16 -0
- package/dist/src/loaders/fixUrlLoader.d.ts +5 -2
- package/dist/src/loaders/fixUrlLoader.js +17 -19
- package/dist/src/loaders/fixUrlLoader.js.map +1 -1
- package/dist/src/loaders/fixUrlLoader.mjs +19 -0
- package/dist/src/loaders/fixUrlLoader.mjs.map +1 -0
- package/dist/src/loaders/helpers.js +72 -124
- package/dist/src/loaders/helpers.js.map +1 -1
- package/dist/src/loaders/helpers.mjs +79 -0
- package/dist/src/loaders/helpers.mjs.map +1 -0
- package/dist/src/loaders/nextPageMapLoader.d.mts +29 -0
- package/dist/src/loaders/nextPageMapLoader.d.ts +10 -5
- package/dist/src/loaders/nextPageMapLoader.js +126 -144
- package/dist/src/loaders/nextPageMapLoader.js.map +1 -1
- package/dist/src/loaders/nextPageMapLoader.mjs +143 -0
- package/dist/src/loaders/nextPageMapLoader.mjs.map +1 -0
- package/dist/src/logger.js +7 -7
- package/dist/src/logger.js.map +1 -1
- package/dist/src/logger.mjs +8 -0
- package/dist/src/logger.mjs.map +1 -0
- package/dist/src/plugins/CopyFederationPlugin.js +64 -71
- package/dist/src/plugins/CopyFederationPlugin.js.map +1 -1
- package/dist/src/plugins/CopyFederationPlugin.mjs +65 -0
- package/dist/src/plugins/CopyFederationPlugin.mjs.map +1 -0
- package/dist/src/plugins/NextFederationPlugin/apply-client-plugins.js +36 -53
- package/dist/src/plugins/NextFederationPlugin/apply-client-plugins.js.map +1 -1
- package/dist/src/plugins/NextFederationPlugin/apply-client-plugins.mjs +40 -0
- package/dist/src/plugins/NextFederationPlugin/apply-client-plugins.mjs.map +1 -0
- package/dist/src/plugins/NextFederationPlugin/apply-server-plugins.js +103 -121
- package/dist/src/plugins/NextFederationPlugin/apply-server-plugins.js.map +1 -1
- package/dist/src/plugins/NextFederationPlugin/apply-server-plugins.mjs +111 -0
- package/dist/src/plugins/NextFederationPlugin/apply-server-plugins.mjs.map +1 -0
- package/dist/src/plugins/NextFederationPlugin/index.d.mts +31 -0
- package/dist/src/plugins/NextFederationPlugin/index.d.ts +27 -23
- package/dist/src/plugins/NextFederationPlugin/index.js +147 -181
- package/dist/src/plugins/NextFederationPlugin/index.js.map +1 -1
- package/dist/src/plugins/NextFederationPlugin/index.mjs +150 -0
- package/dist/src/plugins/NextFederationPlugin/index.mjs.map +1 -0
- package/dist/src/plugins/NextFederationPlugin/next-fragments.d.mts +18 -0
- package/dist/src/plugins/NextFederationPlugin/next-fragments.d.ts +16 -18
- package/dist/src/plugins/NextFederationPlugin/next-fragments.js +62 -92
- package/dist/src/plugins/NextFederationPlugin/next-fragments.js.map +1 -1
- package/dist/src/plugins/NextFederationPlugin/next-fragments.mjs +68 -0
- package/dist/src/plugins/NextFederationPlugin/next-fragments.mjs.map +1 -0
- package/dist/src/plugins/NextFederationPlugin/set-options.js +17 -19
- package/dist/src/plugins/NextFederationPlugin/set-options.js.map +1 -1
- package/dist/src/plugins/NextFederationPlugin/set-options.mjs +19 -0
- package/dist/src/plugins/NextFederationPlugin/set-options.mjs.map +1 -0
- package/dist/src/plugins/NextFederationPlugin/validate-options.js +30 -38
- package/dist/src/plugins/NextFederationPlugin/validate-options.js.map +1 -1
- package/dist/src/plugins/NextFederationPlugin/validate-options.mjs +35 -0
- package/dist/src/plugins/NextFederationPlugin/validate-options.mjs.map +1 -0
- package/dist/src/plugins/container/InvertedContainerPlugin.js +22 -25
- package/dist/src/plugins/container/InvertedContainerPlugin.js.map +1 -1
- package/dist/src/plugins/container/InvertedContainerPlugin.mjs +23 -0
- package/dist/src/plugins/container/InvertedContainerPlugin.mjs.map +1 -0
- package/dist/src/plugins/container/InvertedContainerRuntimeModule.js +55 -67
- package/dist/src/plugins/container/InvertedContainerRuntimeModule.js.map +1 -1
- package/dist/src/plugins/container/InvertedContainerRuntimeModule.mjs +57 -0
- package/dist/src/plugins/container/InvertedContainerRuntimeModule.mjs.map +1 -0
- package/dist/src/plugins/container/runtimePlugin.d.mts +7 -0
- package/dist/src/plugins/container/runtimePlugin.d.ts +6 -2
- package/dist/src/plugins/container/runtimePlugin.js +167 -0
- package/dist/src/plugins/container/runtimePlugin.js.map +1 -1
- package/dist/src/plugins/container/runtimePlugin.mjs +166 -0
- package/dist/src/plugins/container/runtimePlugin.mjs.map +1 -0
- package/dist/utils/flushedChunks.d.mts +29 -0
- package/dist/utils/flushedChunks.d.ts +12 -5
- package/dist/utils/flushedChunks.js +32 -72
- package/dist/utils/flushedChunks.js.map +1 -1
- package/dist/utils/flushedChunks.mjs +35 -0
- package/dist/utils/flushedChunks.mjs.map +1 -0
- package/dist/utils/index.d.mts +14 -0
- package/dist/utils/index.d.ts +8 -14
- package/dist/utils/index.js +27 -62
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +22 -0
- package/dist/utils/index.mjs.map +1 -0
- package/package.json +29 -9
- package/dist/client/UrlNode.d.ts +0 -17
- package/dist/node.d.ts +0 -1
- package/dist/node.js +0 -18
- package/dist/node.js.map +0 -1
- package/dist/package.json +0 -80
- package/dist/src/federation-noop.cjs +0 -15
- package/dist/src/internal.d.ts +0 -50
- package/dist/src/loaders/helpers.d.ts +0 -17
- package/dist/src/logger.d.ts +0 -2
- package/dist/src/plugins/AddRuntimeRequirementToPromiseExternalPlugin.d.ts +0 -5
- package/dist/src/plugins/AddRuntimeRequirementToPromiseExternalPlugin.js +0 -19
- package/dist/src/plugins/AddRuntimeRequirementToPromiseExternalPlugin.js.map +0 -1
- package/dist/src/plugins/CopyFederationPlugin.d.ts +0 -20
- package/dist/src/plugins/NextFederationPlugin/apply-client-plugins.d.ts +0 -23
- package/dist/src/plugins/NextFederationPlugin/apply-server-plugins.d.ts +0 -36
- package/dist/src/plugins/NextFederationPlugin/regex-equal.d.ts +0 -14
- package/dist/src/plugins/NextFederationPlugin/regex-equal.js +0 -25
- package/dist/src/plugins/NextFederationPlugin/regex-equal.js.map +0 -1
- package/dist/src/plugins/NextFederationPlugin/remove-unnecessary-shared-keys.d.ts +0 -8
- package/dist/src/plugins/NextFederationPlugin/remove-unnecessary-shared-keys.js +0 -34
- package/dist/src/plugins/NextFederationPlugin/remove-unnecessary-shared-keys.js.map +0 -1
- package/dist/src/plugins/NextFederationPlugin/set-options.d.ts +0 -16
- package/dist/src/plugins/NextFederationPlugin/validate-options.d.ts +0 -25
- package/dist/src/plugins/container/InvertedContainerPlugin.d.ts +0 -5
- package/dist/src/plugins/container/InvertedContainerRuntimeModule.d.ts +0 -11
- package/dist/src/plugins/container/RemoveEagerModulesFromRuntimePlugin.d.ts +0 -39
- package/dist/src/plugins/container/RemoveEagerModulesFromRuntimePlugin.js +0 -78
- package/dist/src/plugins/container/RemoveEagerModulesFromRuntimePlugin.js.map +0 -1
- package/dist/src/plugins/container/runtimePlugin.cjs +0 -219
- package/dist/src/plugins/container/types.d.ts +0 -2
- package/dist/src/plugins/container/types.js +0 -3
- package/dist/src/plugins/container/types.js.map +0 -1
- package/dist/src/types.d.ts +0 -28
- package/dist/src/types.js +0 -3
- package/dist/src/types.js.map +0 -1
|
@@ -1,135 +1,117 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
const require_runtime = require('../../../_virtual/_rolldown/runtime.js');
|
|
2
|
+
const require_InvertedContainerPlugin = require('../container/InvertedContainerPlugin.js');
|
|
3
|
+
let path = require("path");
|
|
4
|
+
path = require_runtime.__toESM(path);
|
|
5
|
+
let _module_federation_node_universe_entry_chunk_tracker_plugin = require("@module-federation/node/universe-entry-chunk-tracker-plugin");
|
|
6
|
+
_module_federation_node_universe_entry_chunk_tracker_plugin = require_runtime.__toESM(_module_federation_node_universe_entry_chunk_tracker_plugin);
|
|
7
|
+
|
|
8
|
+
//#region src/plugins/NextFederationPlugin/apply-server-plugins.ts
|
|
9
|
+
const isExternalItemValue = (value) => {
|
|
10
|
+
return typeof value === "string" || typeof value === "boolean" || Array.isArray(value) || !!value && typeof value === "object";
|
|
11
|
+
};
|
|
12
|
+
const runExternalFunction = async (external, data) => {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
let settled = false;
|
|
15
|
+
const settle = (err, result) => {
|
|
16
|
+
if (settled) return;
|
|
17
|
+
settled = true;
|
|
18
|
+
if (err) {
|
|
19
|
+
reject(err);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (isExternalItemValue(result)) {
|
|
23
|
+
resolve(result);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
resolve(void 0);
|
|
27
|
+
};
|
|
28
|
+
const maybePromise = external(data, (err, result) => {
|
|
29
|
+
settle(err, result);
|
|
30
|
+
});
|
|
31
|
+
if (maybePromise !== void 0) Promise.resolve(maybePromise).then((result) => {
|
|
32
|
+
settle(void 0, result);
|
|
33
|
+
}).catch((error) => {
|
|
34
|
+
settle(error instanceof Error ? error : new Error(String(error)));
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
const isExternalItemFunction = (external) => {
|
|
39
|
+
return typeof external === "function";
|
|
40
|
+
};
|
|
41
|
+
const isSharedImportEnabled = (sharedConfigValue) => {
|
|
42
|
+
if (!sharedConfigValue || typeof sharedConfigValue !== "object") return true;
|
|
43
|
+
if (!Object.prototype.hasOwnProperty.call(sharedConfigValue, "import")) return true;
|
|
44
|
+
return Reflect.get(sharedConfigValue, "import") !== false;
|
|
4
45
|
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.modifyEntry = modifyEntry;
|
|
7
|
-
exports.applyServerPlugins = applyServerPlugins;
|
|
8
|
-
exports.configureServerLibraryAndFilename = configureServerLibraryAndFilename;
|
|
9
|
-
exports.handleServerExternals = handleServerExternals;
|
|
10
|
-
exports.configureServerCompilerOptions = configureServerCompilerOptions;
|
|
11
|
-
const path_1 = __importDefault(require("path"));
|
|
12
|
-
const InvertedContainerPlugin_1 = __importDefault(require("../container/InvertedContainerPlugin"));
|
|
13
|
-
const universe_entry_chunk_tracker_plugin_1 = __importDefault(require("@module-federation/node/universe-entry-chunk-tracker-plugin"));
|
|
14
|
-
// Modifies the Webpack entry configuration
|
|
15
|
-
function modifyEntry(options) {
|
|
16
|
-
const { compiler, staticEntry, prependEntry } = options;
|
|
17
|
-
const operator = (oriEntry, newEntry) => Object.assign(oriEntry, newEntry);
|
|
18
|
-
// If the entry is a function, wrap it to modify the result
|
|
19
|
-
if (typeof compiler.options.entry === 'function') {
|
|
20
|
-
const prevEntryFn = compiler.options.entry;
|
|
21
|
-
compiler.options.entry = async () => {
|
|
22
|
-
let res = await prevEntryFn();
|
|
23
|
-
if (staticEntry) {
|
|
24
|
-
res = operator(res, staticEntry);
|
|
25
|
-
}
|
|
26
|
-
if (prependEntry) {
|
|
27
|
-
prependEntry(res);
|
|
28
|
-
}
|
|
29
|
-
return res;
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
// If the entry is an object, directly modify it
|
|
34
|
-
if (staticEntry) {
|
|
35
|
-
compiler.options.entry = operator(compiler.options.entry, staticEntry);
|
|
36
|
-
}
|
|
37
|
-
if (prependEntry) {
|
|
38
|
-
prependEntry(compiler.options.entry);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
46
|
/**
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
* Applies server-specific plugins to the webpack compiler.
|
|
48
|
+
*
|
|
49
|
+
* @param {Compiler} compiler - The Webpack compiler instance.
|
|
50
|
+
* @param {moduleFederationPlugin.ModuleFederationPluginOptions} options - The ModuleFederationPluginOptions instance.
|
|
51
|
+
*/
|
|
48
52
|
function applyServerPlugins(compiler, options) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
!chunkFileName.includes(uniqueName)) {
|
|
56
|
-
compiler.options.output.chunkFilename = chunkFileName.replace('.js', suffix);
|
|
57
|
-
}
|
|
58
|
-
new universe_entry_chunk_tracker_plugin_1.default().apply(compiler);
|
|
59
|
-
new InvertedContainerPlugin_1.default().apply(compiler);
|
|
53
|
+
const chunkFileName = compiler.options?.output?.chunkFilename;
|
|
54
|
+
const uniqueName = compiler?.options?.output?.uniqueName || options.name;
|
|
55
|
+
const suffix = `-[contenthash].js`;
|
|
56
|
+
if (typeof chunkFileName === "string" && uniqueName && !chunkFileName.includes(uniqueName)) compiler.options.output.chunkFilename = chunkFileName.replace(".js", suffix);
|
|
57
|
+
new _module_federation_node_universe_entry_chunk_tracker_plugin.default().apply(compiler);
|
|
58
|
+
new require_InvertedContainerPlugin.default().apply(compiler);
|
|
60
59
|
}
|
|
61
60
|
/**
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
61
|
+
* Configures server-specific library and filename options.
|
|
62
|
+
*
|
|
63
|
+
* @param {ModuleFederationPluginOptions} options - The ModuleFederationPluginOptions instance.
|
|
64
|
+
*/
|
|
66
65
|
function configureServerLibraryAndFilename(options) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
// Set the filename option to the basename of the current filename
|
|
73
|
-
options.filename = path_1.default.basename(options.filename);
|
|
66
|
+
options.library = {
|
|
67
|
+
type: "commonjs-module",
|
|
68
|
+
name: options.name
|
|
69
|
+
};
|
|
70
|
+
if (typeof options.filename === "string") options.filename = path.default.basename(options.filename);
|
|
74
71
|
}
|
|
75
72
|
/**
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
73
|
+
* Patches Next.js' default externals function to ensure shared modules are bundled and not treated as external.
|
|
74
|
+
*
|
|
75
|
+
* @param {Compiler} compiler - The Webpack compiler instance.
|
|
76
|
+
* @param {ModuleFederationPluginOptions} options - The ModuleFederationPluginOptions instance.
|
|
77
|
+
*/
|
|
81
78
|
function handleServerExternals(compiler, options) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
if (req.startsWith('next') ||
|
|
103
|
-
req.startsWith('react/') ||
|
|
104
|
-
req.startsWith('react-dom/') ||
|
|
105
|
-
req === 'react' ||
|
|
106
|
-
req === 'styled-jsx/style' ||
|
|
107
|
-
req === 'react-dom') {
|
|
108
|
-
return fromNext;
|
|
109
|
-
}
|
|
110
|
-
return;
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
}
|
|
79
|
+
if (Array.isArray(compiler.options.externals)) {
|
|
80
|
+
const functionIndex = compiler.options.externals.findIndex((external) => isExternalItemFunction(external));
|
|
81
|
+
if (functionIndex !== -1) {
|
|
82
|
+
const originalExternals = compiler.options.externals[functionIndex];
|
|
83
|
+
if (!isExternalItemFunction(originalExternals)) return;
|
|
84
|
+
compiler.options.externals[functionIndex] = async (ctx) => {
|
|
85
|
+
const fromNext = await runExternalFunction(originalExternals, ctx);
|
|
86
|
+
if (typeof fromNext !== "string") return fromNext;
|
|
87
|
+
const req = fromNext.split(" ")[1];
|
|
88
|
+
if (!req) return;
|
|
89
|
+
const isSharedRequest = (options.shared && !Array.isArray(options.shared) && typeof options.shared === "object" ? Object.entries(options.shared) : []).some(([key, sharedConfigValue]) => {
|
|
90
|
+
if (!isSharedImportEnabled(sharedConfigValue)) return false;
|
|
91
|
+
return key.endsWith("/") ? req.includes(key) : req === key;
|
|
92
|
+
});
|
|
93
|
+
if (ctx.request && (ctx.request.includes("@module-federation/utilities") || isSharedRequest || ctx.request.includes("@module-federation/"))) return;
|
|
94
|
+
if (req.startsWith("next") || req.startsWith("react/") || req.startsWith("react-dom/") || req === "react" || req === "styled-jsx/style" || req === "react-dom") return fromNext;
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
114
98
|
}
|
|
115
99
|
/**
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
100
|
+
* Configures server-specific compiler options.
|
|
101
|
+
*
|
|
102
|
+
* @param {Compiler} compiler - The Webpack compiler instance.
|
|
103
|
+
*/
|
|
120
104
|
function configureServerCompilerOptions(compiler) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
// Set the compiler target to 'async-node' for server-side rendering compatibility
|
|
127
|
-
// Set the target to 'async-node' for server-side builds
|
|
128
|
-
compiler.options.target = 'async-node';
|
|
129
|
-
// Runtime chunk creation is currently disabled
|
|
130
|
-
// Uncomment if separate runtime chunk is needed for specific use cases
|
|
131
|
-
// compiler.options.optimization.runtimeChunk = {
|
|
132
|
-
// name: 'webpack-runtime',
|
|
133
|
-
// };
|
|
105
|
+
compiler.options.node = {
|
|
106
|
+
...compiler.options.node,
|
|
107
|
+
global: false
|
|
108
|
+
};
|
|
109
|
+
compiler.options.target = "async-node";
|
|
134
110
|
}
|
|
111
|
+
|
|
112
|
+
//#endregion
|
|
113
|
+
exports.applyServerPlugins = applyServerPlugins;
|
|
114
|
+
exports.configureServerCompilerOptions = configureServerCompilerOptions;
|
|
115
|
+
exports.configureServerLibraryAndFilename = configureServerLibraryAndFilename;
|
|
116
|
+
exports.handleServerExternals = handleServerExternals;
|
|
135
117
|
//# sourceMappingURL=apply-server-plugins.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apply-server-plugins.js","sourceRoot":"","sources":["../../../../src/plugins/NextFederationPlugin/apply-server-plugins.ts"],"names":[],"mappings":";;;;;AAqBA,kCA6BC;AAQD,gDAqBC;AAOD,8EAWC;AAQD,sDAwDC;AAOD,wEAeC;AAjLD,gDAAwB;AACxB,mGAA2E;AAC3E,sIAA0G;AAY1G,2CAA2C;AAC3C,SAAgB,WAAW,CAAC,OAA2B;IACrD,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC;IACxD,MAAM,QAAQ,GAAG,CACf,QAA+B,EAC/B,QAA+B,EACR,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAE9D,2DAA2D;IAC3D,IAAI,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,KAAK,UAAU,EAAE,CAAC;QACjD,MAAM,WAAW,GAAG,QAAQ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verseEntryChunkTrackerPlugin","InvertedContainerPlugin"],"sources":["../../../../src/plugins/NextFederationPlugin/apply-server-plugins.ts"],"sourcesContent":["import type { WebpackOptionsNormalized, Compiler } from 'webpack';\nimport type ExternalModuleFactoryPlugin from 'webpack/lib/ExternalModuleFactoryPlugin';\nimport type { moduleFederationPlugin } from '@module-federation/sdk';\nimport path from 'path';\nimport InvertedContainerPlugin from '../container/InvertedContainerPlugin';\nimport UniverseEntryChunkTrackerPlugin from '@module-federation/node/universe-entry-chunk-tracker-plugin';\n\ntype EntryStaticNormalized = Awaited<\n ReturnType<Extract<WebpackOptionsNormalized['entry'], () => any>>\n>;\n\ninterface ModifyEntryOptions {\n compiler: Compiler;\n prependEntry?: (entry: EntryStaticNormalized) => void;\n staticEntry?: EntryStaticNormalized;\n}\n\ntype ExternalItemFunction =\n ExternalModuleFactoryPlugin.ExternalItemFunctionCallback;\ntype ExternalItemFunctionData =\n ExternalModuleFactoryPlugin.ExternalItemFunctionData;\ntype ExternalItemValue = ExternalModuleFactoryPlugin.ExternalItemValue;\n\nconst isExternalItemValue = (value: unknown): value is ExternalItemValue => {\n return (\n typeof value === 'string' ||\n typeof value === 'boolean' ||\n Array.isArray(value) ||\n (!!value && typeof value === 'object')\n );\n};\n\nconst runExternalFunction = async (\n external: ExternalItemFunction,\n data: ExternalItemFunctionData,\n): Promise<ExternalItemValue | undefined> => {\n return new Promise((resolve, reject) => {\n let settled = false;\n const settle = (err?: Error | null, result?: unknown) => {\n if (settled) {\n return;\n }\n settled = true;\n if (err) {\n reject(err);\n return;\n }\n if (isExternalItemValue(result)) {\n resolve(result);\n return;\n }\n resolve(undefined);\n };\n\n const maybePromise: unknown = external(data, (err, result) => {\n settle(err, result);\n });\n\n if (maybePromise !== undefined) {\n Promise.resolve(maybePromise)\n .then((result) => {\n settle(undefined, result);\n })\n .catch((error: unknown) => {\n const normalizedError =\n error instanceof Error ? error : new Error(String(error));\n settle(normalizedError);\n });\n }\n });\n};\n\nconst isExternalItemFunction = (\n external: unknown,\n): external is ExternalItemFunction => {\n return typeof external === 'function';\n};\n\nconst isSharedImportEnabled = (sharedConfigValue: unknown): boolean => {\n if (!sharedConfigValue || typeof sharedConfigValue !== 'object') {\n return true;\n }\n if (!Object.prototype.hasOwnProperty.call(sharedConfigValue, 'import')) {\n return true;\n }\n return Reflect.get(sharedConfigValue, 'import') !== false;\n};\n\n// Modifies the Webpack entry configuration\nexport function modifyEntry(options: ModifyEntryOptions): void {\n const { compiler, staticEntry, prependEntry } = options;\n const operator = (\n oriEntry: EntryStaticNormalized,\n newEntry: EntryStaticNormalized,\n ): EntryStaticNormalized => Object.assign(oriEntry, newEntry);\n\n // If the entry is a function, wrap it to modify the result\n if (typeof compiler.options.entry === 'function') {\n const prevEntryFn = compiler.options.entry;\n compiler.options.entry = async () => {\n let res = await prevEntryFn();\n if (staticEntry) {\n res = operator(res, staticEntry);\n }\n if (prependEntry) {\n prependEntry(res);\n }\n return res;\n };\n } else {\n // If the entry is an object, directly modify it\n if (staticEntry) {\n compiler.options.entry = operator(compiler.options.entry, staticEntry);\n }\n if (prependEntry) {\n prependEntry(compiler.options.entry);\n }\n }\n}\n\n/**\n * Applies server-specific plugins to the webpack compiler.\n *\n * @param {Compiler} compiler - The Webpack compiler instance.\n * @param {moduleFederationPlugin.ModuleFederationPluginOptions} options - The ModuleFederationPluginOptions instance.\n */\nexport function applyServerPlugins(\n compiler: Compiler,\n options: moduleFederationPlugin.ModuleFederationPluginOptions,\n): void {\n const chunkFileName = compiler.options?.output?.chunkFilename;\n const uniqueName = compiler?.options?.output?.uniqueName || options.name;\n const suffix = `-[contenthash].js`;\n\n // Modify chunk filename to include a unique suffix if not already present\n if (\n typeof chunkFileName === 'string' &&\n uniqueName &&\n !chunkFileName.includes(uniqueName)\n ) {\n compiler.options.output.chunkFilename = chunkFileName.replace(\n '.js',\n suffix,\n );\n }\n new UniverseEntryChunkTrackerPlugin().apply(compiler);\n new InvertedContainerPlugin().apply(compiler);\n}\n\n/**\n * Configures server-specific library and filename options.\n *\n * @param {ModuleFederationPluginOptions} options - The ModuleFederationPluginOptions instance.\n */\nexport function configureServerLibraryAndFilename(\n options: moduleFederationPlugin.ModuleFederationPluginOptions,\n): void {\n // Set the library option to \"commonjs-module\" format with the name from the options\n options.library = {\n type: 'commonjs-module',\n name: options.name,\n };\n\n // Set the filename option to the basename of the current filename\n if (typeof options.filename === 'string') {\n options.filename = path.basename(options.filename);\n }\n}\n\n/**\n * Patches Next.js' default externals function to ensure shared modules are bundled and not treated as external.\n *\n * @param {Compiler} compiler - The Webpack compiler instance.\n * @param {ModuleFederationPluginOptions} options - The ModuleFederationPluginOptions instance.\n */\nexport function handleServerExternals(\n compiler: Compiler,\n options: moduleFederationPlugin.ModuleFederationPluginOptions,\n): void {\n if (Array.isArray(compiler.options.externals)) {\n const functionIndex = compiler.options.externals.findIndex((external) =>\n isExternalItemFunction(external),\n );\n\n if (functionIndex !== -1) {\n const originalExternals = compiler.options.externals[functionIndex];\n if (!isExternalItemFunction(originalExternals)) {\n return;\n }\n\n compiler.options.externals[functionIndex] = async (\n ctx: ExternalItemFunctionData,\n ): Promise<ExternalItemValue | undefined> => {\n const fromNext = await runExternalFunction(originalExternals, ctx);\n if (typeof fromNext !== 'string') {\n return fromNext;\n }\n\n const req = fromNext.split(' ')[1];\n if (!req) {\n return;\n }\n\n const sharedEntries =\n options.shared &&\n !Array.isArray(options.shared) &&\n typeof options.shared === 'object'\n ? Object.entries(options.shared)\n : [];\n const isSharedRequest = sharedEntries.some(\n ([key, sharedConfigValue]) => {\n if (!isSharedImportEnabled(sharedConfigValue)) {\n return false;\n }\n return key.endsWith('/') ? req.includes(key) : req === key;\n },\n );\n\n if (\n ctx.request &&\n (ctx.request.includes('@module-federation/utilities') ||\n isSharedRequest ||\n ctx.request.includes('@module-federation/'))\n ) {\n return;\n }\n\n if (\n req.startsWith('next') ||\n req.startsWith('react/') ||\n req.startsWith('react-dom/') ||\n req === 'react' ||\n req === 'styled-jsx/style' ||\n req === 'react-dom'\n ) {\n return fromNext;\n }\n return;\n };\n }\n }\n}\n\n/**\n * Configures server-specific compiler options.\n *\n * @param {Compiler} compiler - The Webpack compiler instance.\n */\nexport function configureServerCompilerOptions(compiler: Compiler): void {\n // Disable the global option in node builds and set the target to \"async-node\"\n compiler.options.node = {\n ...compiler.options.node,\n global: false,\n };\n // Set the compiler target to 'async-node' for server-side rendering compatibility\n // Set the target to 'async-node' for server-side builds\n compiler.options.target = 'async-node';\n\n // Runtime chunk creation is currently disabled\n // Uncomment if separate runtime chunk is needed for specific use cases\n // compiler.options.optimization.runtimeChunk = {\n // name: 'webpack-runtime',\n // };\n}\n"],"mappings":";;;;;;;;AAuBA,MAAM,uBAAuB,UAA+C;AAC1E,QACE,OAAO,UAAU,YACjB,OAAO,UAAU,aACjB,MAAM,QAAQ,MAAM,IACnB,CAAC,CAAC,SAAS,OAAO,UAAU;;AAIjC,MAAM,sBAAsB,OAC1B,UACA,SAC2C;AAC3C,QAAO,IAAI,SAAS,SAAS,WAAW;EACtC,IAAI,UAAU;EACd,MAAM,UAAU,KAAoB,WAAqB;AACvD,OAAI,QACF;AAEF,aAAU;AACV,OAAI,KAAK;AACP,WAAO,IAAI;AACX;;AAEF,OAAI,oBAAoB,OAAO,EAAE;AAC/B,YAAQ,OAAO;AACf;;AAEF,WAAQ,OAAU;;EAGpB,MAAM,eAAwB,SAAS,OAAO,KAAK,WAAW;AAC5D,UAAO,KAAK,OAAO;IACnB;AAEF,MAAI,iBAAiB,OACnB,SAAQ,QAAQ,aAAa,CAC1B,MAAM,WAAW;AAChB,UAAO,QAAW,OAAO;IACzB,CACD,OAAO,UAAmB;AAGzB,UADE,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,OAAO,MAAM,CAAC,CACpC;IACvB;GAEN;;AAGJ,MAAM,0BACJ,aACqC;AACrC,QAAO,OAAO,aAAa;;AAG7B,MAAM,yBAAyB,sBAAwC;AACrE,KAAI,CAAC,qBAAqB,OAAO,sBAAsB,SAC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AC8B,MACnC,CAAC,KAAK,uBAAuB;AAC5B,SAAI,CAAC,sBAAsB,kBAAkB,CAC3C,QAAO;AAET,YAAO,IAAI,SAAS,IAAI,GAAG,IAAI,SAAS,IAAI,GAAG,QAAQ;MAE1D;AAED,QACE,IAAI,YACH,IAAI,QAAQ,SAAS,+BAA+B,IACnD,mBACA,IAAI,QAAQ,SAAS,sBAAsB,EAE7C;AAGF,QACE,IAAI,WAAW,OAAO,IACtB,IAAI,WAAW,SAAS,IACxB,IAAI,WAAW,aAAa,IAC5B,QAAQ,WACR,QAAQ,sBACR,QAAQ,YAER,QAAO;;;;;;;;;;AAajB,SAAgB,+BAA+B,UAA0B;AAEvE,UAAS,QAAQ,OAAO;EACtB,GAAG,SAAS,QAAQ;EACpB,QAAQ;EACT;AAGD,UAAS,QAAQ,SAAS"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import InvertedContainerPlugin from "../container/InvertedContainerPlugin.mjs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import UniverseEntryChunkTrackerPlugin from "@module-federation/node/universe-entry-chunk-tracker-plugin";
|
|
4
|
+
|
|
5
|
+
//#region src/plugins/NextFederationPlugin/apply-server-plugins.ts
|
|
6
|
+
const isExternalItemValue = (value) => {
|
|
7
|
+
return typeof value === "string" || typeof value === "boolean" || Array.isArray(value) || !!value && typeof value === "object";
|
|
8
|
+
};
|
|
9
|
+
const runExternalFunction = async (external, data) => {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
let settled = false;
|
|
12
|
+
const settle = (err, result) => {
|
|
13
|
+
if (settled) return;
|
|
14
|
+
settled = true;
|
|
15
|
+
if (err) {
|
|
16
|
+
reject(err);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (isExternalItemValue(result)) {
|
|
20
|
+
resolve(result);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
resolve(void 0);
|
|
24
|
+
};
|
|
25
|
+
const maybePromise = external(data, (err, result) => {
|
|
26
|
+
settle(err, result);
|
|
27
|
+
});
|
|
28
|
+
if (maybePromise !== void 0) Promise.resolve(maybePromise).then((result) => {
|
|
29
|
+
settle(void 0, result);
|
|
30
|
+
}).catch((error) => {
|
|
31
|
+
settle(error instanceof Error ? error : new Error(String(error)));
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
const isExternalItemFunction = (external) => {
|
|
36
|
+
return typeof external === "function";
|
|
37
|
+
};
|
|
38
|
+
const isSharedImportEnabled = (sharedConfigValue) => {
|
|
39
|
+
if (!sharedConfigValue || typeof sharedConfigValue !== "object") return true;
|
|
40
|
+
if (!Object.prototype.hasOwnProperty.call(sharedConfigValue, "import")) return true;
|
|
41
|
+
return Reflect.get(sharedConfigValue, "import") !== false;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Applies server-specific plugins to the webpack compiler.
|
|
45
|
+
*
|
|
46
|
+
* @param {Compiler} compiler - The Webpack compiler instance.
|
|
47
|
+
* @param {moduleFederationPlugin.ModuleFederationPluginOptions} options - The ModuleFederationPluginOptions instance.
|
|
48
|
+
*/
|
|
49
|
+
function applyServerPlugins(compiler, options) {
|
|
50
|
+
const chunkFileName = compiler.options?.output?.chunkFilename;
|
|
51
|
+
const uniqueName = compiler?.options?.output?.uniqueName || options.name;
|
|
52
|
+
const suffix = `-[contenthash].js`;
|
|
53
|
+
if (typeof chunkFileName === "string" && uniqueName && !chunkFileName.includes(uniqueName)) compiler.options.output.chunkFilename = chunkFileName.replace(".js", suffix);
|
|
54
|
+
new UniverseEntryChunkTrackerPlugin().apply(compiler);
|
|
55
|
+
new InvertedContainerPlugin().apply(compiler);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Configures server-specific library and filename options.
|
|
59
|
+
*
|
|
60
|
+
* @param {ModuleFederationPluginOptions} options - The ModuleFederationPluginOptions instance.
|
|
61
|
+
*/
|
|
62
|
+
function configureServerLibraryAndFilename(options) {
|
|
63
|
+
options.library = {
|
|
64
|
+
type: "commonjs-module",
|
|
65
|
+
name: options.name
|
|
66
|
+
};
|
|
67
|
+
if (typeof options.filename === "string") options.filename = path.basename(options.filename);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Patches Next.js' default externals function to ensure shared modules are bundled and not treated as external.
|
|
71
|
+
*
|
|
72
|
+
* @param {Compiler} compiler - The Webpack compiler instance.
|
|
73
|
+
* @param {ModuleFederationPluginOptions} options - The ModuleFederationPluginOptions instance.
|
|
74
|
+
*/
|
|
75
|
+
function handleServerExternals(compiler, options) {
|
|
76
|
+
if (Array.isArray(compiler.options.externals)) {
|
|
77
|
+
const functionIndex = compiler.options.externals.findIndex((external) => isExternalItemFunction(external));
|
|
78
|
+
if (functionIndex !== -1) {
|
|
79
|
+
const originalExternals = compiler.options.externals[functionIndex];
|
|
80
|
+
if (!isExternalItemFunction(originalExternals)) return;
|
|
81
|
+
compiler.options.externals[functionIndex] = async (ctx) => {
|
|
82
|
+
const fromNext = await runExternalFunction(originalExternals, ctx);
|
|
83
|
+
if (typeof fromNext !== "string") return fromNext;
|
|
84
|
+
const req = fromNext.split(" ")[1];
|
|
85
|
+
if (!req) return;
|
|
86
|
+
const isSharedRequest = (options.shared && !Array.isArray(options.shared) && typeof options.shared === "object" ? Object.entries(options.shared) : []).some(([key, sharedConfigValue]) => {
|
|
87
|
+
if (!isSharedImportEnabled(sharedConfigValue)) return false;
|
|
88
|
+
return key.endsWith("/") ? req.includes(key) : req === key;
|
|
89
|
+
});
|
|
90
|
+
if (ctx.request && (ctx.request.includes("@module-federation/utilities") || isSharedRequest || ctx.request.includes("@module-federation/"))) return;
|
|
91
|
+
if (req.startsWith("next") || req.startsWith("react/") || req.startsWith("react-dom/") || req === "react" || req === "styled-jsx/style" || req === "react-dom") return fromNext;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Configures server-specific compiler options.
|
|
98
|
+
*
|
|
99
|
+
* @param {Compiler} compiler - The Webpack compiler instance.
|
|
100
|
+
*/
|
|
101
|
+
function configureServerCompilerOptions(compiler) {
|
|
102
|
+
compiler.options.node = {
|
|
103
|
+
...compiler.options.node,
|
|
104
|
+
global: false
|
|
105
|
+
};
|
|
106
|
+
compiler.options.target = "async-node";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
//#endregion
|
|
110
|
+
export { applyServerPlugins, configureServerCompilerOptions, configureServerLibraryAndFilename, handleServerExternals };
|
|
111
|
+
//# sourceMappingURL=apply-server-plugins.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apply-server-plugins.mjs","names":[],"sources":["../../../../src/plugins/NextFederationPlugin/apply-server-plugins.ts"],"sourcesContent":["import type { WebpackOptionsNormalized, Compiler } from 'webpack';\nimport type ExternalModuleFactoryPlugin from 'webpack/lib/ExternalModuleFactoryPlugin';\nimport type { moduleFederationPlugin } from '@module-federation/sdk';\nimport path from 'path';\nimport InvertedContainerPlugin from '../container/InvertedContainerPlugin';\nimport UniverseEntryChunkTrackerPlugin from '@module-federation/node/universe-entry-chunk-tracker-plugin';\n\ntype EntryStaticNormalized = Awaited<\n ReturnType<Extract<WebpackOptionsNormalized['entry'], () => any>>\n>;\n\ninterface ModifyEntryOptions {\n compiler: Compiler;\n prependEntry?: (entry: EntryStaticNormalized) => void;\n staticEntry?: EntryStaticNormalized;\n}\n\ntype ExternalItemFunction =\n ExternalModuleFactoryPlugin.ExternalItemFunctionCallback;\ntype ExternalItemFunctionData =\n ExternalModuleFactoryPlugin.ExternalItemFunctionData;\ntype ExternalItemValue = ExternalModuleFactoryPlugin.ExternalItemValue;\n\nconst isExternalItemValue = (value: unknown): value is ExternalItemValue => {\n return (\n typeof value === 'string' ||\n typeof value === 'boolean' ||\n Array.isArray(value) ||\n (!!value && typeof value === 'object')\n );\n};\n\nconst runExternalFunction = async (\n external: ExternalItemFunction,\n data: ExternalItemFunctionData,\n): Promise<ExternalItemValue | undefined> => {\n return new Promise((resolve, reject) => {\n let settled = false;\n const settle = (err?: Error | null, result?: unknown) => {\n if (settled) {\n return;\n }\n settled = true;\n if (err) {\n reject(err);\n return;\n }\n if (isExternalItemValue(result)) {\n resolve(result);\n return;\n }\n resolve(undefined);\n };\n\n const maybePromise: unknown = external(data, (err, result) => {\n settle(err, result);\n });\n\n if (maybePromise !== undefined) {\n Promise.resolve(maybePromise)\n .then((result) => {\n settle(undefined, result);\n })\n .catch((error: unknown) => {\n const normalizedError =\n error instanceof Error ? error : new Error(String(error));\n settle(normalizedError);\n });\n }\n });\n};\n\nconst isExternalItemFunction = (\n external: unknown,\n): external is ExternalItemFunction => {\n return typeof external === 'function';\n};\n\nconst isSharedImportEnabled = (sharedConfigValue: unknown): boolean => {\n if (!sharedConfigValue || typeof sharedConfigValue !== 'object') {\n return true;\n }\n if (!Object.prototype.hasOwnProperty.call(sharedConfigValue, 'import')) {\n return true;\n }\n return Reflect.get(sharedConfigValue, 'import') !== false;\n};\n\n// Modifies the Webpack entry configuration\nexport function modifyEntry(options: ModifyEntryOptions): void {\n const { compiler, staticEntry, prependEntry } = options;\n const operator = (\n oriEntry: EntryStaticNormalized,\n newEntry: EntryStaticNormalized,\n ): EntryStaticNormalized => Object.assign(oriEntry, newEntry);\n\n // If the entry is a function, wrap it to modify the result\n if (typeof compiler.options.entry === 'function') {\n const prevEntryFn = compiler.options.entry;\n compiler.options.entry = async () => {\n let res = await prevEntryFn();\n if (staticEntry) {\n res = operator(res, staticEntry);\n }\n if (prependEntry) {\n prependEntry(res);\n }\n return res;\n };\n } else {\n // If the entry is an object, directly modify it\n if (staticEntry) {\n compiler.options.entry = operator(compiler.options.entry, staticEntry);\n }\n if (prependEntry) {\n prependEntry(compiler.options.entry);\n }\n }\n}\n\n/**\n * Applies server-specific plugins to the webpack compiler.\n *\n * @param {Compiler} compiler - The Webpack compiler instance.\n * @param {moduleFederationPlugin.ModuleFederationPluginOptions} options - The ModuleFederationPluginOptions instance.\n */\nexport function applyServerPlugins(\n compiler: Compiler,\n options: moduleFederationPlugin.ModuleFederationPluginOptions,\n): void {\n const chunkFileName = compiler.options?.output?.chunkFilename;\n const uniqueName = compiler?.options?.output?.uniqueName || options.name;\n const suffix = `-[contenthash].js`;\n\n // Modify chunk filename to include a unique suffix if not already present\n if (\n typeof chunkFileName === 'string' &&\n uniqueName &&\n !chunkFileName.includes(uniqueName)\n ) {\n compiler.options.output.chunkFilename = chunkFileName.replace(\n '.js',\n suffix,\n );\n }\n new UniverseEntryChunkTrackerPlugin().apply(compiler);\n new InvertedContainerPlugin().apply(compiler);\n}\n\n/**\n * Configures server-specific library and filename options.\n *\n * @param {ModuleFederationPluginOptions} options - The ModuleFederationPluginOptions instance.\n */\nexport function configureServerLibraryAndFilename(\n options: moduleFederationPlugin.ModuleFederationPluginOptions,\n): void {\n // Set the library option to \"commonjs-module\" format with the name from the options\n options.library = {\n type: 'commonjs-module',\n name: options.name,\n };\n\n // Set the filename option to the basename of the current filename\n if (typeof options.filename === 'string') {\n options.filename = path.basename(options.filename);\n }\n}\n\n/**\n * Patches Next.js' default externals function to ensure shared modules are bundled and not treated as external.\n *\n * @param {Compiler} compiler - The Webpack compiler instance.\n * @param {ModuleFederationPluginOptions} options - The ModuleFederationPluginOptions instance.\n */\nexport function handleServerExternals(\n compiler: Compiler,\n options: moduleFederationPlugin.ModuleFederationPluginOptions,\n): void {\n if (Array.isArray(compiler.options.externals)) {\n const functionIndex = compiler.options.externals.findIndex((external) =>\n isExternalItemFunction(external),\n );\n\n if (functionIndex !== -1) {\n const originalExternals = compiler.options.externals[functionIndex];\n if (!isExternalItemFunction(originalExternals)) {\n return;\n }\n\n compiler.options.externals[functionIndex] = async (\n ctx: ExternalItemFunctionData,\n ): Promise<ExternalItemValue | undefined> => {\n const fromNext = await runExternalFunction(originalExternals, ctx);\n if (typeof fromNext !== 'string') {\n return fromNext;\n }\n\n const req = fromNext.split(' ')[1];\n if (!req) {\n return;\n }\n\n const sharedEntries =\n options.shared &&\n !Array.isArray(options.shared) &&\n typeof options.shared === 'object'\n ? Object.entries(options.shared)\n : [];\n const isSharedRequest = sharedEntries.some(\n ([key, sharedConfigValue]) => {\n if (!isSharedImportEnabled(sharedConfigValue)) {\n return false;\n }\n return key.endsWith('/') ? req.includes(key) : req === key;\n },\n );\n\n if (\n ctx.request &&\n (ctx.request.includes('@module-federation/utilities') ||\n isSharedRequest ||\n ctx.request.includes('@module-federation/'))\n ) {\n return;\n }\n\n if (\n req.startsWith('next') ||\n req.startsWith('react/') ||\n req.startsWith('react-dom/') ||\n req === 'react' ||\n req === 'styled-jsx/style' ||\n req === 'react-dom'\n ) {\n return fromNext;\n }\n return;\n };\n }\n }\n}\n\n/**\n * Configures server-specific compiler options.\n *\n * @param {Compiler} compiler - The Webpack compiler instance.\n */\nexport function configureServerCompilerOptions(compiler: Compiler): void {\n // Disable the global option in node builds and set the target to \"async-node\"\n compiler.options.node = {\n ...compiler.options.node,\n global: false,\n };\n // Set the compiler target to 'async-node' for server-side rendering compatibility\n // Set the target to 'async-node' for server-side builds\n compiler.options.target = 'async-node';\n\n // Runtime chunk creation is currently disabled\n // Uncomment if separate runtime chunk is needed for specific use cases\n // compiler.options.optimization.runtimeChunk = {\n // name: 'webpack-runtime',\n // };\n}\n"],"mappings":";;;;;AAuBA,MAAM,uBAAuB,UAA+C;AAC1E,QACE,OAAO,UAAU,YACjB,OAAO,UAAU,aACjB,MAAM,QAAQ,MAAM,IACnB,CAAC,CAAC,SAAS,OAAO,UAAU;;AAIjC,MAAM,sBAAsB,OAC1B,UACA,SAC2C;AAC3C,QAAO,IAAI,SAAS,SAAS,WAAW;EACtC,IAAI,UAAU;EACd,MAAM,UAAU,KAAoB,WAAqB;AACvD,OAAI,QACF;AAEF,aAAU;AACV,OAAI,KAAK;AACP,WAAO,IAAI;AACX;;AAEF,OAAI,oBAAoB,OAAO,EAAE;AAC/B,YAAQ,OAAO;AACf;;AAEF,WAAQ,OAAU;;EAGpB,MAAM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Q,UAAU;AACrD,OAAI,CAAC,uBAAuB,kBAAkB,CAC5C;AAGF,YAAS,QAAQ,UAAU,iBAAiB,OAC1C,QAC2C;IAC3C,MAAM,WAAW,MAAM,oBAAoB,mBAAmB,IAAI;AAClE,QAAI,OAAO,aAAa,SACtB,QAAO;IAGT,MAAM,MAAM,SAAS,MAAM,IAAI,CAAC;AAChC,QAAI,CAAC,IACH;IASF,MAAM,mBALJ,QAAQ,UACR,CAAC,MAAM,QAAQ,QAAQ,OAAO,IAC9B,OAAO,QAAQ,WAAW,WACtB,OAAO,QAAQ,QAAQ,OAAO,GAC9B,EAAE,EAC8B,MACnC,CAAC,KAAK,uBAAuB;AAC5B,SAAI,CAAC,sBAAsB,kBAAkB,CAC3C,QAAO;AAET,YAAO,IAAI,SAAS,IAAI,GAAG,IAAI,SAAS,IAAI,GAAG,QAAQ;MAE1D;AAED,QACE,IAAI,YACH,IAAI,QAAQ,SAAS,+BAA+B,IACnD,mBACA,IAAI,QAAQ,SAAS,sBAAsB,EAE7C;AAGF,QACE,IAAI,WAAW,OAAO,IACtB,IAAI,WAAW,SAAS,IACxB,IAAI,WAAW,aAAa,IAC5B,QAAQ,WACR,QAAQ,sBACR,QAAQ,YAER,QAAO;;;;;;;;;;AAajB,SAAgB,+BAA+B,UAA0B;AAEvE,UAAS,QAAQ,OAAO;EACtB,GAAG,SAAS,QAAQ;EACpB,QAAQ;EACT;AAGD,UAAS,QAAQ,SAAS"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { NextFederationPluginOptions } from "./next-fragments.mjs";
|
|
2
|
+
import { Compiler } from "webpack";
|
|
3
|
+
|
|
4
|
+
//#region src/plugins/NextFederationPlugin/index.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* NextFederationPlugin is a webpack plugin that handles Next.js application federation using Module Federation.
|
|
7
|
+
*/
|
|
8
|
+
declare class NextFederationPlugin {
|
|
9
|
+
private _options;
|
|
10
|
+
private _extraOptions;
|
|
11
|
+
name: string;
|
|
12
|
+
/**
|
|
13
|
+
* Constructs the NextFederationPlugin with the provided options.
|
|
14
|
+
*
|
|
15
|
+
* @param options The options to configure the plugin.
|
|
16
|
+
*/
|
|
17
|
+
constructor(options: NextFederationPluginOptions);
|
|
18
|
+
/**
|
|
19
|
+
* The apply method is called by the webpack compiler and allows the plugin to hook into the webpack process.
|
|
20
|
+
* @param compiler The webpack compiler object.
|
|
21
|
+
*/
|
|
22
|
+
apply(compiler: Compiler): void;
|
|
23
|
+
private validateOptions;
|
|
24
|
+
private isServerCompiler;
|
|
25
|
+
private applyConditionalPlugins;
|
|
26
|
+
private getNormalFederationPluginOptions;
|
|
27
|
+
private getNoopPath;
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
export { NextFederationPlugin };
|
|
31
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -1,27 +1,31 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { NextFederationPluginOptions } from "./next-fragments.js";
|
|
2
|
+
import { Compiler } from "webpack";
|
|
3
|
+
|
|
4
|
+
//#region src/plugins/NextFederationPlugin/index.d.ts
|
|
3
5
|
/**
|
|
4
6
|
* NextFederationPlugin is a webpack plugin that handles Next.js application federation using Module Federation.
|
|
5
7
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
8
|
+
declare class NextFederationPlugin {
|
|
9
|
+
private _options;
|
|
10
|
+
private _extraOptions;
|
|
11
|
+
name: string;
|
|
12
|
+
/**
|
|
13
|
+
* Constructs the NextFederationPlugin with the provided options.
|
|
14
|
+
*
|
|
15
|
+
* @param options The options to configure the plugin.
|
|
16
|
+
*/
|
|
17
|
+
constructor(options: NextFederationPluginOptions);
|
|
18
|
+
/**
|
|
19
|
+
* The apply method is called by the webpack compiler and allows the plugin to hook into the webpack process.
|
|
20
|
+
* @param compiler The webpack compiler object.
|
|
21
|
+
*/
|
|
22
|
+
apply(compiler: Compiler): void;
|
|
23
|
+
private validateOptions;
|
|
24
|
+
private isServerCompiler;
|
|
25
|
+
private applyConditionalPlugins;
|
|
26
|
+
private getNormalFederationPluginOptions;
|
|
27
|
+
private getNoopPath;
|
|
26
28
|
}
|
|
27
|
-
|
|
29
|
+
//#endregion
|
|
30
|
+
export { NextFederationPlugin };
|
|
31
|
+
//# sourceMappingURL=index.d.ts.map
|