@@ -33,15 +33,7 @@ class AutoPublicPathRuntimeModule extends RuntimeModule {
33
33
compilation ?. getPath ( publicPath || '' , {
34
34
hash : compilation ?. hash || 'XXXX' ,
35
35
} ) ;
36
- // If publicPath is not "auto", return the static value
37
- // if (publicPath !== 'auto') {
38
- // const path = getPath();
39
- // return Template.asString([
40
- // `${RuntimeGlobals.publicPath} = ${JSON.stringify(path)};`,
41
- // 'var addProtocol = (url)=> url.startsWith(\'//\') ? \'https:\' + url : url;',
42
- // `globalThis.currentVmokPublicPath = addProtocol(${RuntimeGlobals.publicPath}) || '/';`,
43
- // ]);
44
- // }
36
+
45
37
const chunkName = compilation ?. getPath (
46
38
javascript . JavascriptModulesPlugin . getChunkFilenameTemplate (
47
39
this . chunk ,
@@ -52,85 +44,116 @@ class AutoPublicPathRuntimeModule extends RuntimeModule {
52
44
contentHashType : 'javascript' ,
53
45
} ,
54
46
) ;
55
- const undoPath = getUndoPath ( chunkName , path , false ) ;
56
- const ident = Template . toIdentifier ( uniqueName || '' ) ;
57
-
58
- // Define potential lookup keys
59
- const potentialLookups = [ this . chunk ?. name , ident , uniqueName ] ;
60
-
61
- // Generate lookup string using potential keys
62
- const lookupString = potentialLookups
63
- . filter ( Boolean )
64
- . map ( ( lookup ) => {
65
- return `remoteReg[${ JSON . stringify ( lookup ) } ]` ;
66
- } )
67
- . join ( ' || ' ) ;
68
-
69
- return Template . asString ( [
70
- 'var scriptUrl;' ,
71
- // its an esproxy so nesting into _config directly is not possible
72
- `
73
- let remoteContainerRegistry = {
74
- get url() {
75
- var remoteReg = globalThis.__remote_scope__ ? globalThis.__remote_scope__._config : {};
76
- return ${ lookupString }
47
+
48
+ let undoPath : string | null = null ;
49
+ if ( chunkName && path ) {
50
+ undoPath = getUndoPath ( chunkName , path , false ) ;
51
+ }
52
+
53
+ const getPathFromFederation = `
54
+ function getPathFromFederation() {
55
+ // Access the global federation manager or create a fallback object
56
+ var federationManager = globalThis.__FEDERATION__ || {};
57
+ // Access the current Webpack instance's federation details or create a fallback object
58
+ var instance = __webpack_require__.federation.instance || {};
59
+
60
+ // Function to aggregate all known remote module paths
61
+ var getAllKnownRemotes = function() {
62
+ var found = {};
63
+ // Iterate over all federation instances to collect module cache entries
64
+ (federationManager.__INSTANCES__ || []).forEach((instance) => {
65
+ instance.moduleCache.forEach((value, key) => {
66
+ found[key] = value;
67
+ });
68
+ });
69
+ return found;
70
+ };
71
+
72
+ // Retrieve the combined remote cache from all federation instances
73
+ const combinedRemoteCache = getAllKnownRemotes();
74
+ // Get the name of the current host from the instance
75
+ const hostName = instance.name;
76
+ // Find the path for the current host in the remote cache
77
+ const foundPath = combinedRemoteCache[hostName];
78
+ // If a path is not found, return undefined to indicate the absence of an entry path
79
+ if (!foundPath) { return undefined; }
80
+ // Return the entry path for the found remote module
81
+ const entryPath = foundPath.remoteInfo.entry;
82
+ return entryPath;
83
+ }
84
+ ` ;
85
+ const definePropertyCode = `
86
+ Object.defineProperty(__webpack_require__, "p", {
87
+ get: function() {
88
+ var scriptUrl;
89
+
90
+ // Attempt to get the script URL based on the environment
91
+ var scriptType = ${ JSON . stringify ( scriptType ) } ;
92
+ var chunkLoading = ${ JSON . stringify ( chunkLoading ) } ;
93
+ var isModuleEnvironment = ['module', 'node', 'async-node', 'require'].includes(scriptType) || chunkLoading;
94
+
95
+ if (isModuleEnvironment) {
96
+ try {
97
+ // Use Function constructor to avoid direct reference to import.meta in environments that do not support it
98
+ scriptUrl = (new Function('return typeof ${ importMetaName } .url === "string" ? ${ importMetaName } .url : undefined;'))();
99
+ } catch (e) {
100
+ // Handle cases where import.meta is not available or other errors occur
101
+ var scriptPath = getPathFromFederation();
102
+ if (scriptPath) {
103
+ scriptUrl = scriptPath;
104
+ } else if (typeof __filename !== "undefined") {
105
+ scriptUrl = __filename;
106
+ } else {
107
+ scriptUrl = ${
108
+ publicPath !== 'auto' ? JSON . stringify ( getPath ( ) ) : 'undefined'
109
+ } ;
77
110
}
78
- };
79
- ` ,
80
-
81
- [ 'module' , 'node' , 'async-node' , 'require' ] . includes ( scriptType || '' ) ||
82
- chunkLoading
83
- ? Template . asString ( [
84
- 'try {' ,
85
- Template . indent ( [
86
- `scriptUrl = new Function('return typeof ${ importMetaName } .url === "string" ? ${ importMetaName } .url : undefined;')();` ,
87
- ] ) ,
88
- '} catch (e) {' ,
89
- Template . indent ( [
90
- 'if (typeof remoteContainerRegistry.url === "string") {' ,
91
- Template . indent ( 'scriptUrl = remoteContainerRegistry.url;' ) ,
92
- '} else if(typeof __filename !== "undefined") {' ,
93
- Template . indent ( 'scriptUrl = __filename;' ) ,
94
- '} else {' ,
95
- Template . indent ( [
96
- `scriptUrl = ${
97
- publicPath !== 'auto'
98
- ? JSON . stringify ( getPath ( ) )
99
- : 'undefined'
100
- } `,
101
- ] ) ,
102
- '}' ,
103
- ] ) ,
104
- '}' ,
105
- ] )
106
- : Template . asString ( [
107
- `if (${ RuntimeGlobals . global } .importScripts) scriptUrl = ${ RuntimeGlobals . global } .location + "";` ,
108
- `var document = ${ RuntimeGlobals . global } .document;` ,
109
- 'if (!scriptUrl && document) {' ,
110
- Template . indent ( [
111
- 'if (document.currentScript)' ,
112
- Template . indent ( 'scriptUrl = document.currentScript.src' ) ,
113
- 'if (!scriptUrl) {' ,
114
- Template . indent ( [
115
- 'var scripts = document.getElementsByTagName("script");' ,
116
- 'if(scripts.length) scriptUrl = scripts[scripts.length - 1].src' ,
117
- ] ) ,
118
- '}' ,
119
- ] ) ,
120
- '}' ,
121
- ] ) ,
122
- '// When supporting server environments where an automatic publicPath is not supported, you must specify an output.publicPath manually via configuration' ,
123
- '// or pass an empty string ("") and set the __webpack_public_path__ variable from your code to use your own logic.' ,
124
- 'if (!scriptUrl) throw new Error("Unable to calculate automatic public path");' ,
125
- 'scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\\?.*$/, "").replace(/\\/[^\\/]+$/, "/");' ,
126
- ! undoPath
127
- ? `${ RuntimeGlobals . publicPath } = scriptUrl;`
128
- : `${ RuntimeGlobals . publicPath } = scriptUrl + ${ JSON . stringify (
129
- undoPath ,
130
- ) } ;`,
131
- "var addProtocol = (url)=> url.startsWith('//') ? 'https:' + url : url;" ,
132
- `globalThis.currentVmokPublicPath = addProtocol(${ RuntimeGlobals . publicPath } ) || '/';` ,
133
- ] ) ;
111
+ }
112
+ } else {
113
+ // Fallback for non-module environments, such as browsers
114
+ if (${ RuntimeGlobals . global } .importScripts) {
115
+ scriptUrl = ${ RuntimeGlobals . global } .location + "";
116
+ }
117
+ var document = ${ RuntimeGlobals . global } .document;
118
+ if (!scriptUrl && document) {
119
+ if (document.currentScript) {
120
+ scriptUrl = document.currentScript.src;
121
+ } else {
122
+ var scripts = document.getElementsByTagName("script");
123
+ if (scripts.length) {
124
+ scriptUrl = scripts[scripts.length - 1].src;
125
+ }
126
+ }
127
+ }
128
+ }
129
+
130
+ if (!scriptUrl) {
131
+ throw new Error("Unable to calculate automatic public path");
132
+ }
133
+
134
+ // Clean up the script URL by removing any hash or query parameters
135
+ scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\\?.*$/, "").replace(/\\/[^\\/]+$/, "/");
136
+
137
+ // Apply any undo path that might be necessary for nested public paths
138
+ var finalScript = ${ JSON . stringify (
139
+ undoPath ,
140
+ ) } ? scriptUrl + ${ JSON . stringify ( undoPath ) } : scriptUrl;
141
+
142
+ // Helper function to ensure the URL has a protocol if it starts with '//'
143
+ var addProtocol = function(url) {
144
+ return url.startsWith('//') ? 'https:' + url : url;
145
+ };
146
+
147
+ // Set the global variable for the public path
148
+ globalThis.currentVmokPublicPath = addProtocol(finalScript) || '/';
149
+
150
+ // Return the final public path
151
+ return finalScript
152
+ }
153
+ });
154
+ ` ;
155
+
156
+ return Template . asString ( [ getPathFromFederation , definePropertyCode ] ) ;
134
157
}
135
158
}
136
159
0 commit comments