-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
webpack.config.js
174 lines (150 loc) · 4.23 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
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
const {resolve, join} = require('path');
const webpack = require('webpack');
const KeplerPackage = require('../package');
const {
WEBPACK_ENV_VARIABLES,
ENV_VARIABLES_WITH_INSTRUCTIONS,
RESOLVE_ALIASES
} = require('../webpack/shared-webpack-configuration');
const LIB_DIR = resolve(__dirname, '..');
const SRC_DIR = resolve(LIB_DIR, './src');
const console = require('global/console');
const BABEL_CONFIG = {
presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'],
plugins: [
['@babel/plugin-transform-typescript', {isTSX: true, allowDeclareFields: true}],
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-export-namespace-from',
'@babel/plugin-proposal-optional-chaining',
[
'search-and-replace',
{
rules: [
{
search: '__PACKAGE_VERSION__',
replace: KeplerPackage.version
}
]
}
]
]
};
const COMMON_CONFIG = {
entry: ['./src/main'],
output: {
path: resolve(__dirname, 'build'),
filename: 'bundle.js',
publicPath: '/'
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
modules: ['node_modules', SRC_DIR],
alias: RESOLVE_ALIASES
},
module: {
rules: [
{
// Compile ES2015 using bable
test: /\.(js|jsx|ts|tsx)$/,
loader: 'babel-loader',
options: BABEL_CONFIG,
exclude: [/node_modules/]
},
{
test: /\.(eot|svg|ico|ttf|woff|woff2|gif|jpe?g|png)$/,
loader: 'url-loader'
},
{
test: /\.(svg|ico|gif|jpe?g|png)$/,
loader: 'file-loader?name=[name].[ext]'
},
// for compiling apache-arrow ESM module
{
test: /\.mjs$/,
include: /node_modules\/apache-arrow/,
type: 'javascript/auto'
},
// for compiling @probe.gl, website build started to fail (March, 2024)
// netlify biulder complains loader not found for these modules (April, 2024)
{
test: /\.(js)$/,
loader: 'babel-loader',
options: BABEL_CONFIG,
include: [/node_modules\/@probe.gl/, /node_modules\/@loaders.gl/, /node_modules\/@math.gl/]
}
]
},
node: {
fs: 'empty'
},
// to support browser history api and remove the '#' sign
devServer: {
historyApiFallback: true
},
// Optional: Enables reading mapbox token from environment variable
plugins: [
// Provide default values to suppress warnings
new webpack.EnvironmentPlugin(WEBPACK_ENV_VARIABLES)
],
// Required to avoid deck.gl undefined module when code is minified
optimization: {
concatenateModules: false,
providedExports: false,
usedExports: false
}
};
const addDevConfig = config => {
config.module.rules.push({
// Unfortunately, webpack doesn't import library sourcemaps on its own...
test: /\.js$/,
use: ['source-map-loader'],
enforce: 'pre',
exclude: [/node_modules\/react-palm/, /node_modules\/react-data-grid/]
});
return Object.assign(config, {
devtool: 'source-maps',
plugins: config.plugins.concat([
// new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()
])
});
};
const addProdConfig = config => {
return Object.assign(config, {
output: {
path: resolve(__dirname, './dist'),
filename: 'bundle.js'
}
});
};
function logError(msg) {
console.log('\x1b[31m%s\x1b[0m', msg);
}
function logInstruction(msg) {
console.log('\x1b[36m%s\x1b[0m', msg);
}
function validateEnvVariable(variable, instruction) {
if (!process.env[variable]) {
logError(`Error! ${variable} is not defined`);
logInstruction(`Make sure to run "export ${variable}=<token>" before deploy the website`);
logInstruction(instruction);
throw new Error(`Missing ${variable}`);
}
}
module.exports = env => {
env = env || {};
let config = COMMON_CONFIG;
if (env.local) {
config = addDevConfig(config);
}
if (env.prod) {
Object.entries(ENV_VARIABLES_WITH_INSTRUCTIONS).forEach(entry => {
// we validate each entry [name, instruction]
validateEnvVariable(...entry);
});
config = addProdConfig(config);
}
return config;
};