Skip to content

Commit ee91385

Browse files
committed
docs: var host + esm remote (vite) workaround
1 parent 8ff85f2 commit ee91385

File tree

1 file changed

+55
-0
lines changed
  • apps/website-new/docs/en/guide/troubleshooting

1 file changed

+55
-0
lines changed

apps/website-new/docs/en/guide/troubleshooting/other.mdx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,58 @@ export default function MFLinkPlugin(): ModuleFederationRuntimePlugin {
120120
};
121121
}
122122
```
123+
124+
## Problem: esm `remoteEntry.js` usage in var `host` environment
125+
126+
### Problem
127+
128+
There are some cases when your `host` application is written with [ModuleFederationPlugin](https://webpack.js.org/plugins/module-federation-plugin/) and you want to use esm (e.g. `vite`) in your `remote` application.
129+
And your `host` application doesn't support esm `remote` loading (e.g. your `host` doesn't use `promise new Promise` workaround).
130+
131+
In this case you are not able to load `remote` esm application without changing the `host` application.
132+
133+
#### Error Message
134+
:::danger Browser Error Message
135+
Uncaught SyntaxError: Cannot use import statement outside a module
136+
:::
137+
138+
### Solution
139+
140+
If you are not able (or don't want) to rewrite your `host` var environment to support esm `remote` import, you can write a little "var" layer on the `remote`'s side.
141+
142+
In you `remote` (`vite`) app create a new workaround file `public/varRemoteEntry.js`:
143+
144+
```js
145+
// public/varRemoteEntry.js
146+
var remote; // name of remote app
147+
viteRemote = (function () {
148+
function getScriptUrl() {
149+
return document.currentScript.src
150+
}
151+
152+
const entry = `${getScriptUrl()}/../remoteEntry.js` // path to esm remoteEntry.js
153+
154+
return {
155+
get: (...args) => import(entry).then(m => m.get(...args)),
156+
init: (...args) => import(entry).then(m => m.init(...args)),
157+
};
158+
})();
159+
```
160+
161+
Update `remotes` config in your `host` (`webpack`) app:
162+
163+
```diff
164+
new ModuleFederationPlugin({
165+
// ... webpack module federation config
166+
remotes: {
167+
- remote: 'remote@http://localhost:3001/remoteEntry.js',
168+
+ remote: 'remote@http://localhost:3001/varRemoteEntry.js',
169+
}
170+
})
171+
```
172+
173+
Now you are able to import modules from `remote` (esm) in your `host` (var) application:
174+
175+
```tsx
176+
const App = await import('remote/App')
177+
```

0 commit comments

Comments
 (0)