Vue-cli 4.x 配置pug,less,rem布局等

新项目配置高效率的开发环境

很久没有关注Vue生态圈,发现Vue-cli已经4.x版本了,相比3.0版本又有了一些优化,对应的环境配置也有点不同了。

感兴趣可关注我的公众号 ruanjianxiaoyu 相互探讨学习,分享更多有趣好玩的东西。记得后台留言哦!

开始

安装时的问题

官方文档上有说明,在安装最新的脚手架之前最好先卸载旧版本的vue/cli。

npm uninstall vue-cli -g

yarn global remove vue-cli

一切按照官方文档 操作之后会搭建基本开发环境。 但是我们想要在这基础上继续配置就需要了解其原理了。

下面是我的移动端项目的配置 pug + less + flexible 等,简单记录一下

配置pug

安装 yarn add -D pug pug-plain-loader 即可
template模板中就可以快乐的使用 lang="pug"

配置less

安装 yarn add -D less less-loader
style标签中 可以使用 <style lang="less">

配置全局自动引入 common.less

在我们写css的时候,每次都需要再less里引入 全局写的mixin 。每次写一遍往往会觉得很烦。所以能不能自动引用全局公共文件呢。

  1. 安装 style-resources-loade
yarn add -D style-resources-loade
  1. 在项目根目录新增 vue.config.js文件
  2. 添加一下代码即可解决上面的问题
function addStyleResource(rule) {
  rule
    .use("style-resource")
    .loader("style-resources-loader")
    .options({
      patterns: [path.resolve(__dirname, "./src/assets/variable.less")] // 这里配置需要引入的文件
    });
}

module.exports= {
  chainWebpack: config => {
    const types = ["vue-modules", "vue", "normal-modules", "normal"];
    types.forEach(type =>
      addStyleResource(config.module.rule("less").oneOf(type))
    );
  },
}

配置px2rem

  1. 安装 yarn add -D postcss-plugin-px2rem
  2. vue.config.js文件中增加配置
module.exports= {
  css: {
    loaderOptions: {
      postcss: {
        plugins: [
          autoprefixer(),
          px2rem({
            rootValue: 75, // 根节点的,根据UI搞来计算。我的是 750*1334 ,则这里天75. 其他可不变
            unitPrecision: 5,
            propWhiteList: [],
            propBlackList: [],
            exclude: /node_modules/,
            selectorBlackList: [],
            ignoreIdentifier: false,
            replace: true,
            mediaQuery: false,
            minPixelValue: 3
          })
        ]
      }
    }
  },
  chainWebpack: config => {
    const types = ["vue-modules", "vue", "normal-modules", "normal"];
    types.forEach(type =>
      addStyleResource(config.module.rule("less").oneOf(type))
    );
  },
}

配置代理等

vue.conifg.js中增加配置

module.exports = {
  css: {
    loaderOptions: {
      postcss: {
        plugins: [
          autoprefixer(),
          px2rem({
            rootValue: 75, // 根节点的
            unitPrecision: 5,
            propWhiteList: [],
            propBlackList: [],
            exclude: /node_modules/,
            selectorBlackList: [],
            ignoreIdentifier: false,
            replace: true,
            mediaQuery: false,
            minPixelValue: 3
          })
        ]
      }
    }
  },
  chainWebpack: config => {
    const types = ["vue-modules", "vue", "normal-modules", "normal"];
    types.forEach(type =>
      addStyleResource(config.module.rule("less").oneOf(type))
    );
  },
 devServer: {
    disableHostCheck: true,
    proxy: {
      "/api": {
        target: "http://192.168.92.49:8000",
        changeOrigin: true
      },
      "/web": {
        target: "https://uin-develop.ergengtv.com",
        changeOrigin: true,
        pathRewrite: { "^/web": "/api/web" }
      },
      "/ergengtv": {
        target: "https://api.ergengtv.com",
        changeOrigin: true,
        pathRewrite: { "^/ergengtv": "" }
      }
    }
  }
};

结语

前端技术时刻在进步,时刻需要保持学习进步的心。 如有什么问题欢迎留言。感兴趣可关注公众号 ruanjianxiaoyu 相互探讨学习。大家一起进步鸭!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值