用法:css
const path = require('path');
module.exports={
output:{
filename:'bundle.js'
},
module:{
rules:[
{test:/\.txt$/,use:'raw-loader'} //test:指定匹配規則 , use:指定使用的loader名稱
]
}
}
複製代碼
用法:html
const path = require('path');
module.exports = {
output: {
filename:'bundle.js'
},
plugins:[
new HtmlWebpackPlugin({
template:'./src/index.html'
})
]
}
複製代碼
解析es6的語法須要babel-loader,而babel-loader依賴於babel,因此須要創建babel的配置文件.babelrcreact
{
"presets":[],//presets爲一系列babel plugins的集合
"plugins":[] //一個plugin對應一個功能
}
複製代碼
-D:--save devwebpack
{
"presets":[
"@babel/preset-env" //es6的babel preset配置
]
}
複製代碼
module.exports = {
entry:{...},
output:{...},
mode:"production",
module:{
rules:[
{test:/\.js$/,use:'babel-loader'}
]
}
}
複製代碼
{
presets:[
"@babel/preset-react"
]
}
複製代碼
'use strict'
import React from 'react';
import ReactDom from 'react-dom';
class Search extends React.Component {
render(){
return <div>Search Test</div>
}
}
ReactDom.render(
<Search/>,
document.getElementById('root')
)
複製代碼
添加插件html-webpack-plugin,自動解析模板,不用每次去建立htmlgit
'use strict'
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry:{...},
output:{...},
module:{rules:[...]},
plugins:[
new HtmlWebpackPlugin({
template:'./public/index.html' //模板
})
]
}
複製代碼
github源碼:github.com/logmei/webp…