-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.ts
More file actions
executable file
·268 lines (255 loc) · 8.73 KB
/
webpack.config.ts
File metadata and controls
executable file
·268 lines (255 loc) · 8.73 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import * as webpack from "webpack";
import * as HtmlWebpackPlugin from "html-webpack-plugin";
import * as path from "path";
import * as ExtractTextPlugin from "extract-text-webpack-plugin";
import * as CopyWebpackPlugin from "copy-webpack-plugin";
import * as autoprefixer from "autoprefixer";
import { JSONObject } from "src/json";
/**
* Interface for supported "env" properties, which come from "--env.[propname]=[value]" command-line arguments.
*/
interface Env {
/**
* Treated as a boolean: any non-empty value == true.
* True to build for production (generates dist files).
* False to build for development (for use with webpack-dev-server with hot module reloading)
*/
prod?: string;
/**
* Treated as a boolean: any non-empty value == true.
* True to generate source maps for debugging.
* Only relevant if {@link #prod} is true.
* Source maps are ALWAYS generated when {@link #prod} is false.
*/
sourceMap?: string;
/**
* Base URL for API requests.
* If present, this overrides the default URL from the swagger document for this API.
*/
apiBaseUrl?: string;
/**
* Base URL for Vehicle API requests.
* If present, this overrides the default URL from the swagger document for this API.
*/
vehicleApiBaseUrl?: string;
/**
* Treated as a boolean: any non-empty value == true.
* True to build the sandbox application instead of the main web application.
*/
sandbox?: string;
}
/**
* Various file path constants.
* All values are absolute paths.
*/
const PATHS = {
/**
* Project root.
*/
root: path.resolve(__dirname, "./"),
/**
* "node_modules" folder.
*/
nodeModules: path.resolve(__dirname, "./node_modules"),
/**
* Project source folder.
*/
src: path.resolve(__dirname, "./src"),
/**
* Folder containing public static assets that should be copied directly to the
* {@link #dist}/{@link URL_PATHS.publicStatic} folder
*/
publicStatic: path.resolve(__dirname, "./public"),
/**
* Build output folder.
*/
dist: path.resolve(__dirname, "./dist")
};
/**
* Absolute URL paths.
*/
const URL_PATHS = {
/**
* Path to location of public static files to be served.
*/
publicStatic: "/public"
};
/**
* config for webpack-dev-server
*/
const DEV_SERVER = {
port: 8000,
hot: true,
hotOnly: false,
overlay: true,
historyApiFallback: true
};
// Default export is necessary for webpack.config.ts
// tslint:disable-next-line:no-default-export
export default (env: Env = {}) => {
const isSandbox = !!env.sandbox;
const isDev = !env.prod;
const isProd = !isDev;
const isSourceMap = isDev || !!env.sourceMap;
const packageJson: JSONObject = require(PATHS.root + "/package.json");
return {
cache: true,
devtool: isSourceMap
? isDev
? "eval-source-map"
: "source-map"
: false,
devServer: DEV_SERVER,
context: PATHS.root,
entry: [
...(isDev ? ["react-hot-loader/patch"] : []),
isSandbox ? "./src/sandbox.tsx" : "./src/index.tsx"
],
output: {
filename: isDev ? "[name].js" : "[name].[chunkhash].js",
path: PATHS.dist,
publicPath: "/"
},
resolve: {
alias: {
src: PATHS.src
},
extensions: [".ts", ".tsx", ".js", ".jsx", ".json", ".scss"]
},
module: {
rules: [
// CSS/SCSS
{
test: /\.s?css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{
loader: "css-loader",
options: {
sourceMap: isSourceMap,
importLoaders: 2,
minimize: true
}
},
{
loader: "postcss-loader",
options: {
sourceMap: isSourceMap,
ident: "postcss",
plugins: [autoprefixer()]
}
},
{
loader: "sass-loader",
options: {
sourceMap: isSourceMap
}
}
]
})
},
// image/font files included by CSS
{
test: /\.(woff|woff2|eot|ttf|svg|png|jpg|gif)$/,
use: [
{
loader: "url-loader",
options: {
limit: 1024
}
}
]
},
// typescript
{
test: /\.tsx?$/,
include: PATHS.src,
use: [
...(isDev ? ["react-hot-loader/webpack"] : []),
{
loader: "awesome-typescript-loader",
options: {
transpileOnly: true,
compilerOptions: {
sourceMap: isSourceMap,
target: "es5",
isolatedModules: true,
noEmitOnError: false
}
}
}
]
},
// json
{
test: /\.json$/,
include: PATHS.src,
use: ["json-loader"]
}
]
},
plugins: [
new HtmlWebpackPlugin({
title: isSandbox ? "The Sandbox" : "PDS Web App",
template: PATHS.src + "/index.ejs",
publicPath: URL_PATHS.publicStatic
}),
new ExtractTextPlugin({
filename: "[name].[hash].css",
allChunks: true,
disable: isDev
}),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify(
isDev ? "development" : "production"
),
API_BASE_URL: JSON.stringify(env.apiBaseUrl),
VEHICLE_API_BASE_URL: JSON.stringify(env.vehicleApiBaseUrl),
AG_GRID_LICENSE_KEY: JSON.stringify(
"Control-Tec_PDS_Web_App_4Devs9_March_2019__" +
"MTU1MjA4OTYwMDAwMA==137d993909f4bf81fd198c6c37ee3d03"
),
WEB_APP_VERSION: JSON.stringify(packageJson.version),
WEB_APP_COPYRIGHT_YEAR: JSON.stringify(String(2017))
}
}),
new webpack.optimize.CommonsChunkPlugin({
name: "vendor",
minChunks: (module) =>
module.context &&
module.context.indexOf("node_modules") !== -1
}),
new webpack.optimize.CommonsChunkPlugin({
name: "manifest"
}),
...(isDev
? [
new webpack.HotModuleReplacementPlugin({
// NOTE: cannot use multiStep until this bug is fixed:
// https://github.com/jantimon/html-webpack-plugin/issues/716
// multiStep: true // better performance with many files
}),
new webpack.NamedModulesPlugin()
]
: []),
...(isProd
? [
new CopyWebpackPlugin([
{
from: PATHS.publicStatic,
to: PATHS.dist + URL_PATHS.publicStatic
}
]),
new webpack.optimize.UglifyJsPlugin({
beautify: false,
compress: true,
comments: false,
sourceMap: isSourceMap
})
]
: [])
]
};
};