首页 > 移动平台 > 详细

webpack 构建同时适用于手机和电脑的调试服务器

时间:2018-02-10 18:38:58      阅读:302      评论:0      收藏:0      [点我收藏+]

plugins

  plugins: [
    new HtmlWebpackPlugin({ // 使用模板同时生成 pc.html和mobile.html
      title: ‘pc‘,
      filename: ‘pc.html‘,
      hash: true,
      template: path.resolve(__dirname, ‘../src/template.html‘),
      chunks: [‘pc‘] // 只注入pc.js
    }),
    new HtmlWebpackPlugin({ 
      title: ‘mobile‘,
      hash: true,
      filename: ‘mobile.html‘, 
      template: path.resolve(__dirname, ‘../src/template.html‘),
      chunks: [‘mobile‘] // 只注入mobile.js
    }),
  ]

 webpack-dev-server

devServer: {
    historyApiFallback: {
      rewrites: [
      {
        from: /.*/, // 让request url 重写,这儿默认是请求html文件会运行to方法。
        to(ctx) {
          if (isMoble(ctx.request.get(‘user-agent‘))) { // 获取请求客服端信息,如果是mobile,重定向到mobile.html。
            console.log(‘mobile‘)
            return ‘/mobile.html‘ // 若为手机端,地址改为mobile.html
          } else {
            console.log(‘pc‘) // 若为pc端,地址改为pc.html
            return ‘/pc.html‘
          }
        }
      }
    ]
  },
}

 

webpack 构建同时适用于手机和电脑的调试服务器

原文:https://www.cnblogs.com/gsgs/p/8439317.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!