forked from metabase/metabase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
187 lines (161 loc) · 8.22 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
"use strict";
/*eslint-env node */
var webpack = require('webpack');
var webpackPostcssTools = require('webpack-postcss-tools');
var CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var _ = require('underscore');
var glob = require('glob');
var fs = require('fs');
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';
var BUILD_PATH = __dirname + '/resources/frontend_client';
// All JS files in frontend/src
var JS_SRC = glob.sync(SRC_PATH + '/src/**/*.js');
// All CSS files in frontend/src
var CSS_SRC = glob.sync(SRC_PATH + '/src/**/*.css');
// 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 :(
if (hasArg("-w") || hasArg("--watch")) {
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 production unless -d or --debug is specified
var NODE_ENV = process.env["NODE_ENV"] || (hasArg("-d") || (hasArg("--debug")) ? "development": "production");
console.log("webpack env:", NODE_ENV)
var BABEL_FEATURES = ['es7.asyncFunctions', 'es7.decorators', 'es7.classProperties'];
var cssMaps = { vars: {}, media: {}, selector: {} };
CSS_SRC.map(webpackPostcssTools.makeVarMap).forEach(function(map) {
for (var name in cssMaps) _.extend(cssMaps[name], map[name]);
});
var config = module.exports = {
// 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: {
vendor: SRC_PATH + '/vendor.js',
app: JS_SRC,
styles: [
SRC_PATH + '/vendor.css'
].concat(CSS_SRC)
},
// 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: [
// JavaScript
{ test: /\.(js|jsx)$/, exclude: /node_modules/, loader: 'babel', query: { cacheDirectory: '.babel_cache', optional: BABEL_FEATURES }},
{ test: /\.(js|jsx)$/, exclude: /node_modules|\.spec\.js/, loader: 'eslint' },
// CSS
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style', 'css?-restructuring&compatibility!cssnext') }
// { test: /\.css$/, loader: 'style!css!cssnext' }
],
noParse: [
/node_modules\/(angular|ng-|ace|react-onclickoutside|moment|underscore|d3)/ // doesn't include 'crossfilter', 'dc', and 'tether' due to use of 'require'
]
},
resolve: {
// modulesDirectories: [],
alias: {
'metabase': SRC_PATH + '/src',
// angular
'angular': __dirname + '/node_modules/angular/angular.min.js',
'angular-cookies': __dirname + '/node_modules/angular-cookies/angular-cookies.min.js',
'angular-resource': __dirname + '/node_modules/angular-resource/angular-resource.min.js',
'angular-route': __dirname + '/node_modules/angular-route/angular-route.min.js',
// angular 3rd-party
'angular-ui-bootstrap': __dirname + '/node_modules/angular-ui-bootstrap/ui-bootstrap-tpls.min.js',
'angular-cookie': __dirname + '/node_modules/angular-cookie/angular-cookie.min.js',
'angular-http-auth': __dirname + '/node_modules/angular-http-auth/src/http-auth-interceptor.js',
'angular-ui-ace': __dirname + '/node_modules/angular-ui-ace/src/ui-ace.js',
// ace
'ace/ace': __dirname + '/node_modules/ace-builds/src-min-noconflict/ace.js',
'ace/ext-language_tools':__dirname+ '/node_modules/ace-builds/src-min-noconflict/ext-language_tools.js',
'ace/mode-sql': __dirname + '/node_modules/ace-builds/src-min-noconflict/mode-sql.js',
'ace/snippets/sql': __dirname + '/node_modules/ace-builds/src-min-noconflict/snippets/sql.js',
// react
'react-onclickoutside': __dirname + '/node_modules/react-onclickoutside/index.js',
'fixed-data-table': __dirname + '/node_modules/fixed-data-table/dist/fixed-data-table.min.js',
// 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',
'd3-tip': __dirname + '/node_modules/d3-tip/index.js',
'humanize': __dirname + '/node_modules/humanize-plus/public/src/humanize.js'
}
},
plugins: [
// Automatically annotates angular functions (from "function($foo) {}" to "['$foo', function($foo) {}]")
// so minification doesn't break dependency injections
// new NgAnnotatePlugin({ add: true }),
// Separates out modules common to multiple entry points into a single common file that should be loaded first.
// Not currently useful but necessary for code-splitting
new CommonsChunkPlugin({
name: 'vendor',
minChunks: Infinity // (with more entries, this ensures that no other module goes into the vendor chunk)
}),
// 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',
template: 'resources/frontend_client/index_template.html',
inject: 'head'
}),
new webpack.DefinePlugin({
'process.env': { NODE_ENV: JSON.stringify(NODE_ENV) }
})
],
// CSSNext configuration
cssnext: {
features: {
// pass in the variables and custom media we scanned for before
customProperties: { variables: cssMaps.vars },
customMedia: { extensions: cssMaps.media }
},
import: {
path: ['resources/frontend_client/app/css']
},
compress: false
},
};
if (NODE_ENV === "hot") {
config.entry.app.unshift(
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server'
);
config.output.publicPath = "http://localhost:8080" + config.output.publicPath;
config.module.loaders.unshift(
{ test: /\.jsx$/, exclude: /node_modules/, loaders: ['react-hot', 'babel?'+BABEL_FEATURES.map(function(f) { return 'optional[]='+f; }).join('&')] }
);
config.module.loaders[config.module.loaders.length - 1].loader = 'style!css?-restructuring&compatibility!cssnext';
config.plugins.unshift(
new webpack.NoErrorsPlugin()
);
}
// development environment:
if (NODE_ENV === "development" || NODE_ENV === "hot") {
// 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/, '');
if (minified !== unminified && fs.existsSync(unminified)) {
config.resolve.alias[name] = unminified;
}
}
// SourceMaps
// Normal source map works better but takes longer to build
// config.devtool = 'source-map';
// Eval source map doesn't work with CSS but is faster to build
// config.devtool = 'eval-source-map';
}