Skip to content

fix(ssr): fix compatibility with webpack@4 and webpack@5 for monorepos #12435

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/server/webpack-plugin/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class VueSSRClientPlugin {
if (!chunk || !chunk.files) {
return
}
const id = stripModuleIdHash(m.identifier)
const id = stripModuleIdHash(m.identifier,compiler)
const files = manifest.modules[hash(id)] = chunk.files.map(fileToIndex)
// find all asset modules associated with the same chunk
assetModules.forEach(m => {
Expand Down
15 changes: 7 additions & 8 deletions src/server/webpack-plugin/util.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
const { red, yellow } = require('chalk')
const webpack = require('webpack')

const prefix = `[vue-server-renderer-webpack-plugin]`
const warn = exports.warn = msg => console.error(red(`${prefix} ${msg}\n`))
const tip = exports.tip = msg => console.log(yellow(`${prefix} ${msg}\n`))

const isWebpack5 = !!(webpack.version && webpack.version[0] > 4)

export const validate = compiler => {
if (compiler.options.target !== 'node') {
warn('webpack config `target` should be "node".')
Expand All @@ -31,16 +28,18 @@ export const validate = compiler => {
)
}
}

function isWebpack5(compiler){
return !!compiler.webpack;
}
export const onEmit = (compiler, name, stageName, hook) => {
if (isWebpack5) {
if (isWebpack5(compiler)) {
// Webpack >= 5.0.0
compiler.hooks.compilation.tap(name, compilation => {
if (compilation.compiler !== compiler) {
// Ignore child compilers
return
}
const stage = webpack.Compilation[stageName]
const stage = compiler.webpack.Compilation[stageName]
compilation.hooks.processAssets.tapAsync({ name, stage }, (assets, cb) => {
hook(compilation, cb)
})
Expand All @@ -54,8 +53,8 @@ export const onEmit = (compiler, name, stageName, hook) => {
}
}

export const stripModuleIdHash = id => {
if (isWebpack5) {
export const stripModuleIdHash = (id,compiler) => {
if (isWebpack5(compiler)) {
// Webpack >= 5.0.0
return id.replace(/\|\w+$/, '')
}
Expand Down