webpack配置app和pc多入口

需求:原先做的app端又增加了pc端,现在把他们放在同一个项目下,就需要我们配置多入口了。

  • 原先页面为index.html,后面增加pcJs.html

更改路径

在paths.js里

1
2
3
4
{
// 增加pc的html
pcIndexJs: resolveModule(resolveApp, 'src/pc/index'),
}

增加entry

在webpack.config.js下

1
2
3
4
5
6
7
8
9
10
11
12
entry: {
index: [
isEnvDevelopment &&
require.resolve("react-dev-utils/webpackHotDevClient"),
paths.appIndexJs
].filter(Boolean),
pcJs: [
isEnvDevelopment &&
require.resolve("react-dev-utils/webpackHotDevClient"),
paths.pcIndexJs
].filter(Boolean)
},

动态生成文件名

在webpack.config.js下

1
2
3
4
5
6
7
8
9
10
11
output: {
filename: isEnvProduction
? "static/js/[name].[contenthash:8].js"
: isEnvDevelopment && "static/js/[name].bundle.js",
// TODO: remove this when upgrading to webpack 5
futureEmitAssets: true,
// There are also additional JS chunk files if you use code splitting.
chunkFilename: isEnvProduction
? "english/static/js/[name].[contenthash:8].chunk.js"
: isEnvDevelopment && "static/js/[name].chunk.js",
}

动态生成对应HTML

在webpack.config.js下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
plugins: [
new HtmlWebpackPlugin(
Object.assign(
{},
{
inject: true,
template: paths.appHtml,
chunks: ["index"],
inject: true,
filename: "index.html"
},
isEnvProduction
? {
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}
}
: undefined
)
),
new HtmlWebpackPlugin(
Object.assign(
{},
{
inject: true,
template: paths.appHtml,
chunks: ["pcJs"],
inject: true,
filename: "pcJs.html"
},
isEnvProduction
? {
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}
}
: undefined
)
),
]

路由重指向

在webpackDevServer.config.js下

1
2
3
4
5
6
7
historyApiFallback: {
// Paths with dots should still use the history fallback.
// See https://github.com/facebook/create-react-app/issues/387.
disableDotRule: true,
// pc开头全部指向pcJs.html
rewrites: [{ from: /^\/pc/, to: "/pcJs.html" }]
},

重定向时to的后面,不需要加build!