Skip to content

Commit 8468aeb

Browse files
committed
fix(ssr): vue-ssr-webpack-plugin compatible with webpack 4/5
close vuejs#11718
1 parent f7ddd26 commit 8468aeb

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/server/template-renderer/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export default class TemplateRenderer {
224224
if (this.clientManifest) {
225225
const initial = this.preloadFiles.filter(({ file }) => isJS(file))
226226
const async = (this.getUsedAsyncFiles(context) || []).filter(({ file }) => isJS(file))
227-
const needed = [initial[0]].concat(async, initial.slice(1))
227+
const needed = [].concat(initial[0] ? [initial[0]] : [], async, initial.slice(1));
228228
return needed.map(({ file }) => {
229229
return `<script src="${this.publicPath}${file}" defer></script>`
230230
}).join('')

src/server/webpack-plugin/client.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const hash = require('hash-sum')
22
const uniq = require('lodash.uniq')
33
import { isJS, isCSS, onEmit } from './util'
4+
import { isObject } from 'shared/util'
45

56
export default class VueSSRClientPlugin {
67
constructor (options = {}) {
@@ -19,6 +20,13 @@ export default class VueSSRClientPlugin {
1920
const initialFiles = uniq(Object.keys(stats.entrypoints)
2021
.map(name => stats.entrypoints[name].assets)
2122
.reduce((assets, all) => all.concat(assets), [])
23+
.map(file => {
24+
if (isObject(file) && file.name) {
25+
return file.name;
26+
} else {
27+
return file;
28+
}
29+
})
2230
.filter((file) => isJS(file) || isCSS(file)))
2331

2432
const asyncFiles = allFiles

src/server/webpack-plugin/server.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { validate, isJS, onEmit } from './util'
2+
import { isObject } from 'shared/util'
23

34
export default class VueSSRServerPlugin {
45
constructor (options = {}) {
@@ -20,7 +21,14 @@ export default class VueSSRServerPlugin {
2021
return cb()
2122
}
2223

23-
const entryAssets = entryInfo.assets.filter(isJS)
24+
const entryAssets = entryInfo.assets
25+
.map(file => {
26+
if ( isObject(file) && file.name) {
27+
return file.name;
28+
} else {
29+
return file;
30+
}
31+
}).filter(isJS)
2432

2533
if (entryAssets.length > 1) {
2634
throw new Error(
@@ -45,6 +53,9 @@ export default class VueSSRServerPlugin {
4553
stats.assets.forEach(asset => {
4654
if (isJS(asset.name)) {
4755
bundle.files[asset.name] = compilation.assets[asset.name].source()
56+
if (asset.info && asset.info.related && asset.info.related.sourceMap) {
57+
bundle.maps[asset.info.related.sourceMap.replace(/\.map$/, '')] = JSON.parse(compilation.assets[asset.info.related.sourceMap].source());
58+
}
4859
} else if (asset.name.match(/\.js\.map$/)) {
4960
bundle.maps[asset.name.replace(/\.map$/, '')] = JSON.parse(compilation.assets[asset.name].source())
5061
}

src/server/webpack-plugin/util.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ export const validate = compiler => {
99
warn('webpack config `target` should be "node".')
1010
}
1111

12-
if (compiler.options.output && compiler.options.output.libraryTarget !== 'commonjs2') {
13-
warn('webpack config `output.libraryTarget` should be "commonjs2".')
12+
if (compiler.options.output) {
13+
// Webpack < 5.0.0
14+
if (compiler.options.output.libraryTarget && compiler.options.output.libraryTarget !== 'commonjs2') {
15+
warn('webpack config `output.libraryTarget` should be "commonjs2".')
16+
}
17+
// Webpack >= 5.0.0
18+
else if (compiler.options.output.library && compiler.options.output.library.type !== 'commonjs2') {
19+
warn('webpack config `output.libraryTarget` should be "commonjs2".')
20+
}
1421
}
1522

1623
if (!compiler.options.externals) {

0 commit comments

Comments
 (0)