-
Notifications
You must be signed in to change notification settings - Fork 31
/
webpack.config.js
161 lines (157 loc) · 4.36 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const path = require('path');
const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const entry = './src/entry.jsx';
const outputPath = path.resolve('./dist');
const publicPath = process.env.PUBLIC_PATH || '/';
const resolve = {
extensions: ['.js', '.jsx'],
};
const clientConfig = {
entry,
target: 'web',
devtool: process.env.NODE_ENV == 'production' ? 'nosource-source-map' : 'source-map',
mode: process.env.NODE_ENV == 'production' ? 'production' : 'development',
output: {
path: outputPath,
chunkFilename: '[name].bundle.js',
filename: 'index.bundle.js',
publicPath,
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: require.resolve('babel-loader')
}
},
{
test: /\.(gif|png|jpe?g|svg)$/i,
use: [
'file-loader',
{
loader: 'image-webpack-loader',
options: {
bypassOnDebug: true
}
}
]
}
]
},
resolve,
plugins: [
// Copy all used resources (no dir available)
new CopyWebpackPlugin([
{ from: "assets", to: "assets" },
{ from: "css", to: "css" },
{ from: "public" },
{ from: "index.html", to: "index.html", transform: (content) => {
if (process.env.PUBLIC_PATH) {
let path = process.env.PUBLIC_PATH;
if (path.endsWith("/")) {
path = path.substring(0, path.length - 1);
}
return String(content).replace(/{{CDN}}/g, path);
} else {
// Don`t use CDN
return String(content).replace(/{{CDN}}\//g, "");
}
}
}
]),
// During the build make literal replacements on client side for
// process.env.API_URL, because there is no process.env
new webpack.DefinePlugin({
'process.env.API_URL': JSON.stringify(process.env.API_URL || "http://localhost:3000/api")
}),
]
};
const serverConfig = {
entry,
target: 'node',
devtool: process.env.NODE_ENV == 'production' ? 'nosource-source-map' : 'source-map',
mode: process.env.NODE_ENV == 'production' ? 'production' : 'development',
node: {
__dirname: false
},
output: {
libraryTarget: 'commonjs2',
path: outputPath,
chunkFilename: '[name].server.bundle.js',
filename: 'index.server.bundle.js',
publicPath,
// https://webpack.js.org/configuration/output/#output-strictmoduleexceptionhandling
// - When set to false, the module is not removed from cache, which results in
// the exception getting thrown only on the first require call (making it incompatible with node.js).
strictModuleExceptionHandling: true
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: require.resolve('babel-loader'),
options: {
presets: [
[ '@babel/env', {
targets: {
node: '8.10'
}
}
]
]
}
}
},
{
test: /\.(gif|png|jpe?g|svg)$/i,
use: [
'file-loader',
{
loader: 'image-webpack-loader',
options: {
bypassOnDebug: true
}
}
]
}
]
},
resolve,
plugins: [
// Assume runs last
new CleanWebpackPlugin('dist/*.*'),
// Copy all used resources (no dir available)
new CopyWebpackPlugin([
{ from: "assets", to: "assets" },
{ from: "css", to: "css" },
{ from: "public" },
{ from: "index.html", to: "index.html", transform: (content) => {
if (process.env.PUBLIC_PATH) {
let path = process.env.PUBLIC_PATH;
if (path.endsWith("/")) {
path = path.substring(0, path.length - 1);
}
return String(content).replace(/{{CDN}}/g, path);
} else {
// Don`t use CDN
return String(content).replace(/{{CDN}}\//g, "");
}
}
}
]),
// Limit chunks to 1 effectively disable chunking (used in dynamic imports)
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1
})
]
};
module.exports = [
clientConfig,
serverConfig,
];