渡边的青豆
2018-07-23 21:50
在网上找到一位仁兄的代码,链接 https://segmentfault.com/a/1190000013994415,
然后就根据他的来试了一下,
------ webpack.config.js ------
const path = require('path') const HTMLPlugin = require('html-webpack-plugin') const webpack = require('webpack') const MiniCssExtractPlugin = require('mini-css-extract-plugin') const { VueLoaderPlugin } = require('vue-loader'); const isDev = process.env.NODE_ENV === 'development' const config = { target: 'web', entry: path.join(__dirname, 'src/index.js'), output: { filename: 'bundle.[hash:8].js', path: path.join(__dirname, 'dist') }, module: { rules: [ { test: /\.vue$/, loader: 'vue-loader' }, { test: /\.jsx$/, loader: 'babel-loader' }, { test:/\.(gif|jpg|jpeg|png|svg)$/, use: [ { loader: 'url-loader', options: { limit: 1024, name: '[name]-aaa.[ext]' } } ] } ] }, plugins: [ new VueLoaderPlugin(), new webpack.DefinePlugin({ 'process.env': { NODE_ENV: isDev ? '"development"' : '"production"' } }), new HTMLPlugin(), new MiniCssExtractPlugin({ // Options similar to the same options in webpackOptions.output // both options are optional filename: "[name].css", chunkFilename: "[id].css" }) ] } if(isDev){ config.module.rules.push({ test: /\.styl/, use: [ MiniCssExtractPlugin.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 // historyFallback: { // } hot: true } config.plugins.push( new webpack.HotModuleReplacementPlugin(), new webpack.NoEmitOnErrorsPlugin() ) } else { config.output.filename = 'js/[name].[chunkhash:8].js'; config.module.rules.push( { test: /\.styl$/, use: [ MiniCssExtractPlugin.loader, 'css-loader', { loader: 'postcss-loader', options: { sourceMap: true } }, 'stylus-loader' ] } ) config.plugins.push( new MiniCssExtractPlugin({ filename: "[name].[chunkhash:8].css", chunkFilename: "[id].css" }) ) } module.exports = config;
然后,的确是有生成(然而我已经搞不懂到底会生成什么样子的文件了......),生成的dist如下
,
然而,也有报错....,真的看不懂了.....,如下图
求个大佬来说说到底是怎么回事.....改成 mini-css-extract-plugin 到底是怎么个用法....
求你们了...
} else {
// 修改打包的js名称
config.output.filename = '[name].[hash:8].js'
// 添加正式环境的规则
config.module.rules.push({
test: /\.styl$/,
use: extracttextwebpackplugin.extract({
fallback: 'style-loader',
use: [
'css-loader',
{
loader: 'postcss-loader',
options: {
sourceMap: true
}
},
'stylus-loader',
]
})
})
config.plugins.push(
new extracttextwebpackplugin('styles.[Hash:8].css')
)
}
楼上的是webpack4.+常规用法。
但你还可以尝试使用:
npm install --save-dev extract-text-webpack-plugin@next
升级extract-text-webpack-plugin为4.0best版本
然后将
contentHsah:8 更改为 chunkhash:8
这样打包出来的和老师的是一样的
我的也是基于webpack4.0+ 的,这个里有一篇文章
https://blog.csdn.net/harsima/article/details/80819747
介绍很清楚,不过里面的写法和老师的不太一样,不懂的可以看我的GitHub https://github.com/marin1993/todo 里面用的是 mini-css-extract-plugin
Vue+Webpack打造todo应用
84606 学习 · 787 问题
相似问题