Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): vue-ssr-webpack-plugin compatible with webpack 4/5 #11863

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/server/template-renderer/index.js
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
@@ -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
@@ -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
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".')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

output.library.type

Copy link

@tibineagu tibineagu Mar 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blake-newman According to the webpack 5 documentation, I dont think output.library.type is what's required in the webpack config.

The config supports output.libraryTarget: 'commonjs2' as until now.

Indeed, the logic here should read the value from library.type (I haven't checked, trusting the author on this one), but the warning to users needs to mention the proper webpack config - which is still output.libraryTarget.

}
}

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