Skip to content

Commit 495c25a

Browse files
committed
fix: defensive html chunk sorting
close #1993
1 parent 686ec25 commit 495c25a

File tree

1 file changed

+25
-12
lines changed
  • packages/@vue/cli-service/lib/config

1 file changed

+25
-12
lines changed

packages/@vue/cli-service/lib/config/app.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,31 @@ module.exports = (api, options) => {
4848
// HTML plugin
4949
const resolveClientEnv = require('../util/resolveClientEnv')
5050

51+
// #1669 html-webpack-plugin's default sort uses toposort which cannot
52+
// handle cyclic deps in certain cases. Monkey patch it to handle the case
53+
// before we can upgrade to its 4.0 version (incompatible with preload atm)
54+
const chunkSorters = require('html-webpack-plugin/lib/chunksorter')
55+
const depSort = chunkSorters.dependency
56+
chunkSorters.auto = chunkSorters.dependency = (chunks, ...args) => {
57+
try {
58+
return depSort(chunks, ...args)
59+
} catch (e) {
60+
// fallback to a manual sort if that happens...
61+
return chunks.sort((a, b) => {
62+
// make sure user entry is loaded last so user CSS can override
63+
// vendor CSS
64+
if (a.id === 'app') {
65+
return 1
66+
} else if (b.id === 'app') {
67+
return -1
68+
} else if (a.entry !== b.entry) {
69+
return b.entry ? -1 : 1
70+
}
71+
return 0
72+
})
73+
}
74+
}
75+
5176
const htmlOptions = {
5277
templateParameters: (compilation, assets, pluginOptions) => {
5378
// enhance html-webpack-plugin's built in template params
@@ -81,18 +106,6 @@ module.exports = (api, options) => {
81106
removeScriptTypeAttributes: true
82107
// more options:
83108
// https://github.com/kangax/html-minifier#options-quick-reference
84-
},
85-
// #1669 default sort mode uses toposort which cannot handle cyclic deps
86-
// in certain cases, and in webpack 4, chunk order in HTML doesn't
87-
// matter anyway
88-
chunksSortMode: (a, b) => {
89-
if (a.entry !== b.entry) {
90-
// make sure entry is loaded last so user CSS can override
91-
// vendor CSS
92-
return b.entry ? -1 : 1
93-
} else {
94-
return 0
95-
}
96109
}
97110
})
98111

0 commit comments

Comments
 (0)