Skip to content

Commit 806418e

Browse files
author
Guillaume Chau
committed
fix: mini-css-extract-plugin error on server
1 parent ee4edd1 commit 806418e

File tree

4 files changed

+66
-854
lines changed

4 files changed

+66
-854
lines changed

lib/ServerMiniCssExtractPlugin.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
2+
3+
class ServerMiniCssExtractPlugin extends MiniCssExtractPlugin {
4+
getCssChunkObject (mainChunk) {
5+
return {}
6+
}
7+
}
8+
9+
module.exports = ServerMiniCssExtractPlugin

lib/webpack.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ const WebpackBar = require('webpackbar')
77

88
const config = require('./config')
99
const HtmlFilterPlugin = require('./HtmlFilterPlugin')
10+
const ServerMiniCssExtractPlugin = require('./ServerMiniCssExtractPlugin')
1011

1112
exports.getWebpackConfig = ({ target }) => {
1213
const service = config.service
1314
const isProd = service.mode === 'production'
15+
const isClient = target === 'client'
1416

1517
let webpackConfig = service.resolveChainableWebpackConfig()
1618

@@ -21,6 +23,25 @@ exports.getWebpackConfig = ({ target }) => {
2123
webpackConfig.plugins.delete('progress')
2224
webpackConfig.plugins.delete('no-emit-on-errors')
2325

26+
if (!isClient) {
27+
webpackConfig.plugins.delete('friendly-errors')
28+
29+
// Fix mini-css-extract-plugin error on server
30+
const langs = ['css', 'postcss', 'scss', 'sass', 'less', 'stylus']
31+
const types = ['vue-modules', 'vue', 'normal-modules', 'normal']
32+
for (const lang of langs) {
33+
for (const type of types) {
34+
const rule = webpackConfig.module.rule(lang).oneOf(type)
35+
if (rule.uses.has('extract-css-loader')) {
36+
rule.use('extract-css-loader').loader(ServerMiniCssExtractPlugin.loader)
37+
}
38+
}
39+
}
40+
if (webpackConfig.plugins.has('extract-css')) {
41+
webpackConfig.plugin('extract-css').use(ServerMiniCssExtractPlugin)
42+
}
43+
}
44+
2445
// HTML
2546
webpackConfig.plugin('html-filter').use(HtmlFilterPlugin)
2647
if (isProd) {
@@ -49,7 +70,6 @@ exports.getWebpackConfig = ({ target }) => {
4970
},
5071
})
5172

52-
const isClient = target === 'client'
5373
if (isClient) {
5474
webpackConfig = mergeWebpack(webpackConfig, {
5575
plugins: [
@@ -80,6 +100,7 @@ exports.getWebpackConfig = ({ target }) => {
80100
devtool: 'source-map',
81101
optimization: {
82102
splitChunks: false,
103+
minimize: false,
83104
},
84105
})
85106

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"fs-extra": "^7.0.0",
3636
"lru-cache": "^4.1.3",
3737
"memory-fs": "^0.4.1",
38+
"mini-css-extract-plugin": "^0.4.2",
3839
"portfinder": "^1.0.17",
3940
"rimraf": "^2.6.2",
4041
"serve-favicon": "^2.5.0",

0 commit comments

Comments
 (0)