webpack动态创建入口

本文介绍如何使用webpack在一个项目中同时开发多个独立的单页应用(SPA),通过自动化配置减少重复工作,实现不同SPA项目的高效管理和部署。

转载自 webpack动态创建入口

#背景背景

create-react-app创建的应用默认是SPA的架子入口只有index.html。但是有些情况下我们确实需要在同一个工程下开发多个SPA项目,一个是2C的H5项目,一个是后台的管理项目。网上多页面的配置已经很多了,这里只是想扩展记录一些方法。

#实践实践

项目的架子是下面这样的。

按照网上大多数的配置如下。

  // 入口文件的配置
  entry: {
    anchorH5: [
      require.resolve('./polyfills'),
      require.resolve('react-dev-utils/webpackHotDevClient'),
      paths.appSrc + "/views/anchorH5/index.js",
    ],
    anchorWeb: [
      require.resolve('./polyfills'),
      require.resolve('react-dev-utils/webpackHotDevClient'),
      paths.appSrc + "/views/anchorWeb/index.js",
    ],
    orderManageH5: [
      require.resolve('./polyfills'),
      require.resolve('react-dev-utils/webpackHotDevClient'),
      paths.appSrc + "/views/orderManageH5/index.js",
    ]
  }


  // 插件文件新增
  new HtmlWebpackPlugin({
    inject: true,
    chunks: ["anchorH5"],
    template: paths.appHtml,
    filename: 'anchorH5.html',
    favicon: paths.appPublicFavicon,
    minify: {
      removeComments: true,
      collapseWhitespace: true,
      removeRedundantAttributes: true,
      useShortDoctype: true,
      removeEmptyAttributes: true,
      removeStyleLinkTypeAttributes: true,
      keepClosingSlash: true,
      minifyJS: true,
      minifyCSS: true,
      minifyURLs: true,
    },
  })
  new HtmlWebpackPlugin({
    inject: true,
    chunks: ["anchorWeb"],
    template: paths.appHtml,
    filename: 'anchorWeb.html',
    favicon: paths.appPublicFavicon,
    minify: {
      removeComments: true,
      collapseWhitespace: true,
      removeRedundantAttributes: true,
      useShortDoctype: true,
      removeEmptyAttributes: true,
      removeStyleLinkTypeAttributes: true,
      keepClosingSlash: true,
      minifyJS: true,
      minifyCSS: true,
      minifyURLs: true,
    },
  })
  new HtmlWebpackPlugin({
    inject: true,
    chunks: ["orderManageH5"],
    template: paths.appHtml,
    filename: 'orderManageH5.html',
    favicon: paths.appPublicFavicon,
    minify: {
      removeComments: true,
      collapseWhitespace: true,
      removeRedundantAttributes: true,
      useShortDoctype: true,
      removeEmptyAttributes: true,
      removeStyleLinkTypeAttributes: true,
      keepClosingSlash: true,
      minifyJS: true,
      minifyCSS: true,
      minifyURLs: true,
    },
  })
复制代码

大量的重复性代码看着就难受,所以我们就需要将这些提取简化成方法。

  const glob = require('gob')
  function getEntry() {
    let entry = {};
    let srcDirName = './src/views/*/index.js'; //入口文件夹路径
    glob.sync(srcDirName).forEach(function (name) {
      let n = name.slice(0, name.length - 9); //去掉/index.js
      n = n.slice(n.lastIndexOf('/')).split("/")[1]; //获取文件夹目录名
      entry[n] = [
          require.resolve('./polyfills'),
          require.resolve('react-dev-utils/webpackHotDevClient'),
          paths.appSrc + "/views/"+ n +"/index.js",
      ];
    });
    return entry;
  }


  function getPlugins() {
    let = [...otherPlugin]
    let srcDirName = './src/views/*/index.js'; 
    glob.sync(srcDirName).forEach(function (name) {
      let n = name.slice(0, name.length - 9); 
      n = n.slice(n.lastIndexOf('/')).split("/")[1];
      plugins.push(new HtmlWebpackPlugin({
          inject: true,
          chunks: [n],
          template: paths.appHtml,
          filename: n + '.html',
          favicon: paths.appPublicFavicon,
          minify: {
              removeComments: true,
              collapseWhitespace: true,
              removeRedundantAttributes: true,
              useShortDoctype: true,
              removeEmptyAttributes: true,
              removeStyleLinkTypeAttributes: true,
              keepClosingSlash: true,
              minifyJS: true,
              minifyCSS: true,
              minifyURLs: true,
          },
      }))
    });
  }


  module.export = {
    entry: getEntry(),
    ...
    plugins: getPlugins(),
  }
复制代码

这样后面维护的人只需要按照这个规则建新的目录就好了,而不用每次都去修改webpack的配置了。 但是这样本地启动服务的时候还有点问题,因为我们还没有修改webpackDevServer的配置。

  // 指明哪些路径映射到哪个html
  function getRewrites (){
    let list = []
    let srcDirName = './src/views/*/index.js'; 
    glob.sync(srcDirName).forEach(function (name) {
        var n = name.slice(0, name.length - 9);
        n = n.slice(n.lastIndexOf('/')).split("/")[1];
        list.push({
            from: eval("/^\\/" + n + "/"), to: config.output.publicPath + n +'.html'
        });
    });
    return list;
  }


  // 修改webpackDevServer配置
  historyApiFallback: {
    disableDotRule: true,
    rewrites: getRewrites()
  }
复制代码

这样就OK了?。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值