使用 postcss-loadercss
使用後置處理器(代碼生成完成以後更改)autoprefixer插件(根據Can I Use 規則 caniuse.com)html
module.exports={
module:{
rules:[
test:/\.css$/,
use:[
'style-loader',
'css-loader',
'postcss-loader'
]
]
}
}
複製代碼
module.exports = {
plugins: [
require('autoprefixer')
]
複製代碼
}html5
* 四、package.json中添加瀏覽器支持版本的配置
> https://github.com/browserslist/browserslist#readme
```js
"browserslist": [
"last 1 version",
"> 1%",
"IE 10"
]
複製代碼
使用px2rem-loader:對px和rem的轉換;node
頁面渲染時須要知道根元素的font-size值:使用手淘的庫lib-flexiblewebpack
一、安裝css3
a、npm i px2rem-loader -D (開發時使用)git
b、npm i lib-flexible -S (依賴包)es6
二、webpack添加配置github
module.exports = {
...
module:{
rules:[{
test:/\.scss$/,
use:[
'style-loader',
'css-loader',
'sass-loader',
{
loader:'px2rem-loader',
options:{
remUnit:75,
remPrecision:8
}
}
]
}]
}
}
複製代碼
三、頁面加載時須要計算根元素的font-size,因此lib-flexible的引用須要前置web
在生成的html中加入lib-flexible的代碼
若是存在es6代碼須要增長babel-loader,進行轉換
${require('...')}讀取出內容,而後進行插入
${require('raw-loader!./meta.html')}
如:lib-flexible,在頁面加載時,須要嵌入到html中
<script>$(require('raw-loader!babel-loader!./node_modules/lib-flexible'))</script>
如:首屏的樣式內聯入html中,防止頁面閃動
module.exports = {
module:{
rules: [
{
test:/\.scss$/,
use:[
{
loader:'style-loader',
options:{
insertAt:'top',//樣式插入到<head>
singleton:true//將全部的style標籤合併成一個
},
'css-loader',
'sass-loader'
}
]
}
]
}
}
複製代碼
須要配置多個entry和多個HtmlWebpackPlugin
文件目錄:
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const glob = require('glob')
function setMPA(){
const entryFiles = glob.sync(path.join(__dirname,'../mpa/*/index.js'))
const entry = {}
const htmlWebpackPlugins = []
// console.log(entryFiles)
entryFiles.forEach(key=>{
const name = key.replace(/.*\/mpa\/(\w+)\/index\.js$/,'$1')
entry[name] = key
htmlWebpackPlugins.push(getHtmlWebpackPlugin(name))
})
return {entry,htmlWebpackPlugins}
}
function getHtmlWebpackPlugin(name){
return new HtmlWebpackPlugin({
template: path.join(__dirname,`../mpa/${name}/index.html`),//模板名
filename: `${name}.html`,//html文件名
chunks:[name],//不設置會把全部的entry中js插入html中
inject: true,//true:[Boolean]默認值,script標籤位於html文件的body底部;body:[String]與true功能同樣;head:[String],script標籤位於html的head中;false:不插入生成的js文件
//favicon: 'path/to/my_favicon.ico'
minify:{//對html文件進行壓縮
html5: true,
collapseWhitespace: true,
preserveLineBreaks: false,
minifyCSS: true,
minifyJS: true,
removeComments: false
}
})
}
module.exports = {entry,htmlWebpackPlugins} = setMPA()
複製代碼
三、webpack文件中引用const {entry,htmlWebpackPlugins} = require('./src/util/webpackentrys_glob')
module.exports = {
entry:entry,
...
plugins:[...].contact(htmlWebpackPlugins)
複製代碼