Resct配置less

配置less

 

安裝less-loader

 

yarn add less-loader

 

打開 webpack.config.dev.js 和 webpack.config.prod.js

 

找到  test: /\.css$/修改成  test: /\.(css|less)$/, css

 

找到 text 字段下的 use ,給use數組在添加一個對象 { loader: require.resolve('less-loader')}vue

 

重啓服務

 

啓用css做用域(css只對本頁面生效)

 

打開 webpack.config.dev.js 和 webpack.config.prod.js

 

找到 loader: require.resolve('css-loader') 下的  options 字段, 給這個對象新增一個字段 modules: truereact

 

頁面調用

 

import style from './style.less'
console.log(style) // class名均爲style對象的key,因此調用必須使用style.App
render() {return ( <div className={style.App}> </div> ) }

 

重啓服務 & 查看效果

 

px轉換成rem

 

安裝 postcss-px2rem

 

yarn add postcss-px2rem

打開 webpack.config.dev.js 和 webpack.config.prod.js

 

// 調用postcss-px2rem
const px2rem = require('postcss-px2rem')

 

在css-loader中找到  autoprefixer  這個屬性,在這個屬性以後添加  px2rem({ remUnit: 75 })webpack

autoprefixer({
  browsers: [
    '>1%',
    'last 4 versions',
    'Firefox ESR',
    'not ie < 9' // React doesn't support IE8 anyway
  ],
  flexbox: 'no-2009'
}),
px2rem({ remUnit: 75 }) 

 

重啓服務 & 查看效果

 

附 : 完整代碼

 

webpack.config.dev.js

 

// webpack.config.dev.js
{
  test: /\.(css|less)$/,
  use: [
    require.resolve('style-loader'),
    {
      loader: require.resolve('css-loader'),
      options: {
        importLoaders: 1,
        modules: true // 是否啓動css局部做用域
      }
    },
    {
      loader: require.resolve('postcss-loader'),
      options: {
        // Necessary for external CSS imports to work
        // https://github.com/facebookincubator/create-react-app/issues/2677
        ident: 'postcss',
        plugins: () => [
          require('postcss-flexbugs-fixes'),
          autoprefixer({
            browsers: [
              '>1%',
              'last 4 versions',
              'Firefox ESR',
              'not ie < 9' // React doesn't support IE8 anyway
            ],
            flexbox: 'no-2009'
          }),
          px2rem({ remUnit: 75 }) //設計稿根據750px(iphone6)
        ]
      }
    },
    {
      loader: require.resolve('less-loader')
    }
  ]
},

 

webpack.config.prod.js

 

// webpack.config.prod.js
{
  test: /\.(css|less)$/,
  loader: ExtractTextPlugin.extract(
    Object.assign(
      {
        fallback: {
          loader: require.resolve('style-loader'),
          options: {
            hmr: false
          }
        },
        use: [
          {
            loader: require.resolve('css-loader'),
            options: {
              importLoaders: 1,
              modules: true, // 是否啓動css局部做用域
              minimize: true,
              sourceMap: shouldUseSourceMap
            }
          },
          {
            loader: require.resolve('postcss-loader'),
            options: {
              // Necessary for external CSS imports to work
              // https://github.com/facebookincubator/create-react-app/issues/2677
              ident: 'postcss',
              plugins: () => [
                require('postcss-flexbugs-fixes'),
                autoprefixer({
                  browsers: [
                    '>1%',
                    'last 4 versions',
                    'Firefox ESR',
                    'not ie < 9' // React doesn't support IE8 anyway
                  ],
                  flexbox: 'no-2009'
                }),
                px2rem({ remUnit: 75 }) //設計稿根據750px(iphone6)
              ]
            }
          },
          {
            loader: require.resolve('less-loader')
          }
        ]
      },
      extractTextPluginOptions
    )
  )
  // Note: this won't work without `new ExtractTextPlugin()` in `plugins`.
},

 

style.less

 

.App {
  text-align: center;
  font-size: 16px;
  .goto {
    color: red;
  }
}

 

App.vue

 

// 頁面調用
import style from './style.less'
// console.log(style)  輸出:{App:{...},goto:{...}}
<div className={style.App}>
  <Link to="/series" className={style.goto}>
    跳轉
  </Link>
</div>
相關文章
相關標籤/搜索