Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions apps/website-new/docs/en/guide/troubleshooting/other.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,58 @@ export default function MFLinkPlugin(): ModuleFederationRuntimePlugin {
};
}
```

## Problem: esm `remoteEntry.js` usage in var `host` environment

### Problem

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.
And your `host` application doesn't support esm `remote` loading (e.g. your `host` doesn't use `promise new Promise` workaround).

In this case you are not able to load `remote` esm application without changing the `host` application.

#### Error Message
:::danger Browser Error Message
Uncaught SyntaxError: Cannot use import statement outside a module
:::

### Solution

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.

In you `remote` (`vite`) app create a new workaround file `public/varRemoteEntry.js`:

```js
// public/varRemoteEntry.js
var remote; // name of remote app
remote = (function () {
function getScriptUrl() {
return document.currentScript.src
}

const entry = `${getScriptUrl()}/../remoteEntry.js` // path to esm remoteEntry.js

return {
get: (...args) => import(entry).then(m => m.get(...args)),
init: (...args) => import(entry).then(m => m.init(...args)),
};
})();
```

Update `remotes` config in your `host` (`webpack`) app:

```diff
new ModuleFederationPlugin({
// ... webpack module federation config
remotes: {
- remote: 'remote@http://localhost:3001/remoteEntry.js',
+ remote: 'remote@http://localhost:3001/varRemoteEntry.js',
}
})
```

Now you are able to import modules from `remote` (esm) in your `host` (var) application:

```tsx
const App = await import('remote/App')
```