-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
135 lines (125 loc) · 3.88 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const NodeExternals = require('webpack-node-externals')
const webpack = require('webpack')
const PUBLIC_DIR = path.resolve(__dirname, 'public')
// const PUBLIC_URL = '/ssr'
const PUBLIC_URL = '/'
const BUILD_CLIENT_DIR = path.resolve(__dirname, 'dist_client')
const BUILD_SERVER_DIR = path.resolve(__dirname, 'dist_server')
const CommonConfig = (env, args) => {
const IS_DEBUG = args.mode === 'development'
return {
devtool: IS_DEBUG ? 'cheap-module-source-map' : '',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: /\.(png|svg|jpg|gif|ico)$/,
include: [
path.resolve(__dirname, 'public'),
BUILD_CLIENT_DIR
],
loader: [
'file-loader'
]
}
]
},
resolve: {
alias: {
'@material-ui/core': '@material-ui/core/es'
}
},
plugins: [
new webpack.EnvironmentPlugin({
NODE_ENV: args.mode,
PUBLIC_DIR: PUBLIC_DIR,
PUBLIC_URL: PUBLIC_URL,
BUILD_CLIENT_DIR: BUILD_CLIENT_DIR,
BUILD_SERVER_DIR: BUILD_SERVER_DIR
})
],
devServer: {
contentBase: BUILD_CLIENT_DIR
}
}
}
const ClientConfig = (env, args) => {
const common = CommonConfig(env, args)
return {
...common,
plugins: [
...common.plugins,
new CleanWebpackPlugin(),
new CopyPlugin([
{
from: PUBLIC_DIR,
to: BUILD_CLIENT_DIR,
globOptions: {
ignore: ['**/index.html'],
dot: true,
gitignore: true,
ignore: ['**/.*'],
},
// Add the recursive option to copy all resources in the child folder
options: {
recursive: true
}
}
]),
new HtmlWebpackPlugin({
title: 'Movies Trailer',
favicon: path.join(PUBLIC_DIR, './favicon.ico'),
template: 'index.template.html',
filename: path.join(BUILD_CLIENT_DIR, '/index.html')
}),
new webpack.DefinePlugin({
__isBrowser__: "true"
})
],
entry: './src/client',
output: {
path: BUILD_CLIENT_DIR,
publicPath: PUBLIC_URL
}
}
}
const ServerConfig = (env, args) => {
const common = CommonConfig(env, args)
return {
...common,
plugins: [
...common.plugins,
new CleanWebpackPlugin(),
new webpack.DefinePlugin({
__isBrowser__: "false"
})
],
target: 'node',
entry: './src/server',
output: {
path: BUILD_SERVER_DIR,
publicPath: PUBLIC_URL
},
externals: [NodeExternals({
whitelist: [
/@material-ui\/core\/*./
]
})]
}
}
module.exports = (env, args) => {
let configs = [
ClientConfig(env, args),
ServerConfig(env, args)
]
// console.log('configs', configs)
return configs
}