以css配置示例,less与sass同理
npm install extract-text-webpack-plugin@next --save-dev
const extractTextPlugin=require('extract-text-webpack-plugin')
module.exports={
//...code
module:{
rules:[{
test:/\.css$/,
use:extractTextPlugin.extract({
fallback:"style-loader",
use:['css-loader'],
publicPath:"../"
})
}]
},
plugins:[
new extractTextPlugin("./css/[name].css")//输出路径
]
}
如果还使用了根据css自动加前缀loader
const extractTextPlugin=require('extract-text-webpack-plugin')
module.exports={
//...code
module:{
rules:[{
test:/\.css$/,
use:extractTextPlugin.extract({
fallback: "style-loader",
use: ['css-loader', {
loader: 'postcss-loader',
options: {
plugins: [require('autoprefixer')]
}
}]
publicPath: '../'
})
}]
},
plugins:[
new extractTextPlugin("./css/[name].css")//输出路径
]
}
配置完成后运行webpack打包即可
npm install mini-css-extract-plugin --save-dev
const miniCssPlugin=require('mini-css-extract-plugin');
module.exports={
module:{
rules:[
{
test:/\.css$/,
use: [{
loader: miniCssPlugin.loader,
options: {
publicPath: '../'
}
}, 'css-loader']
}
]
},
plugins:[
new miniCssPlugin({
filename:'./css/[name].css'
})
]
}
原文:https://www.cnblogs.com/roseAT/p/12088740.html