|
2 | 2 |
|
3 | 3 | const webpack = require('webpack');
|
4 | 4 | const HtmlWebpackPlugin = require('html-webpack-plugin');
|
| 5 | +const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin'); |
5 | 6 | const webpackConfigFactory = require('./webpack.config.js');
|
6 | 7 | const paths = require('./paths');
|
| 8 | +const getClientEnvironment = require('./env'); |
| 9 | +const env = getClientEnvironment(''); |
7 | 10 |
|
8 | 11 | // decorate original webpack config
|
9 | 12 | module.exports = function (webpackEnv) {
|
10 | 13 | const template = webpackConfigFactory(webpackEnv);
|
11 |
| - return Object.assign( |
12 |
| - // multi compiler config (client + server) |
13 |
| - [ |
14 |
| - // ssr compiler config |
15 |
| - { |
16 |
| - ...template, |
17 |
| - name: 'ssr', |
18 |
| - target: 'node', |
19 |
| - entry: [ |
20 |
| - // ssr entry point, usually s.th. like `./src/index.ssr.tsx` with `export default (req,res) => {}` |
21 |
| - paths.appIndexSsrJs |
22 |
| - ], |
23 |
| - output: { |
24 |
| - ...template.output, |
25 |
| - filename: 'ssr.js', |
26 |
| - libraryTarget: 'commonjs2', |
27 |
| - // avoid using absolute path '/' defined by template |
28 |
| - publicPath: '' |
29 |
| - }, |
30 |
| - optimization: { |
31 |
| - ...template.optimization, |
32 |
| - // disable chunk splitting |
33 |
| - splitChunks: {}, |
34 |
| - runtimeChunk: false |
35 |
| - }, |
36 |
| - // filter out some plugins |
37 |
| - plugins: template.plugins.filter(plugin => |
38 |
| - [ |
39 |
| - // do not generate an additional index.html for ssr |
40 |
| - HtmlWebpackPlugin, |
41 |
| - // Avoid shadowing of process.env for ssr handler |
42 |
| - webpack.DefinePlugin |
43 |
| - ].every(pluginClass => !(plugin instanceof pluginClass)) |
44 |
| - ), |
45 |
| - // do not bundle express |
46 |
| - externals: [ |
47 |
| - "express" |
48 |
| - ] |
| 14 | + // multi compiler config (client + server) |
| 15 | + return [ |
| 16 | + // ssr compiler config |
| 17 | + { |
| 18 | + ...template, |
| 19 | + name: 'ssr', |
| 20 | + target: 'node', |
| 21 | + entry: [ |
| 22 | + // ssr entry point, usually s.th. like `./src/index.ssr.tsx` with `export default (req,res) => {}` |
| 23 | + paths.appIndexSsrJs |
| 24 | + ], |
| 25 | + output: { |
| 26 | + ...template.output, |
| 27 | + filename: 'ssr.js', |
| 28 | + libraryTarget: 'commonjs2', |
| 29 | + // use '' instead of '/' to make urls relative to base href |
| 30 | + publicPath: '' |
49 | 31 | },
|
50 |
| - // client compiler config |
51 |
| - { |
52 |
| - // use original config |
53 |
| - ...template, |
54 |
| - name: 'client' |
| 32 | + optimization: { |
| 33 | + ...template.optimization, |
| 34 | + // disable chunk splitting |
| 35 | + splitChunks: {}, |
| 36 | + runtimeChunk: false |
55 | 37 | },
|
56 |
| - ], |
57 |
| - // workarounds |
| 38 | + // filter out some plugins |
| 39 | + plugins: template.plugins.filter(plugin => |
| 40 | + [ |
| 41 | + // do not generate an additional index.html for ssr |
| 42 | + HtmlWebpackPlugin, |
| 43 | + // avoid shadowing of process.env for ssr handler |
| 44 | + webpack.DefinePlugin |
| 45 | + ].every(pluginClass => !(plugin instanceof pluginClass)) |
| 46 | + ) |
| 47 | + }, |
| 48 | + // client compiler config |
58 | 49 | {
|
59 |
| - // at least one script expects to find `config.output.publicPath` but `config` is now an array |
| 50 | + // use original config |
| 51 | + ...template, |
| 52 | + name: 'client', |
60 | 53 | output: {
|
61 |
| - publicPath: template.output.publicPath |
62 |
| - } |
63 |
| - } |
64 |
| - ); |
| 54 | + ...template.output, |
| 55 | + // use '' instead of '/' to make urls relative to base href |
| 56 | + publicPath: '' |
| 57 | + }, |
| 58 | + // filter out some plugins |
| 59 | + plugins: template.plugins.filter(plugin => |
| 60 | + [ |
| 61 | + InterpolateHtmlPlugin, |
| 62 | + // filter out original DefinePlugin |
| 63 | + webpack.DefinePlugin |
| 64 | + ].every(pluginClass => !(plugin instanceof pluginClass)) |
| 65 | + ).concat([ |
| 66 | + // redefine process.env values on the client |
| 67 | + new webpack.DefinePlugin({ |
| 68 | + 'process.env': { |
| 69 | + // use existing values |
| 70 | + ...env.stringified['process.env'], |
| 71 | + // override with base href |
| 72 | + PUBLIC_URL: 'document.getElementsByTagName("base")[0].href' |
| 73 | + } |
| 74 | + }), |
| 75 | + ]) |
| 76 | + }, |
| 77 | + ]; |
65 | 78 | };
|
0 commit comments