Skip to content

Commit 701658a

Browse files
committed
fix: workaround for empty style chunk
1 parent d9ac270 commit 701658a

File tree

5 files changed

+28
-33
lines changed

5 files changed

+28
-33
lines changed

lib/build.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
88
const { promisify } = require('util')
99
const rimraf = promisify(require('rimraf'))
1010
const mkdirp = promisify(require('mkdirp'))
11+
const readFile = promisify(fs.readFile)
1112
const writeFile = promisify(fs.writeFile)
1213

1314
const prepare = require('./prepare')
@@ -27,14 +28,18 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
2728
const serverConfig = createServerConfig(options, cliOptions).toConfig()
2829

2930
// compile!
30-
await compile([clientConfig, serverConfig])
31+
const stats = await compile([clientConfig, serverConfig])
3132

3233
const serverBundle = require(path.resolve(outDir, 'manifest/server.json'))
3334
const clientManifest = require(path.resolve(outDir, 'manifest/client.json'))
3435

3536
// remove manifests after loading them.
3637
await rimraf(path.resolve(outDir, 'manifest'))
3738

39+
// fine and remove empty style chunk caused by
40+
// https://github.com/webpack-contrib/mini-css-extract-plugin/issues/85
41+
await workaroundEmptyStyleChunk()
42+
3843
// create server renderer using built manifests
3944
const renderer = createBundleRenderer(serverBundle, {
4045
clientManifest,
@@ -76,7 +81,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
7681
reject(new Error(`Failed to compile with errors.`))
7782
return
7883
}
79-
resolve()
84+
resolve(stats.toJson({ modules: false }))
8085
})
8186
})
8287
}
@@ -121,4 +126,21 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
121126
await mkdirp(path.dirname(filePath))
122127
await writeFile(filePath, html)
123128
}
129+
130+
async function workaroundEmptyStyleChunk () {
131+
const styleChunk = stats.children[0].assets.find(a => {
132+
return /styles\.\w{8}\.js$/.test(a.name)
133+
})
134+
const styleChunkPath = path.resolve(outDir, styleChunk.name)
135+
const styleChunkContent = await readFile(styleChunkPath, 'utf-8')
136+
await rimraf(styleChunkPath)
137+
// prepend it to app.js.
138+
// this is necessary for the webpack runtime to work properly.
139+
const appChunk = stats.children[0].assets.find(a => {
140+
return /app\.\w{8}\.js$/.test(a.name)
141+
})
142+
const appChunkPath = path.resolve(outDir, appChunk.name)
143+
const appChunkContent = await readFile(appChunkPath, 'utf-8')
144+
await writeFile(appChunkPath, styleChunkContent + appChunkContent)
145+
}
124146
}

lib/webpack/RemoveEmptyChunkPlugin.js

-12
This file was deleted.

lib/webpack/baseConfig.js

-9
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ module.exports = function createBaseConfig ({
190190
.set('optimization', {
191191
splitChunks: {
192192
cacheGroups: {
193-
chunks: 'all',
194193
styles: {
195194
name: 'styles',
196195
// necessary for extraction to include md files as well
@@ -201,14 +200,6 @@ module.exports = function createBaseConfig ({
201200
}
202201
}
203202
})
204-
205-
// enforcing all styles extraction leaves an empty styles chunk.
206-
// prevent it from being emitted.
207-
// this is a bug in mini-css-extract-plugin
208-
// https://github.com/webpack-contrib/mini-css-extract-plugin/issues/85
209-
config
210-
.plugin('remove-empty-chunk')
211-
.use(require('./RemoveEmptyChunkPlugin'))
212203
}
213204

214205
return config

lib/webpack/clientConfig.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ module.exports = function createClientConfig (options, cliOptions) {
1515
// source contains it (although only uses it if it's native).
1616
setImmediate: false,
1717
global: false,
18-
// process is injected via DefinePlugin, although some 3rd party
19-
// libraries may require a mock to work properly (#934)
20-
process: 'mock',
18+
process: false,
2119
// prevent webpack from injecting mocks to Node native modules
2220
// that does not make sense for the client
2321
dgram: 'empty',

lib/webpack/clientPlugin.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Temporarily copied from a dev build
1+
// Temporarily hacked dev build
22

33
var isJS = function (file) { return /\.js(\?[^.]+)?$/.test(file) }
44

@@ -32,17 +32,13 @@ VueSSRClientPlugin.prototype.apply = function apply (compiler) {
3232

3333
var allFiles = uniq(stats.assets
3434
.map(function (a) { return a.name }))
35-
.filter(file => {
36-
return !/styles\.\w{8}\.js$/.test(file)
37-
})
3835

3936
var initialFiles = uniq(Object.keys(stats.entrypoints)
4037
.map(function (name) { return stats.entrypoints[name].assets })
4138
.reduce(function (assets, all) { return all.concat(assets) }, [])
4239
.filter(function (file) { return isJS(file) || isCSS(file) }))
43-
.filter(file => {
44-
return !/styles\.\w{8}\.js$/.test(file)
45-
})
40+
// Avoid preloading / injecting the style chunk
41+
.filter(file => !/styles\.\w{8}\.js$/.test(file))
4642

4743
var asyncFiles = allFiles
4844
.filter(function (file) { return isJS(file) || isCSS(file) })

0 commit comments

Comments
 (0)