码迷,mamicode.com
首页 > 移动开发 > 详细

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

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

标签:his   mob   plugins   hash   server   ble   模板   src   电脑   

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 构建同时适用于手机和电脑的调试服务器

标签:his   mob   plugins   hash   server   ble   模板   src   电脑   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!