1
1
const { red, yellow } = require ( 'chalk' )
2
+ const webpack = require ( 'webpack' )
2
3
3
4
const prefix = `[vue-server-renderer-webpack-plugin]`
4
5
const warn = exports . warn = msg => console . error ( red ( `${ prefix } ${ msg } \n` ) )
5
6
const tip = exports . tip = msg => console . log ( yellow ( `${ prefix } ${ msg } \n` ) )
6
7
8
+ const isWebpack5 = ! ! ( webpack . version && webpack . version [ 0 ] > 4 )
9
+
7
10
export const validate = compiler => {
8
11
if ( compiler . options . target !== 'node' ) {
9
12
warn ( 'webpack config `target` should be "node".' )
10
13
}
11
14
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
+ }
14
25
}
15
26
16
27
if ( ! compiler . options . externals ) {
@@ -21,8 +32,20 @@ export const validate = compiler => {
21
32
}
22
33
}
23
34
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 ) {
26
49
// Webpack >= 4.0.0
27
50
compiler . hooks . emit . tapAsync ( name , hook )
28
51
} else {
@@ -31,4 +54,20 @@ export const onEmit = (compiler, name, hook) => {
31
54
}
32
55
}
33
56
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
+
34
73
export { isJS , isCSS } from '../util'
0 commit comments