1.安装包
npm install extract-text-webpack-plugin@next
2.在webpack.config,中引入
const ExtractPlugin = require('extract-text-webpack-plugin')
3.x修改配置文件
如果没用到.css文件css配置可不需要
const path = require('path')
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const HTMLPlugin = require('html-webpack-plugin')
const webpack = require('webpack')
const ExtractPlugin = require('extract-text-webpack-plugin')
const isDev = process.env.NODE_ENV =='development'
const config= {
target:'web',
entry: path.join(__dirname, "src/index.js"), //输入文件
output: {
filename: "build.[hash:8].js", //输出文件
path: path.join(__dirname, 'dist') //输出路径
},
module:{
rules:[
{
test:/\.vue$/,
loader:'vue-loader'
},{
test: /\.jsx$/,
loader: 'babel-loader'
},
{
test:/\.(gif|png|jpg|jpeg|svg)$/,
use:[{
loader:'url-loader',
options:{
limit:1024,
name:'[name]-[hash].[ext]'
}
}]
},
]
},
plugins: [
// make sure to include the plugin for the magic
new VueLoaderPlugin(),
new webpack.DefinePlugin({
'process.env':{
NODE_ENV:isDev?'"development"':'"production"',
}
}),
new HTMLPlugin(),
],
}
if(isDev){
config.module.rules.push({
test: /\.styl/,
use: ['style-loader', 'css-loader', {
loader: 'postcss-loader',
options: {
sourceMap: true
}
}, 'stylus-loader']
})
config.devtool='#cheap-module-eval-source-map',
config.devServer={
port:8000,
host:'0.0.0.0',
overlay:{
errors:true
},
// open:true //每次都打开一个网页
hot:true //只渲染一个组件
}
config.plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
)
}else{
config.output.filename = '[name].[chunkhash:8].js'
config.module.rules.push(
{
test: /\.styl/,
use:ExtractPlugin.extract({
fallback:'style-loader',
use:[
'css-loader',
{
loader: 'postcss-loader',
options: {
sourceMap: true
}
},
'stylus-loader'
]
})
}
)
config.plugins.push(
new ExtractPlugin('styles.[name:8].css')
)
}
module.exports=config