Create file ./src/index.js
with the following content:
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
var mountNode = document.getElementById("app");
ReactDOM.render(, mountNode);
Create file ./src/index.html
with the following content:
This is a naive implementation where we hard-code links to micro frontends' resources in a real project it would come from the configuration.
No we will configure build.
Create file ./.bablrc
with the following content:
{
presets: [
[
'@babel/preset-env',
{
modules: false
}
],
'@babel/preset-react'
],
plugins: [
'react-hot-loader/babel'
]
}
Create file ./webpack.config.js
with the following content:
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const config = {
entry: ["react-hot-loader/patch", "./src/index.js"],
output: {
path: path.resolve(__dirname, "dist"),
filename: "[name].js",
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
use: "babel-loader",
exclude: /node_modules/,
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
importLoaders: 1,
},
},
],
},
],
},
resolve: {
extensions: [".js"],
alias: {
"react-dom": "@hot-loader/react-dom",
},
},
devServer: {
contentBase: "./src",
watchContentBase: true,
port: 7000,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS"
},
},
plugins: [
new HtmlWebpackPlugin({
template: "./src/index.html",
}),
new MiniCssExtractPlugin(),
],
optimization: {
runtimeChunk: "single",
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: "vendors",
chunks: "all",
},
},
},
},
};
module.exports = (env, argv) => {
if (argv.hot) {
// Cannot use 'contenthash' when hot reloading is enabled.
config.output.filename = "[name].js";
}
return config;
};
Open ./package.json
and add following script to scripts
section:
"start": "cross-env NODE_ENV=development webpack serve --hot --mode development"
You should be able to build the shell app now. Run npm start
. It should bind to port 7000
. Remember to build and run micro frontends first.