Skip to content

Commit ce3dfe6

Browse files
authored
Merge pull request #197 from pagesource/feature/bug-fixing
added initial setup of module federation for SSG and SSR arch type apps
2 parents 0ab9bb6 + d466a75 commit ce3dfe6

File tree

5 files changed

+109
-9
lines changed

5 files changed

+109
-9
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-universal-react': minor
3+
---
4+
5+
added initial setup of module federation for SSG and SSR arch type apps

templates/base/next.config.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ const withBundleAnalyzer = require('@next/bundle-analyzer')({
33
});
44

55
const withTM = require('next-transpile-modules')(['services']);
6+
const PACKAGE_JSON = require('./package.json');
67

78
const isAssetPrefix = process.env.BASE_PATH || '';
9+
const APP_NAME = PACKAGE_JSON.name.toLowerCase();
810

911
module.exports = withBundleAnalyzer(
1012
withTM({
@@ -19,8 +21,35 @@ module.exports = withBundleAnalyzer(
1921

2022
config.plugins.push(
2123
new ModuleFederationPlugin({
24+
name: `${APP_NAME}`,
25+
remoteType: 'var',
26+
// remotes: this is where we will include items to consume from remote
2227
remotes: {},
23-
shared: []
28+
// exposes: this is where we will include items to expose
29+
exposes: {},
30+
// shared: here we can put the list of modules we would like to share
31+
shared: [
32+
{
33+
react: {
34+
eager: true,
35+
singleton: true,
36+
requiredVersion: false
37+
}
38+
},
39+
{
40+
'react-dom': {
41+
eager: true,
42+
singleton: true,
43+
requiredVersion: false
44+
}
45+
},
46+
{
47+
'styled-components': {
48+
eager: true,
49+
singleton: true
50+
}
51+
}
52+
]
2453
})
2554
);
2655

templates/base/src/__tests__/_error.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import '@testing-library/jest-dom/extend-expect';
44
import Error from '../pages/_error';
55

66
test('ErrorPage ', () => {
7-
const { getByText, rerender } = render(<Error statusCode={'500'} />);
7+
const { getByText, rerender } = render(<Error statusCode="500" />);
88
expect(getByText('An error 500 occurred on server')).toBeInTheDocument();
99
// Change props
10-
rerender(<Error statusCode={''} />);
10+
rerender(<Error statusCode="" />);
1111
expect(getByText('An error occurred on client')).toBeInTheDocument();
1212
});

templates/microApp/webpack.config.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,35 @@ const APP_NAME = PACKAGE_JSON.name.toLowerCase();
2424
*/
2525
const MF_CONFIG = {
2626
name: `${APP_NAME}_remote`,
27+
// remotes: this is where we will include items to consume from remote
2728
remotes: {},
29+
// exposes: this is where we will include items to expose
2830
exposes: {
2931
'./app': './src/index'
30-
}
32+
},
33+
// shared: here we can put the list of modules we would like to share
34+
shared: [
35+
{
36+
react: {
37+
eager: true,
38+
singleton: true,
39+
requiredVersion: false
40+
}
41+
},
42+
{
43+
'react-dom': {
44+
eager: true,
45+
singleton: true,
46+
requiredVersion: false
47+
}
48+
},
49+
{
50+
'styled-components': {
51+
eager: true,
52+
singleton: true
53+
}
54+
}
55+
]
3156
};
3257

3358
/**
@@ -81,7 +106,8 @@ module.exports = (env, argv) => {
81106
return instance;
82107
};
83108

84-
const addHTMLReplace = () => new HtmlReplaceWebpackPlugin([
109+
const addHTMLReplace = () =>
110+
new HtmlReplaceWebpackPlugin([
85111
{
86112
pattern: '@@app_name',
87113
replacement: APP_NAME

templates/ssg/next.config.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ const withBundleAnalyzer = require('@next/bundle-analyzer')({
66
});
77

88
const withTM = require('next-transpile-modules')(['services']);
9+
const PACKAGE_JSON = require('./package.json');
910

1011
const isAssetPrefix = process.env.BASE_PATH || '';
12+
const APP_NAME = PACKAGE_JSON.name.toLowerCase();
1113

1214
module.exports = withBundleAnalyzer(
1315
withTM({
@@ -18,16 +20,54 @@ module.exports = withBundleAnalyzer(
1820
},
1921
images: {
2022
loader: 'akamai',
21-
path: '',
23+
path: ''
2224
},
2325
// mandatory config for SSG with next export command
2426
exportPathMap: async function (
25-
defaultPathMap,
26-
{ dev, dir, outDir, distDir, buildId }
27+
defaultPathMap,
28+
{ dev, dir, outDir, distDir, buildId }
2729
) {
2830
return {
2931
'/': { page: '/' }
30-
}
32+
};
33+
},
34+
webpack5: true,
35+
webpack: (config, options) => {
36+
config.plugins.push(
37+
new options.webpack.container.ModuleFederationPlugin({
38+
name: `${APP_NAME}`,
39+
remoteType: 'var',
40+
// remotes: this is where we will include items to consume from remote
41+
remotes: {},
42+
// exposes: this is where we will include items to expose
43+
exposes: {},
44+
// shared: here we can put the list of modules we would like to share
45+
shared: [
46+
{
47+
react: {
48+
eager: true,
49+
singleton: true,
50+
requiredVersion: false
51+
}
52+
},
53+
{
54+
'react-dom': {
55+
eager: true,
56+
singleton: true,
57+
requiredVersion: false
58+
}
59+
},
60+
{
61+
'styled-components': {
62+
eager: true,
63+
singleton: true
64+
}
65+
}
66+
]
67+
})
68+
);
69+
70+
return config;
3171
}
3272
})
3373
);

0 commit comments

Comments
 (0)