1
1
const ManifestPlugin = require ( "webpack-manifest-plugin" ) ;
2
2
const nodeExternals = require ( "webpack-node-externals" ) ;
3
3
4
+ const isClient = process . env . CLIENT === "true" ;
5
+ const isSSR = process . env . SSR === "true" ;
6
+
7
+ /**
8
+ * change the output dir for compiled files
9
+ * https://github.com/vuejs/vue-cli/issues/2202
10
+ */
11
+ if ( isClient ) {
12
+ exports . outputDir = "client-dist" ;
13
+ }
14
+
4
15
exports . chainWebpack = webpackConfig => {
5
- if ( ! process . env . SSR ) {
16
+ if ( ! isSSR ) {
6
17
// This is required for repl.it to play nicely with the Dev Server
7
18
webpackConfig . devServer . disableHostCheck ( true ) ;
8
19
return ;
9
20
}
10
21
22
+ /**
23
+ * By default, vue-cli-service uses "app" as entry point for webpack,
24
+ * and the corresponding entry point file is "./src/main.js".
25
+ *
26
+ * When we compile the file on ssr client side, we just allow "client" as
27
+ * the only entry point for webpack, and the corresponding entry point
28
+ * file is "./src/ssr-client.js", so we should delete "app", otherwise,
29
+ * vue-cli-service will be failed because of the lack of "./src/main.js"
30
+ */
11
31
webpackConfig
12
- . entry ( "app" )
13
- . clear ( )
14
- . add ( "./src/main.server.js" ) ;
32
+ . entryPoints
33
+ . delete ( "app" ) ;
34
+
15
35
16
- webpackConfig . target ( "node" ) ;
17
- webpackConfig . output . libraryTarget ( "commonjs2" ) ;
36
+ if ( isClient ) {
37
+ /**
38
+ * Compile ./src/ssr-client.js, and we will response the compiled file
39
+ * to browser in ./src/server.js.We won't import vue separately, so we
40
+ * also need to bundle vue into the compiled file.
41
+ */
42
+ webpackConfig
43
+ . entry ( "client" )
44
+ . clear ( )
45
+ . add ( "./src/ssr-client.js" ) ;
46
+ } else if ( isSSR ) {
47
+ /**
48
+ * Compile ./src/App.js, so we could require it in ./src/server.js.
49
+ *
50
+ * We already require "vue" in ./src/server.js, so we don't need to
51
+ * bundle it, but we still need to compile and bundle .css or .vue files,
52
+ * so we add `nodeExternals` below.
53
+ */
54
+ webpackConfig
55
+ . entry ( "app" )
56
+ . clear ( )
57
+ . add ( "./src/App.js" ) ;
18
58
59
+ webpackConfig . target ( "node" ) ;
60
+ webpackConfig . output . libraryTarget ( "commonjs2" ) ;
61
+ webpackConfig . externals ( nodeExternals ( { allowlist : / \. ( c s s | v u e ) $ / } ) ) ;
62
+
63
+ } else {
64
+ /**
65
+ * Compile for SPA
66
+ */
67
+ webpackConfig
68
+ . entry ( "app" )
69
+ . clear ( )
70
+ . add ( "./src/spa-client.js" ) ;
71
+ }
72
+
19
73
webpackConfig
20
74
. plugin ( "manifest" )
21
75
. use ( new ManifestPlugin ( { fileName : "ssr-manifest.json" } ) ) ;
22
76
23
- webpackConfig . externals ( nodeExternals ( { allowlist : / \. ( c s s | v u e ) $ / } ) ) ;
77
+
24
78
25
79
webpackConfig . optimization . splitChunks ( false ) . minimize ( false ) ;
26
80
@@ -31,4 +85,4 @@ exports.chainWebpack = webpackConfig => {
31
85
webpackConfig . plugins . delete ( "friendly-errors" ) ;
32
86
33
87
// console.log(webpackConfig.toConfig())
34
- } ;
88
+ } ;
0 commit comments