1. 程式人生 > >vue整合px2rem

vue整合px2rem

一、安裝px2rem-loader

npm install px2rem-loader

二、配置

build資料夾下utils.js,找到generateLoaders 

修改如下配置

const px2remLoader = {
    loader: 'px2rem-loader',
    options: {
      remUnit: 75 //設計稿寬度/10
    }
  }
  

  // generate loader string to be used with extract text plugin
  function generateLoaders (loader, loaderOptions) {
    const loaders = options.usePostCSS ? [cssLoader, postcssLoader,px2remLoader] : [cssLoader,px2remLoader]

    if (loader) {
      loaders.push({
        loader: loader + '-loader',
        options: Object.assign({}, loaderOptions, {
          sourceMap: options.sourceMap
        })
      })
    }

    // Extract CSS when that option is specified
    // (which is the case during production build)
    if (options.extract) {
      return ExtractTextPlugin.extract({
        use: loaders,
        fallback: 'vue-style-loader'
      })
    } else {
      return ['vue-style-loader'].concat(loaders)
    }
  }

三、重啟專案

四、如果專案集成了less/sass,還需要做如下更改

const px2remLoader = {
    loader: 'px2rem-loader',
    options: {
      remUnit: 75 //設計稿寬度/10
    }
  }
  const lessLoader ={
  	loader: 'less-loader'
  }

  // generate loader string to be used with extract text plugin
  function generateLoaders (loader, loaderOptions) {
    const loaders = options.usePostCSS ? [cssLoader, postcssLoader,px2remLoader,lessLoader] : [cssLoader,px2remLoader,lessLoader]

    if (loader) {
      loaders.push({
        loader: loader + '-loader',
        options: Object.assign({}, loaderOptions, {
          sourceMap: options.sourceMap
        })
      })
    }

    // Extract CSS when that option is specified
    // (which is the case during production build)
    if (options.extract) {
      return ExtractTextPlugin.extract({
        use: loaders,
        fallback: 'vue-style-loader'
      })
    } else {
      return ['vue-style-loader'].concat(loaders)
    }
  }