forked from metabase/metabase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
266 lines (232 loc) · 9.49 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
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
/* eslint-env node */
/* eslint-disable import/no-commonjs */
require("babel-register");
require("babel-polyfill");
var webpack = require('webpack');
var webpackPostcssTools = require('webpack-postcss-tools');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var UnusedFilesWebpackPlugin = require("unused-files-webpack-plugin").default;
var BannerWebpackPlugin = require('banner-webpack-plugin');
var _ = require('underscore');
var glob = require('glob');
var fs = require('fs');
var chevrotain = require("chevrotain");
var allTokens = require("./frontend/src/metabase/lib/expressions/tokens").allTokens;
function hasArg(arg) {
var regex = new RegExp("^" + ((arg.length === 2) ? ("-\\w*"+arg[1]+"\\w*") : (arg)) + "$");
return process.argv.filter(regex.test.bind(regex)).length > 0;
}
var SRC_PATH = __dirname + '/frontend/src/metabase';
var BUILD_PATH = __dirname + '/resources/frontend_client';
// Need to scan the CSS files for variable and custom media used across files
// NOTE: this requires "webpack -w" (watch mode) to be restarted when variables change :(
var isWatching = hasArg("-w") || hasArg("--watch");
if (isWatching) {
console.warn("Warning: in webpack watch mode you must restart webpack if you change any CSS variables or custom media queries");
}
// default NODE_ENV to development
var NODE_ENV = process.env["NODE_ENV"] || "development";
process.stderr.write("webpack env: " + NODE_ENV + "\n");
// Babel:
var BABEL_CONFIG = {
cacheDirectory: ".babel_cache"
};
// Build mapping of CSS variables
var CSS_SRC = glob.sync(SRC_PATH + '/css/**/*.css');
var CSS_MAPS = { vars: {}, media: {}, selector: {} };
CSS_SRC.map(webpackPostcssTools.makeVarMap).forEach(function(map) {
for (var name in CSS_MAPS) _.extend(CSS_MAPS[name], map[name]);
});
// CSS Next:
var CSSNEXT_CONFIG = {
features: {
// pass in the variables and custom media we scanned for before
customProperties: { variables: CSS_MAPS.vars },
customMedia: { extensions: CSS_MAPS.media }
},
import: {
path: ['resources/frontend_client/app/css']
},
compress: false
};
var CSS_CONFIG = {
localIdentName: NODE_ENV !== "production" ?
"[name]__[local]___[hash:base64:5]" :
"[hash:base64:5]",
restructuring: false,
compatibility: true,
importLoaders: 1
}
var config = module.exports = {
context: SRC_PATH,
// output a bundle for the app JS and a bundle for styles
// eventually we should have multiple (single file) entry points for various pieces of the app to enable code splitting
entry: {
"app-main": './app-main.js',
"app-public": './app-public.js',
"app-embed": './app-embed.js',
styles: './css/index.css',
},
// output to "dist"
output: {
path: BUILD_PATH + '/app/dist',
// NOTE: the filename on disk won't include "?[chunkhash]" but the URL in index.html generated by HtmlWebpackPlugin will:
filename: '[name].bundle.js?[hash]',
publicPath: '/app/dist/'
},
module: {
loaders: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: "babel",
query: BABEL_CONFIG
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules|\.spec\.js/,
loader: 'eslint'
},
{
test: /\.(eot|woff2?|ttf|svg|png)$/,
loader: "file-loader"
},
{
test: /\.json$/,
loader: "json-loader"
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract("style-loader", "css-loader?" + JSON.stringify(CSS_CONFIG) + "!postcss-loader")
}
],
noParse: [
/node_modules\/(ace|moment|underscore)/ // doesn't include 'crossfilter', 'dc', and 'tether' due to use of 'require'
]
},
resolve: {
extensions: ["", ".webpack.js", ".web.js", ".js", ".jsx", ".css"],
alias: {
'metabase': SRC_PATH,
'style': SRC_PATH + '/css/core/index.css',
'ace': __dirname + '/node_modules/ace-builds/src-min-noconflict',
// misc
'moment': __dirname + '/node_modules/moment/min/moment.min.js',
'tether': __dirname + '/node_modules/tether/dist/js/tether.min.js',
'underscore': __dirname + '/node_modules/underscore/underscore-min.js',
'd3': __dirname + '/node_modules/d3/d3.min.js',
'crossfilter': __dirname + '/node_modules/crossfilter/index.js',
'dc': __dirname + '/node_modules/dc/dc.min.js',
'humanize': __dirname + '/node_modules/humanize-plus/dist/humanize.min.js'
}
},
plugins: [
new UnusedFilesWebpackPlugin({
globOptions: {
ignore: [
"**/types/*.js",
"**/*.spec.*"
]
}
}),
// Extracts initial CSS into a standard stylesheet that can be loaded in parallel with JavaScript
// NOTE: the filename on disk won't include "?[chunkhash]" but the URL in index.html generated by HtmlWebpackPlugin will:
new ExtractTextPlugin('[name].bundle.css?[contenthash]'),
new HtmlWebpackPlugin({
filename: '../../index.html',
chunks: ["app-main", "styles"],
template: __dirname + '/resources/frontend_client/index_template.html',
inject: 'head'
}),
new HtmlWebpackPlugin({
filename: '../../public.html',
chunks: ["app-public", "styles"],
template: __dirname + '/resources/frontend_client/index_template.html',
inject: 'head'
}),
new HtmlWebpackPlugin({
filename: '../../embed.html',
chunks: ["app-embed", "styles"],
template: __dirname + '/resources/frontend_client/index_template.html',
inject: 'head'
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(NODE_ENV)
}
}),
new BannerWebpackPlugin({
chunks: {
'app-main': {
beforeContent: "/*\n* This file is subject to the terms and conditions defined in\n * file 'LICENSE.txt', which is part of this source code package.\n */\n",
},
'app-public': {
beforeContent: "/*\n* This file is subject to the terms and conditions defined in\n * file 'LICENSE.txt', which is part of this source code package.\n */\n",
},
'app-embed': {
beforeContent: "/*\n* This file is subject to the terms and conditions defined in\n * file 'LICENSE-EMBEDDING.txt', which is part of this source code package.\n */\n",
},
}
}),
],
postcss: function (webpack) {
return [
require("postcss-import")(),
require("postcss-url")(),
require("postcss-cssnext")(CSSNEXT_CONFIG)
]
}
};
if (NODE_ENV === "hot") {
// suffixing with ".hot" allows us to run both `yarn run build-hot` and `yarn run test` or `yarn run test-watch` simultaneously
config.output.filename = "[name].hot.bundle.js?[hash]";
// point the publicPath (inlined in index.html by HtmlWebpackPlugin) to the hot-reloading server
config.output.publicPath = "http://localhost:8080" + config.output.publicPath;
config.module.loaders.unshift({
test: /\.jsx$/,
exclude: /node_modules/,
loaders: ['react-hot', 'babel?'+JSON.stringify(BABEL_CONFIG)]
});
// disable ExtractTextPlugin
config.module.loaders[config.module.loaders.length - 1].loader = "style-loader!css-loader?" + JSON.stringify(CSS_CONFIG) + "!postcss-loader"
config.devServer = {
hot: true,
inline: true,
contentBase: "frontend"
// if you want to reduce stats noise
// stats: 'minimal' // values: none, errors-only, minimal, normal, verbose
};
config.plugins.unshift(
new webpack.NoErrorsPlugin(),
new webpack.HotModuleReplacementPlugin()
);
}
if (NODE_ENV !== "production") {
// replace minified files with un-minified versions
for (var name in config.resolve.alias) {
var minified = config.resolve.alias[name];
var unminified = minified.replace(/[.-\/]min\b/g, '');
if (minified !== unminified && fs.existsSync(unminified)) {
config.resolve.alias[name] = unminified;
}
}
// enable "cheap" source maps in hot or watch mode since re-build speed overhead is < 1 second
config.devtool = "cheap-module-source-map";
// works with breakpoints
// config.devtool = "inline-source-map"
// helps with source maps
config.output.devtoolModuleFilenameTemplate = '[absolute-resource-path]';
config.output.pathinfo = true;
} else {
// this is required to ensure we don't minify Chevrotain token identifiers
// https://github.com/SAP/chevrotain/tree/master/examples/parser/minification
config.plugins.push(new webpack.optimize.UglifyJsPlugin({
mangle: {
except: allTokens.map(function(currTok) {
return chevrotain.tokenName(currTok);
})
}
}))
config.devtool = "source-map";
}