/
webpack.config.js
202 lines (171 loc) · 4.31 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
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
const {resolve} = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const ALIASES = require('../aliases')('src');
const TEST_CONFIG = {
mode: 'development',
devServer: {
stats: {
warnings: false
},
quiet: true
},
// Generate a bundle in dist folder
output: {
path: resolve('./dist'),
filename: 'bundle.js'
},
resolve: {
alias: ALIASES
},
devtool: '#inline-source-maps',
module: {
rules: [
{
// Unfortunately, webpack doesn't import library sourcemaps on its own...
test: /\.js$/,
use: ['source-map-loader'],
enforce: 'pre'
}
]
},
node: {
fs: 'empty'
},
plugins: [new HtmlWebpackPlugin({title: 'deck.gl tests'})]
};
// Get first key in an object
function getFirstKey(object) {
for (const key in object) {
return key;
}
return null;
}
// Hack: first key is app
function getApp(env) {
return getFirstKey(env);
}
function getDist(env) {
if (env.esm) {
return 'esm';
}
if (env.es5) {
return 'es5';
}
return 'es6';
}
const CONFIGS = {
test: env =>
Object.assign({}, TEST_CONFIG, {
// Bundle the tests for running in the browser
entry: {
'test-browser': resolve('./test/test-browser.js')
}
// plugins: [new HtmlWebpackPlugin()]
}),
bench: env =>
Object.assign({}, TEST_CONFIG, {
entry: {
'test-browser': resolve('./test/bench/browser.js')
},
plugins: [new HtmlWebpackPlugin()]
}),
render: env =>
Object.assign({}, TEST_CONFIG, {
// Bundle the tests for running in the browser
entry: {
'test-browser': resolve('./test/render/test-rendering.js')
},
plugins: [new HtmlWebpackPlugin()]
}),
renderReact: env =>
Object.assign({}, TEST_CONFIG, {
// Bundle the tests for running in the browser
entry: {
'test-browser': resolve('./test/render/old/test-rendering.react.js')
},
plugins: [new HtmlWebpackPlugin()]
}),
size: env => {
const dist = getDist(env);
const config = Object.assign({}, TEST_CONFIG, {
resolve: {
alias: ALIASES
}
});
if (dist === 'es6') {
resolve.mainFields = ['esnext', 'browser', 'module', 'main'];
}
if (dist === 'es5') {
resolve.mainFields = ['main'];
}
return config;
},
bundle: env => {
const app = getApp(env);
const config = CONFIGS.size(env);
Object.assign(config, {
mode: 'production',
// Replace the entry point for webpack-dev-server
entry: {
'test-browser': resolve(__dirname, './size', `${app}.js`)
},
output: {
path: resolve('/tmp'),
filename: 'bundle.js'
},
resolve: {
alias: ALIASES
},
plugins: [
// leave minification to app
// new webpack.optimize.UglifyJsPlugin({comments: false})
new webpack.DefinePlugin({NODE_ENV: JSON.stringify('production')})
]
});
const dist = getDist(env);
if (dist === 'es6') {
config.resolve.mainFields = ['esnext', 'browser', 'module', 'main'];
}
if (dist === 'es5') {
config.resolve.mainFields = ['main'];
}
delete config.devtool;
return config;
},
// Bundles a test app for size analysis and starts the webpack bundle analyzer
analyze: env => {
const config = CONFIGS.bundle(env);
config.plugins.push(new BundleAnalyzerPlugin());
return config;
}
};
// Pick a webpack config based on --env.*** argument to webpack
function getConfig(env) {
if (env.test || env.testBrowser || env.test_browser) {
return CONFIGS.test(env);
}
if (env.render) {
return CONFIGS.render(env);
}
if (env.renderReact || env.render_react) {
return CONFIGS.renderReact(env);
}
if (env.bench) {
return CONFIGS.bench(env);
}
if (env.analyze) {
return CONFIGS.analyze(env);
}
return CONFIGS.bundle(env);
}
module.exports = (env = {}) => {
// env = getEnv(env);
// NOTE uncomment to display env
// console.log('webpack env', JSON.stringify(env));
const config = getConfig(env);
// NOTE uncomment to display config
// console.log('webpack config', JSON.stringify(config));
return config;
};