Skip to content

Commit

Permalink
fix(ssr): vue-ssr-webpack-plugin compatible with webpack 4/5
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Jan 12, 2021
1 parent f7ddd26 commit 8468aeb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/server/template-renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export default class TemplateRenderer {
if (this.clientManifest) {
const initial = this.preloadFiles.filter(({ file }) => isJS(file))
const async = (this.getUsedAsyncFiles(context) || []).filter(({ file }) => isJS(file))
const needed = [initial[0]].concat(async, initial.slice(1))
const needed = [].concat(initial[0] ? [initial[0]] : [], async, initial.slice(1));
return needed.map(({ file }) => {
return `<script src="${this.publicPath}${file}" defer></script>`
}).join('')
Expand Down
8 changes: 8 additions & 0 deletions src/server/webpack-plugin/client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const hash = require('hash-sum')
const uniq = require('lodash.uniq')
import { isJS, isCSS, onEmit } from './util'
import { isObject } from 'shared/util'

export default class VueSSRClientPlugin {
constructor (options = {}) {
Expand All @@ -19,6 +20,13 @@ export default class VueSSRClientPlugin {
const initialFiles = uniq(Object.keys(stats.entrypoints)
.map(name => stats.entrypoints[name].assets)
.reduce((assets, all) => all.concat(assets), [])
.map(file => {
if (isObject(file) && file.name) {
return file.name;
} else {
return file;
}
})
.filter((file) => isJS(file) || isCSS(file)))

const asyncFiles = allFiles
Expand Down
13 changes: 12 additions & 1 deletion src/server/webpack-plugin/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { validate, isJS, onEmit } from './util'
import { isObject } from 'shared/util'

export default class VueSSRServerPlugin {
constructor (options = {}) {
Expand All @@ -20,7 +21,14 @@ export default class VueSSRServerPlugin {
return cb()
}

const entryAssets = entryInfo.assets.filter(isJS)
const entryAssets = entryInfo.assets
.map(file => {
if ( isObject(file) && file.name) {
return file.name;
} else {
return file;
}
}).filter(isJS)

if (entryAssets.length > 1) {
throw new Error(
Expand All @@ -45,6 +53,9 @@ export default class VueSSRServerPlugin {
stats.assets.forEach(asset => {
if (isJS(asset.name)) {
bundle.files[asset.name] = compilation.assets[asset.name].source()
if (asset.info && asset.info.related && asset.info.related.sourceMap) {
bundle.maps[asset.info.related.sourceMap.replace(/\.map$/, '')] = JSON.parse(compilation.assets[asset.info.related.sourceMap].source());
}
} else if (asset.name.match(/\.js\.map$/)) {
bundle.maps[asset.name.replace(/\.map$/, '')] = JSON.parse(compilation.assets[asset.name].source())
}
Expand Down
11 changes: 9 additions & 2 deletions src/server/webpack-plugin/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ export const validate = compiler => {
warn('webpack config `target` should be "node".')
}

if (compiler.options.output && compiler.options.output.libraryTarget !== 'commonjs2') {
warn('webpack config `output.libraryTarget` should be "commonjs2".')
if (compiler.options.output) {
// Webpack < 5.0.0
if (compiler.options.output.libraryTarget && compiler.options.output.libraryTarget !== 'commonjs2') {
warn('webpack config `output.libraryTarget` should be "commonjs2".')
}
// Webpack >= 5.0.0
else if (compiler.options.output.library && compiler.options.output.library.type !== 'commonjs2') {
warn('webpack config `output.libraryTarget` should be "commonjs2".')

This comment has been minimized.

Copy link
@tux2nicolae

tux2nicolae Jan 18, 2021

You forgot to update the warning message

}
}

if (!compiler.options.externals) {
Expand Down

0 comments on commit 8468aeb

Please sign in to comment.