Skip to content

Commit

Permalink
fix: defensive html chunk sorting
Browse files Browse the repository at this point in the history
close #1993
  • Loading branch information
yyx990803 committed Jul 31, 2018
1 parent 686ec25 commit 495c25a
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions packages/@vue/cli-service/lib/config/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ module.exports = (api, options) => {
// HTML plugin
const resolveClientEnv = require('../util/resolveClientEnv')

// #1669 html-webpack-plugin's default sort uses toposort which cannot
// handle cyclic deps in certain cases. Monkey patch it to handle the case
// before we can upgrade to its 4.0 version (incompatible with preload atm)
const chunkSorters = require('html-webpack-plugin/lib/chunksorter')
const depSort = chunkSorters.dependency
chunkSorters.auto = chunkSorters.dependency = (chunks, ...args) => {
try {
return depSort(chunks, ...args)
} catch (e) {
// fallback to a manual sort if that happens...
return chunks.sort((a, b) => {
// make sure user entry is loaded last so user CSS can override
// vendor CSS
if (a.id === 'app') {
return 1
} else if (b.id === 'app') {
return -1
} else if (a.entry !== b.entry) {
return b.entry ? -1 : 1
}
return 0
})
}
}

const htmlOptions = {
templateParameters: (compilation, assets, pluginOptions) => {
// enhance html-webpack-plugin's built in template params
Expand Down Expand Up @@ -81,18 +106,6 @@ module.exports = (api, options) => {
removeScriptTypeAttributes: true
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
},
// #1669 default sort mode uses toposort which cannot handle cyclic deps
// in certain cases, and in webpack 4, chunk order in HTML doesn't
// matter anyway
chunksSortMode: (a, b) => {
if (a.entry !== b.entry) {
// make sure entry is loaded last so user CSS can override
// vendor CSS
return b.entry ? -1 : 1
} else {
return 0
}
}
})

Expand Down

0 comments on commit 495c25a

Please sign in to comment.