沒接觸過flexible的建議先看看大漠的這篇文章這樣你纔會知道長度爲何用rem,而字體要用pxcss
npm install lib-flexible --save
在項目入口文件main.js中添加以下代碼,引入flexiblehtml
import 'lib-flexible'
使用 webpack 的 px2rem-loader,自動將px轉換爲remvue
npm install px2rem-loader --save-dev
在vue-cli生成的文件中,找到如下文件 build/utils.js,以下圖添加配置html5
'use strict'
const path = require('path')
const config = require('../config')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const packageConfig = require('../package.json')
exports.assetsPath = function (_path) {
const assetsSubDirectory = process.env.NODE_ENV === 'production'
? config.build.assetsSubDirectory
: config.dev.assetsSubDirectory
return path.posix.join(assetsSubDirectory, _path)
}
exports.cssLoaders = function (options) {
options = options || {}
const cssLoader = {
loader: 'css-loader',
options: {
sourceMap: options.sourceMap,
importLoaders:5 //在css-loader前應用的loader的數目,默認爲0.
//若是不加這個 @import的外部css文件將不能正常轉換
//若是不行 請試着調大數字
//更改後必須調大數字 不然無效
}
}
const px2remLoader={
loader:'px2rem-loader',
options:{
remUnit:75 //設計稿的1/10 ,咱們假設設計稿是750px
}
}
const postcssLoader = {
loader: 'postcss-loader',
options: {
sourceMap: options.sourceMap
}
}
// generate loader string to be used with extract text plugin
function generateLoaders (loader, loaderOptions) {
const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader,px2remLoader] //添加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)
}
}
// https://vue-loader.vuejs.org/en/configurations/extract-css.html
return {
css: generateLoaders(),
postcss: generateLoaders(),
less: generateLoaders('less'),
sass: generateLoaders('sass', { indentedSyntax: true }),
scss: generateLoaders('sass'),
stylus: generateLoaders('stylus'),
styl: generateLoaders('stylus')
}
}
// Generate loaders for standalone style files (outside of .vue)
exports.styleLoaders = function (options) {
const output = []
const loaders = exports.cssLoaders(options)
for (const extension in loaders) {
const loader = loaders[extension]
output.push({
test: new RegExp('\\.' + extension + '$'),
use: loader
})
}
return output
}
exports.createNotifierCallback = () => {
const notifier = require('node-notifier')
return (severity, errors) => {
if (severity !== 'error') return
const error = errors[0]
const filename = error.file && error.file.split('!').pop()
notifier.notify({
title: packageConfig.name,
message: severity + ': ' + error.name,
subtitle: filename || '',
icon: path.join(__dirname, 'logo.png')
})
}
}
關於importLoaders
:如如有疑問,請參考webpack深刻與實戰4-4
2017.12.8更新說明:是否須要配置importLoaders
,可參考最底部的說明。node
安裝px2rem後,再使用px上有些不一樣,你們能夠參考px2rem官方介紹,下面簡單介紹一下。webpack
直接寫px,編譯後會直接轉化成rem ---- 除開下面兩種狀況,其餘長度用這個
在px後面添加/*no*/
,不會轉化px,會原樣輸出。 --- 通常border需用這個
在px後面添加/*px*/
,會根據dpr的不一樣,生成三套代碼。---- 通常字體需用這個git
示例代碼
編譯前(本身寫的代碼)github
.selector { width: 150px; height: 64px; /*px*/ font-size: 28px; /*px*/ border: 1px solid #ddd; /*no*/ }
編譯後(打包後的代碼)web
.selector { width: 2rem; border: 1px solid #ddd; } [data-dpr="1"] .selector { height: 32px; font-size: 14px; } [data-dpr="2"] .selector { height: 64px; font-size: 28px; } [data-dpr="3"] .selector { height: 96px; font-size: 42px; }
2017.12.8更新:在實際運用中發現對於外部引入的css文件,有時候px2rem能正常轉換,有時候又不能轉換,究竟是什麼緣由呢?試驗了三種不一樣的css引入狀況,發現第一種能正常轉換,二三不能正常轉換,至於緣由,因爲才疏學淺,仍是不懂,求大神解答三種引入方式的區別。vue-cli
若是明白了這些方法,就不必再配置cssLoader
的importLoaders
了,由於下面的方法更容易控制外部引入的css是否須要轉rem,而更改importLoaders
就控制不了了,它會強制轉換。
<style src='../assets/style.css'> /* px2rem能正常轉換 */ </style>
<style> /* px2rem不能正常轉換 */ @import '../assets/style.css'; </style>
<style> /* px2rem不能正常轉換 */ @import url('../assets/style.css'); </style>
'use strict'
const path = require('path')
const config = require('../config')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const packageConfig = require('../package.json')
exports.assetsPath = function (_path) {
const assetsSubDirectory = process.env.NODE_ENV === 'production'
? config.build.assetsSubDirectory
: config.dev.assetsSubDirectory
return path.posix.join(assetsSubDirectory, _path)
}
exports.cssLoaders = function (options) {
options = options || {}
const cssLoader = {
loader: 'css-loader',
options: {
sourceMap: options.sourceMap,
importLoaders:5 //在css-loader前應用的loader的數目,默認爲0.
//若是不加這個 @import的外部css文件將不能正常轉換
//若是不行 請試着調大數字
//更改後必須調大數字 不然無效
}
}
const px2remLoader={
loader:'px2rem-loader',
options:{
remUnit:75 //設計稿的1/10 ,咱們假設設計稿是750px
}
}
const postcssLoader = {
loader: 'postcss-loader',
options: {
sourceMap: options.sourceMap
}
}
// generate loader string to be used with extract text plugin
function generateLoaders (loader, loaderOptions) {
const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader,px2remLoader] //添加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)
}
}
// https://vue-loader.vuejs.org/en/configurations/extract-css.html
return {
css: generateLoaders(),
postcss: generateLoaders(),
less: generateLoaders('less'),
sass: generateLoaders('sass', { indentedSyntax: true }),
scss: generateLoaders('sass'),
stylus: generateLoaders('stylus'),
styl: generateLoaders('stylus')
}
}
// Generate loaders for standalone style files (outside of .vue)
exports.styleLoaders = function (options) {
const output = []
const loaders = exports.cssLoaders(options)
for (const extension in loaders) {
const loader = loaders[extension]
output.push({
test: new RegExp('\\.' + extension + '$'),
use: loader
})
}
return output
}
exports.createNotifierCallback = () => {
const notifier = require('node-notifier')
return (severity, errors) => {
if (severity !== 'error') return
const error = errors[0]
const filename = error.file && error.file.split('!').pop()
notifier.notify({
title: packageConfig.name,
message: severity + ': ' + error.name,
subtitle: filename || '',
icon: path.join(__dirname, 'logo.png')
})
}
}