Newer
Older
require("babel-register");
require("babel-polyfill");
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 UnusedFilesWebpackPlugin = require("unused-files-webpack-plugin").default;
var _ = require('underscore');
var glob = require('glob');
Tom Robinson
committed
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");
Tom Robinson
committed
}
// default NODE_ENV to development
var NODE_ENV = process.env["NODE_ENV"] || "development";
Tom Robinson
committed
// 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]);
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
};
Tom Robinson
committed
localIdentName: NODE_ENV !== "production" ?
"[name]__[local]___[hash:base64:5]" :
"[hash:base64:5]",
Tom Robinson
committed
compatibility: true,
importLoaders: 1
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
vendor: './vendor.js',
app: './app.js',
// NOTE: the filename on disk won't include "?[chunkhash]" but the URL in index.html generated by HtmlWebpackPlugin will:
publicPath: '/app/dist/'
{
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)$/,
{
test: /\.json$/,
loader: "json-loader"
},
loader: ExtractTextPlugin.extract("style-loader", "css-loader?" + JSON.stringify(CSS_CONFIG) + "!postcss-loader")
/node_modules\/(ace|moment|underscore)/ // doesn't include 'crossfilter', 'dc', and 'tether' due to use of 'require'
extensions: ["", ".webpack.js", ".web.js", ".js", ".jsx", ".css"],
'metabase': SRC_PATH,
Tom Robinson
committed
'style': SRC_PATH + '/css/core/index.css',
Tom Robinson
committed
// 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',
Tom Robinson
committed
'ace/mode-sql': __dirname + '/node_modules/ace-builds/src-min-noconflict/mode-sql.js',
'ace/mode-mysql': __dirname + '/node_modules/ace-builds/src-min-noconflict/mode-mysql.js',
'ace/mode-pgsql': __dirname + '/node_modules/ace-builds/src-min-noconflict/mode-pgsql.js',
'ace/mode-sqlserver': __dirname + '/node_modules/ace-builds/src-min-noconflict/mode-sqlserver.js',
'ace/mode-json': __dirname + '/node_modules/ace-builds/src-min-noconflict/mode-json.js',
Tom Robinson
committed
'ace/snippets/sql': __dirname + '/node_modules/ace-builds/src-min-noconflict/snippets/sql.js',
'ace/snippets/mysql': __dirname + '/node_modules/ace-builds/src-min-noconflict/snippets/mysql.js',
'ace/snippets/pgsql': __dirname + '/node_modules/ace-builds/src-min-noconflict/snippets/pgsql.js',
'ace/snippets/sqlserver': __dirname + '/node_modules/ace-builds/src-min-noconflict/snippets/sqlserver.js',
'ace/snippets/json': __dirname + '/node_modules/ace-builds/src-min-noconflict/snippets/json.js',
Tom Robinson
committed
// misc
Tom Robinson
committed
'moment': __dirname + '/node_modules/moment/min/moment.min.js',
'tether': __dirname + '/node_modules/tether/dist/js/tether.min.js',
Tom Robinson
committed
'underscore': __dirname + '/node_modules/underscore/underscore-min.js',
'd3': __dirname + '/node_modules/d3/d3.min.js',
'crossfilter': __dirname + '/node_modules/crossfilter/index.js',
Tom Robinson
committed
'dc': __dirname + '/node_modules/dc/dc.min.js',
'humanize': __dirname + '/node_modules/humanize-plus/dist/humanize.min.js'
new UnusedFilesWebpackPlugin({
globOptions: {
ignore: [
"**/types/*.js"
]
}
}),
// 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
Tom Robinson
committed
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({
Tom Robinson
committed
filename: '../../index.html',
template: __dirname + '/resources/frontend_client/index_template.html',
Tom Robinson
committed
inject: 'head'
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(NODE_ENV)
}
postcss: function (webpack) {
return [
require("postcss-import")(),
require("postcss-url")(),
require("postcss-cssnext")(CSSNEXT_CONFIG)
]
}
Tom Robinson
committed
};
Tom Robinson
committed
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)]
});
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"
};
new webpack.NoErrorsPlugin(),
new webpack.HotModuleReplacementPlugin()
if (NODE_ENV !== "production") {
Tom Robinson
committed
// 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, '');
Tom Robinson
committed
if (minified !== unminified && fs.existsSync(unminified)) {
Tom Robinson
committed
}
}
// enable "cheap" source maps in hot or watch mode since re-build speed overhead is < 1 second
Tom Robinson
committed
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);
})