Create-react-app如何实现多个入口文件?

#1

RT。现在create-react-app一般做的都是单页应用,现在有个需求,js有多个入口文件,那么如何改造下,变成多页应用?

相关资料:
Add more entry points

1 Like
#2

通过index来派发应该更好吧。我看了相关资料里的用例,不是很强啊。

#3

对,目前我是在index.js 判断url,然后渲染不同的组件到root元素实现的。

#5

这个问题,我也遇到过,在一个工程里面做客户和后台管理两个SPA,刚刚解决了:

  1. 主要是修改wepack的entry结点,增加多个webpack的入口:
  entry: {
    index: [
      require.resolve('react-dev-utils/webpackHotDevClient'),
      require.resolve('./polyfills'),
      require.resolve('react-error-overlay'),
      paths.appIndexJs,
    ],
    admin:[
      require.resolve('react-dev-utils/webpackHotDevClient'),
      require.resolve('./polyfills'),
      require.resolve('react-error-overlay'),
      paths.appSrc + "/admin.js",
      ]
  },
  output: {
    path: paths.appBuild,
    pathinfo: true,
    filename: 'static/js/[name].bundle.js',
    chunkFilename: 'static/js/[name].chunk.js',
    publicPath: publicPath,
    devtoolModuleFilenameTemplate: info =>
      path.resolve(info.absoluteResourcePath),
  },

但同时也要配合:
2. HtmlWebpackPlugin配合修改
3. webpack Dev Server也要修改重定向。

具体可参考:http://imshuai.com/create-react-app-multiple-entry-points/

#6

好的,谢谢。后续有需求可以参考你的

#7

如何访问后台管理那个页面呢

#8

使用组件配置Create-React-App多入口:https://www.npmjs.com/package/react-app-rewire-multiple-entry