如何为 create-react-app 创建多个条目和输出并将它们分开?

Mat*_*ran 6 javascript reactjs webpack electron create-react-app

我的用例是我正在使用电子并且希望有多个可以加载的窗口。最初将加载一个节点应用程序,但不会向用户显示。我只是用它来运行特定的任务。另一个窗口将是 UI 客户端,这将内置于 react。我正在使用 create-react-app。

我采取的步骤

  • 我已经弹射了。
  • 我创建了多个入口点。

基本上,我遵循了这里的讨论

我目前面临的问题是 webpack 将我的第二个捆绑文件插入到原始文件中 index.html

我希望两个束保持完全分开,因为次级束将由电子启动。

我需要在我的 webpack 配置中修改什么才能使包完全分开?

所做的修改

webpack.config.dev.js

入口:

entry: {
  app: [
    require.resolve('./polyfills'),
    require.resolve('react-dev-utils/webpackHotDevClient'),
    paths.appIndexJs,
  ],
  secondary: [
    require.resolve('./polyfills'),
    require.resolve('react-dev-utils/webpackHotDevClient'),
    paths.secondaryIndexJs,
  ]
},


output: {
  pathinfo: true,
  filename: 'static/js/[name].bundle.js',
  chunkFilename: 'static/js/[name].chunk.js',
  publicPath: publicPath,
  devtoolModuleFilenameTemplate: info =>
  path.resolve(info.absoluteResourcePath).replace(/\\/g, '/'),
},
Run Code Online (Sandbox Code Playgroud)

HtmlWebpackPlugin插件中的附加内容

new HtmlWebpackPlugin({
  inject: true,
  chunks: ["secondary"],
  template: paths.secondaryHtml,
  filename: 'secondary.html',
}),
Run Code Online (Sandbox Code Playgroud)

路径.js

  • 相应修改

Mat*_*ran 1

解决办法就是简单地添加 excludeChunks: ["secondary"],到原来的HtmlWebpackPlugin属性上。

总的来说:

 new HtmlWebpackPlugin({
  inject: true,
  template: paths.appHtml,
  excludeChunks: ["secondary"]
}),
new HtmlWebpackPlugin({
  inject: true,
  chunks: ["secondary"],
  template: paths.secondaryHtml,
  filename: 'secondary.html',
}),
Run Code Online (Sandbox Code Playgroud)