11const { red, yellow } = require ( 'chalk' )
2+ const webpack = require ( 'webpack' )
23
34const prefix = `[vue-server-renderer-webpack-plugin]`
45const warn = exports . warn = msg => console . error ( red ( `${ prefix } ${ msg } \n` ) )
56const tip = exports . tip = msg => console . log ( yellow ( `${ prefix } ${ msg } \n` ) )
67
8+ const isWebpack5 = ! ! ( webpack . version && webpack . version [ 0 ] > 4 )
9+
710export const validate = compiler => {
811 if ( compiler . options . target !== 'node' ) {
912 warn ( 'webpack config `target` should be "node".' )
1013 }
1114
12- if ( compiler . options . output && compiler . options . output . libraryTarget !== 'commonjs2' ) {
13- warn ( 'webpack config `output.libraryTarget` should be "commonjs2".' )
15+ if ( compiler . options . output ) {
16+ if ( compiler . options . output . library ) {
17+ // Webpack >= 5.0.0
18+ if ( compiler . options . output . library . type !== 'commonjs2' ) {
19+ warn ( 'webpack config `output.library.type` should be "commonjs2".' )
20+ }
21+ } else if ( compiler . options . output . libraryTarget !== 'commonjs2' ) {
22+ // Webpack < 5.0.0
23+ warn ( 'webpack config `output.libraryTarget` should be "commonjs2".' )
24+ }
1425 }
1526
1627 if ( ! compiler . options . externals ) {
@@ -21,8 +32,20 @@ export const validate = compiler => {
2132 }
2233}
2334
24- export const onEmit = ( compiler , name , hook ) => {
25- if ( compiler . hooks ) {
35+ export const onEmit = ( compiler , name , stageName , hook ) => {
36+ if ( isWebpack5 ) {
37+ // Webpack >= 5.0.0
38+ compiler . hooks . compilation . tap ( name , compilation => {
39+ if ( compilation . compiler !== compiler ) {
40+ // Ignore child compilers
41+ return
42+ }
43+ const stage = webpack . Compilation [ stageName ]
44+ compilation . hooks . processAssets . tapAsync ( { name, stage } , ( assets , cb ) => {
45+ hook ( compilation , cb )
46+ } )
47+ } )
48+ } else if ( compiler . hooks ) {
2649 // Webpack >= 4.0.0
2750 compiler . hooks . emit . tapAsync ( name , hook )
2851 } else {
@@ -31,4 +54,20 @@ export const onEmit = (compiler, name, hook) => {
3154 }
3255}
3356
57+ export const stripModuleIdHash = id => {
58+ if ( isWebpack5 ) {
59+ // Webpack >= 5.0.0
60+ return id . replace ( / \| \w + $ / , '' )
61+ }
62+ // Webpack < 5.0.0
63+ return id . replace ( / \s \w + $ / , '' )
64+ }
65+
66+ export const getAssetName = asset => {
67+ if ( typeof asset === 'string' ) {
68+ return asset
69+ }
70+ return asset . name
71+ }
72+
3473export { isJS , isCSS } from '../util'
0 commit comments