# webpack.config.js
```javascript
const path = require("path");
const { ModuleFederationPlugin } = require("../../").container;
const rules = [
{
test: /\.js$/,
include: path.resolve(__dirname, "src"),
use: {
loader: "babel-loader",
options: {
presets: ["@babel/react"]
}
}
}
];
const optimization = {
chunkIds: "named", // for this example only: readable filenames in production too
nodeEnv: "production" // for this example only: always production version of react
};
const stats = {
chunks: true,
modules: false,
chunkModules: true,
chunkOrigins: true
};
module.exports = (env = "development") => [
// For this example we have 3 configs in a single file
// In practice you probably would have separate config
// maybe even separate repos for each build.
// For Module Federation there is not compile-time dependency
// between the builds.
// Each one can have different config options.
{
name: "app",
mode: env,
entry: {
app: "./src/index.js"
},
output: {
filename: "[name].js",
path: path.resolve(__dirname, "dist/aaa"),
publicPath: "dist/aaa/",
// Each build needs a unique name
// to avoid runtime collisions
// The default uses "name" from package.json
uniqueName: "module-federation-aaa"
},
module: { rules },
optimization,
plugins: [
new ModuleFederationPlugin({
// List of remotes with URLs
remotes: {
"mfe-b": "mfeBBB@/dist/bbb/mfeBBB.js",
"mfe-c": "mfeCCC@/dist/ccc/mfeCCC.js"
},
// list of shared modules with optional options
shared: {
// specifying a module request as shared module
// will provide all used modules matching this name (version from package.json)
// and consume shared modules in the version specified in dependencies from package.json
// (or in dev/peer/optionalDependencies)
// So it use the highest available version of this package matching the version requirement
// from package.json, while providing it's own version to others.
react: {
singleton: true // make sure only a single react module is used
}
}
})
],
stats
},
{
name: "mfe-b",
mode: env,
entry: {},
output: {
filename: "[name].js",
path: path.resolve(__dirname, "dist/bbb"),
publicPath: "dist/bbb/",
uniqueName: "module-federation-bbb"
},
module: { rules },
optimization,
plugins: [
new ModuleFederationPlugin({
// A unique name
name: "mfeBBB",
// List of exposed modules
exposes: {
"./Component": "./src-b/Component"
},
// list of shared modules
shared: [
// date-fns is shared with the other remote, app doesn't know about that
"date-fns",
{
react: {
singleton: true // must be specified in each config
}
}
]
})
],
stats
},
{
name: "mfe-c",
mode: env,
entry: {},
output: {
filename: "[name].js",
path: path.resolve(__dirname, "dist/ccc"),
publicPath: "dist/ccc/",
uniqueName: "module-federation-ccc"
},
module: { rules },
optimization,
plugins: [
new ModuleFederationPlugin({
name: "mfeCCC",
exposes: {
"./Component": "./src-c/Component",
"./Component2": "./src-c/LazyComponent"
},
shared: [
// All (used) requests within lodash are shared.
"lodash/",
"date-fns",
{
react: {
// Do not load our own version.
// There must be a valid shared module available at runtime.
// This improves build time as this module doesn't need to be compiled,
// but it opts-out of possible fallbacks and runtime version upgrade.
import: false,
singleton: true
}
}
]
})
],
stats
}
];
```
# src/index.js
```javascript
// Sharing modules requires that all remotes are initialized
// and can provide shared modules to the common scope
// As this is an async operation we need an async boundary (import())
// Using modules from remotes is also an async operation
// as chunks need to be loaded for the code of the remote module
// This also requires an async boundary (import())
// At this point shared modules initialized and remote modules are loaded
import("./bootstrap");
// It's possible to place more code here to do stuff on page init
// but it can't use any of the shared modules or remote modules.
```
# src/bootstrap.js
```jsx
import ReactDom from "react-dom";
import React from "react"; // <- this is a shared module, but used as usual
import App from "./App";
// load app
const el = document.createElement("main");
ReactDom.render(<App />, el);
document.body.appendChild(el);
// remove spinner
document.body.removeChild(document.getElementsByClassName("spinner")[0]);
```
# src/App.js
```jsx
import React from "react";
import ComponentB from "mfe-b/Component"; // <- these are remote modules,
import ComponentC from "mfe-c/Component"; // <- but they are used as usual packages
import { de } from "date-fns/locale";
// remote modules can also be used with import() which lazy loads them as usual
const ComponentD = React.lazy(() => import("mfe-c/Component2"));
const App = () => (
<article>
<header>
<h1>Hello World</h1>
</header>
<p>This component is from a remote container:</p>
<ComponentB locale={de} />
<p>And this component is from another remote container:</p>
<ComponentC locale={de} />
<React.Suspense fallback={<p>Lazy loading component...</p>}>
<p>
And this component is from this remote container too, but lazy loaded:
</p>
<ComponentD />
</React.Suspense>
</article>
);
export default App;
```
# index.html
```html
<html>
<head>
<style>
.spinner {
font-size: 10px;
margin: 50px auto;
text-indent: -9999em;
width: 11em;
height: 11em;
border-radius: 50%;
background: #595959;
background: linear-gradient(
to right,
#595959 10%,
rgba(89, 89, 89, 0) 42%
);
position: relative;
animation: spin 1.4s infinite linear;
transform: translateZ(0);
}
.spinner:before {
width: 50%;
height: 50%;
background: #595959;
border-radius: 100% 0 0 0;
position: absolute;
top: 0;
left: 0;
content: "";
}
.spinner:after {
background: white;
width: 75%;
height: 75%;
border-radius: 50%;
content: "";
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
@-webkit-keyframes spin {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
@keyframes spin {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<!-- A spinner -->
<div class="spinner"></div>
<!-- This script only contains bootstrapping logic -->
<!-- It will load all other scripts if necessary -->
<script src="/dist/aaa/app.js" async></script>
<!-- These script tags are optional -->
<!-- They improve loading performance -->
<!-- Omitting them will add an additional round trip -->
<script src="/dist/bbb/mfeBBB.js" async></script>
<script src="/dist/ccc/mfeCCC.js" async></script>
<!-- All these scripts are pretty small ~5kb -->
<!-- For optimal performance they can be inlined -->
</body>
</html>
```
# src-b/Component.js
```jsx
import React from "react";
import { formatRelative, subDays } from "date-fns";
// date-fns is a shared module, but used as usual
// exposing modules act as async boundary,
// so no additional async boundary need to be added here
// As data-fns is an shared module, it will be placed in a separate file
// It will be loaded in parallel to the code of this module
const Component = ({ locale }) => (
<div style={{ border: "5px solid darkblue" }}>
<p>I'm a Component exposed from container B!</p>
<p>
Using date-fn in Remote:{" "}
{formatR
没有合适的资源?快使用搜索试试~ 我知道了~
webpack 静态模块打包工具.rar
共7435个文件
js:6517个
json:269个
md:147个
需积分: 5 0 下载量 31 浏览量
2023-07-11
22:49:59
上传
评论
收藏 6.59MB RAR 举报
温馨提示
1.1什么是webpack Webpack是一个现代化的静态模块打包工具,它可以将各种类型的文件,如JavaScript、CSS、HTML、图片等,转换为一个或多个静态资源文件,以便在浏览器中加载。Webpack的主要作用是处理模块依赖关系,将模块打包成静态资源文件,并提供一些高级功能,如代码分割、热模块替换、代码压缩等。Webpack是一个非常流行的前端工具,被广泛应用于各种类型的项目中。 在webpack的环境中,可以将CSS文件打包成JS文件,webpack主要就是分析文件的打包工具. 1.2webpack五个核心概念 Entry 入口 Output 出口/输出 Loader 加载器 Plugin 插件 Mode 模式 1.2.1Entry 入口 入口起点(entry point) 指示 webpack 应该使用哪个模块,来作为构建其内部依赖图的开始。进入入口起点后,webpack 会找出有哪些模块和库是入口起点依赖的 1.2.2Output 属性告诉 webpack 在哪里生成(输出)打包文件(bundles),以及如何命名打包文件
资源推荐
资源详情
资源评论
收起资源包目录
webpack 静态模块打包工具.rar (7435个子文件)
npm_p-map_5.1_9895e1a83d37d06ab277.0 2KB
npm_clean-stack_4.1_b2805ba009abd32b0160.0 1KB
npm_aggregate-error_4.0_50f751f77af91e405af4.0 1KB
npm_indent-string_5.0_39c50c3c56a92bbf73ba.0 884B
npm_escape-string-regexp_5.0_703470061c4748c30ba2.0 546B
npm_jspm_core_2.0.0-beta_1620e8f9e144fe702a06.11_nodelibs_os 3KB
npm_jspm_core_2.0.0-beta_12b8110471722e74fcb6.11_nodelibs_process 7KB
a 22B
a 0B
b 21B
bar 17B
.browserslistrc 40B
.browserslistrc 5B
loader.cjs 171B
loader.cjs 171B
loader.cjs 165B
example.coffee 190B
cup1.coffee 103B
script.coffee 85B
cup2.coffee 54B
module-only.coffee 6B
tailwind.min.css 1.88MB
tailwind.module.css 1.88MB
spacing.css 19KB
style.css 10KB
style.module.css 4KB
style.css 282B
style.module.css 200B
style10.css 174B
style.module.css 170B
stylesheet-import1.css 167B
style.module.css 149B
style.css 148B
style.css 147B
duplicate-nested.css 143B
style2.css 141B
all-deep-nested.css 133B
all-nested.css 122B
anonymous-nested.css 121B
style12.css 118B
style.css 107B
media-deep-nested.css 104B
supports-deep-nested.css 97B
b.css 95B
c.css 91B
media-nested.css 89B
supports-nested.css 86B
stylesheet.css 86B
mixed-nested.css 83B
mixed-deep-nested.css 80B
layer-deep-nested.css 80B
style-imported.css 79B
anonymous-deep-nested.css 79B
style.css 78B
lazy2.css 72B
lazy1.css 72B
layer-nested.css 70B
a.css 69B
nested.css 66B
style.css 57B
style2.css 57B
style.css 57B
style.css 57B
style2.css 57B
b.css 56B
lazy3.css 55B
chunk.css 53B
chunk.css 53B
c.css 49B
d.css 49B
a.css 49B
a.css 49B
test.module.css 48B
style3.css 47B
stylesheet-import3.css 46B
stylesheet-import2.css 42B
stylesheet-import2.css 42B
external1.css 38B
external2.css 38B
test test.css 38B
style.css 38B
external.css 37B
style6.css 35B
style9.css 35B
style8.css 35B
style5.css 35B
style4.css 35B
styl'le7.css 35B
layer.css 34B
test.css 33B
supports-deep-deep-nested.css 33B
mixed-deep-deep-nested.css 33B
anonymous-deep-deep-nested.css 33B
media-deep-deep-nested.css 33B
all-deep-deep-nested.css 33B
layer-deep-deep-nested.css 33B
test.css 33B
dependency.css 31B
style2-imported.css 30B
style2-imported.css 30B
共 7435 条
- 1
- 2
- 3
- 4
- 5
- 6
- 75
资源评论
野生的狒狒
- 粉丝: 3393
- 资源: 2436
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- vmware-VMnet8一键启动和停止脚本
- 可移植的 Python 数据框库.zip
- 包含 Andrei Neagoie 的《从零到精通掌握编码面试 - 数据结构 + 算法》课程的所有代码示例,使用 Python 语言 .zip
- 数据库课程设计(图书馆管理系统)springboot+swing+mysql+mybatis
- C++ Vigenère 密码(解密代码)
- zblog日收站群,zblog泛目录
- C++ Vigenère 密码(加密代码)
- Vue Router 是 Vue 生态系统的一部分,是一个 MIT 许可的开源项目,其持续开发完全在赞助商的支持下成为可能 支持 Vue 路由器
- PM2.5 数据集 包含上海、成都、广州、北京、沈阳五地的PM2.5观测,csv文件
- 电动汽车与软件定义汽车(SDV)时代的汽车行业数字化转型
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功