I have two apps.
- Container.
Webpack
JavaScript
x
45
45
1
const baseConfig = {
2
mode: 'development',
3
resolve: {
4
extensions: ['.ts', '.tsx', '.js', '.jsx'],
5
},
6
entry: {
7
main: './src/index.tsx',
8
},
9
module: {
10
rules: [
11
{
12
test: /.tsx?$/,
13
use: 'ts-loader',
14
exclude: '/node_modules/',
15
}
16
],
17
},
18
19
devServer: { hot: false, contentBase: path.join(__dirname, 'dist'), port: 3100 },
20
output: {
21
filename: '[name].js',
22
path: path.resolve(__dirname, 'dist'),
23
chunkFilename: '[id].[contenthash].js',
24
},
25
plugins: [
26
new ModuleFederationPlugin({
27
name: 'Shell',
28
library: { type: 'var', name: 'shell' },
29
remotes: {
30
usersweb: 'usersweb',
31
},
32
shared: {
33
deps,
34
react: { singleton: true, eager: true, requiredVersion: deps.react },
35
'react-dom': { singleton: true, eager: true, requiredVersion: deps['react-dom'] },
36
},
37
}),
38
39
new HtmlWebpackPlugin({
40
template: './public/index.html',
41
}),
42
],
43
44
}
45
App.tsx
JavaScript
1
12
12
1
import React from 'react'
2
3
const Portal = React.lazy(() => import('usersweb/Portal'))
4
5
export default function Shell() {
6
return (
7
<React.Suspense fallback={'Loading'}>
8
<Portal />
9
</React.Suspense>
10
)
11
}
12
2nd App. Webpack
JavaScript
1
39
39
1
const baseConfig = {
2
mode: 'development',
3
resolve: {
4
extensions: ['.ts', '.tsx', '.js', '.jsx'],
5
},
6
module: {
7
rules: [
8
{
9
test: /.tsx?$/,
10
use: 'ts-loader',
11
exclude: '/node_modules/',
12
}
13
],
14
},
15
entry: {
16
main: './src/index.tsx',
17
},
18
output: {
19
chunkFilename: '[id].[contenthash].js',
20
path: path.resolve(__dirname, 'dist'),
21
},
22
devServer: { contentBase: path.join(__dirname, 'dist'), port: 3101 },
23
24
plugins: [
25
new ModuleFederationPlugin({
26
filename: 'remoteEntry.js',
27
name: 'usersweb',
28
exposes: {
29
'./Portal': './src/portal',
30
},
31
shared: {
32
deps,
33
react: { singleton: true, eager: true, requiredVersion: deps.react },
34
'react-dom': { singleton: true, eager: true, requiredVersion: deps['react-dom'] },
35
},
36
}),
37
],
38
}
39
./src/portal.tsx
JavaScript
1
8
1
import React from 'react'
2
3
const Portal: React.FC = () => {
4
return <div>Hello from userweb</div>
5
}
6
7
export default Portal
8
It should work smooth.
But it is failing
because inside of webpack_modules exists
webpack/container/reference/usersweb
key, but when React.Lazy requiring remote model it is requiring
webpack/container/remote/usersweb/Portal
which is not added and app crashes.
remoteEntry.js
loaded properly and I may see userweb object in the console.
dependencies versions
“react”: “^17.0.2”, “webpack”: “5.21.2”, “typescript”: “^4.1.2”,
Advertisement
Answer
After 5 days debugging issue was in tsconfig.json
In order to proper load modules with ‘ts-loader’ need to add to compilerOptions
JavaScript
1
2
1
"module": "esnext",
2