refactor: use importmap for micro-frontend apps

This commit is contained in:
Ayo 2024-01-21 22:44:23 +01:00
parent cf1b43e6a2
commit 2e0dbaa1a0
4 changed files with 22 additions and 14 deletions

View file

@ -7,14 +7,21 @@ export default defineConfig({
react(), react(),
solidJs(), solidJs(),
{ {
name: 'importmap-externals', name: "importmap-externals",
hooks: { hooks: {
'astro:build:setup': ({ vite, target }) => { "astro:build:setup": ({ vite, target }) => {
if(target === 'client') { if (target === "client") {
vite.build.rollupOptions["external"] = ["react", "react-dom", "solid-js"]; vite.build.rollupOptions["external"] = [
"react",
"react-dom",
"solid-js",
"app-cart",
"app-heading",
"app-products",
];
} }
} },
} },
}, },
], ],
}); });

View file

@ -1,15 +1,13 @@
import MicroFrontendA from "http://localhost:7100/bundle.js"; import Cart from "app-cart";
import MicroFrontendB from "http://localhost:7200/bundle.js"; import Heading from "app-heading";
const ReactComponent = (props) => { const ReactComponent = (props) => {
const { title } = props; const { title } = props;
console.log(">>> title", title);
return ( return (
<> <>
<MicroFrontendB title={title} /> <Heading title={title} />
<MicroFrontendA /> <Cart />
</> </>
); );
}; };

View file

@ -1,9 +1,9 @@
import MicroFrontendC from "http://localhost:7300/bundle.js"; import Products from "app-products";
function App() { function App() {
return ( return (
<> <>
<MicroFrontendC /> <Products />
</> </>
); );
} }

View file

@ -13,6 +13,9 @@ const appName = 'Scalable Shoe Shop'
<script type="importmap"> <script type="importmap">
{ {
"imports": { "imports": {
"app-cart": "http://localhost:7100/bundle.js",
"app-heading": "http://localhost:7200/bundle.js",
"app-products": "http://localhost:7300/bundle.js",
"react": "https://esm.sh/stable/react@18.2.0/es2022/react.mjs", "react": "https://esm.sh/stable/react@18.2.0/es2022/react.mjs",
"react-dom": "https://esm.sh/v135/react-dom@18.2.0/es2022/react-dom.mjs", "react-dom": "https://esm.sh/v135/react-dom@18.2.0/es2022/react-dom.mjs",
"solid-js": "https://esm.sh/stable/solid-js@1.8.11/es2022/solid-js.mjs" "solid-js": "https://esm.sh/stable/solid-js@1.8.11/es2022/solid-js.mjs"