Newer
Older
var webpack = require('webpack');
var webpackPostcssTools = require('webpack-postcss-tools');
var CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;
var NgAnnotatePlugin = require('ng-annotate-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var _ = require('underscore');
var glob = require('glob');
Tom Robinson
committed
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 BASE_PATH = __dirname + '/resources/frontend_client/app/';
// All JS files except dist and test
Tom Robinson
committed
var JS_SRC = glob.sync(BASE_PATH + '**/*.js', { ignore: BASE_PATH + 'dist/**/*.js' });
var CSS_SRC = glob.sync(BASE_PATH + 'css/**/*.css').concat(glob.sync(BASE_PATH + 'components/**/*.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");
Tom Robinson
committed
}
Tom Robinson
committed
// 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)
Tom Robinson
committed
var BABEL_FEATURES = ['es7.asyncFunctions', 'es7.decorators'];
var cssMaps = { vars: {}, media: {}, selector: {} };
CSS_SRC.map(webpackPostcssTools.makeVarMap).forEach(function(map) {
for (var name in cssMaps) _.extend(cssMaps[name], map[name]);
Tom Robinson
committed
// 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: __dirname + '/resources/frontend_client/vendor.js',
__dirname + '/resources/frontend_client/vendor.css'
].concat(CSS_SRC)
output: {
path: __dirname + '/resources/frontend_client/app/dist',
// NOTE: the filename on disk won't include "?[chunkhash]" but the URL in index.html generated by HtmlWebpackPlugin will:
Tom Robinson
committed
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel', query: { cacheDirectory: '.babel_cache', optional: BABEL_FEATURES }},
{ test: /\.js$/, exclude: /node_modules|\.spec\.js/, loader: 'eslint' },
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader?-restructuring&compatibility!cssnext-loader') }
// { test: /\.css$/, loader: 'style-loader!css-loader!cssnext-loader' }
/node_modules\/(angular|ng-|ace|react-onclickoutside|moment|underscore|jquery|d3)/ // doesn't include 'crossfilter', 'dc', and 'tether' due to use of 'require'
Tom Robinson
committed
'metabase': __dirname + '/resources/frontend_client/app',
// 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',
Tom Robinson
committed
'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',
Tom Robinson
committed
'fixed-data-table': __dirname + '/node_modules/fixed-data-table/dist/fixed-data-table.min.js',
// 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',
'jquery': __dirname + '/node_modules/jquery/dist/jquery.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',
'd3-tip': __dirname + '/node_modules/d3-tip/index.js',
'humanize': __dirname + '/node_modules/humanize-plus/public/src/humanize.js'
// Automatically annotates angular functions (from "function($foo) {}" to "['$foo', function($foo) {}]")
// so minification doesn't break dependency injections
Tom Robinson
committed
// 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
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: 'resources/frontend_client/index_template.html',
inject: 'head'
}),
new webpack.DefinePlugin({
'process.env': { NODE_ENV: JSON.stringify(NODE_ENV) }
// 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']
Tom Robinson
committed
};
Tom Robinson
committed
if (NODE_ENV === "hot") {
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server'
);
config.output.publicPath = "http://localhost:8080" + config.output.publicPath;
Tom Robinson
committed
{ test: /\.react.js$/, exclude: /node_modules/, loaders: ['react-hot', 'babel?'+BABEL_FEATURES.map(function(f) { return 'optional[]='+f; }).join('&')] }
Tom Robinson
committed
// development environment:
Tom Robinson
committed
if (NODE_ENV === "development" || NODE_ENV === "hot") {
Tom Robinson
committed
// replace minified files with un-minified versions
for (var name in config.resolve.alias) {
var minified = config.resolve.alias[name];
Tom Robinson
committed
var unminified = minified.replace(/[.-]min/, '');
if (minified !== unminified && fs.existsSync(unminified)) {
Tom Robinson
committed
}
}
// SourceMaps
// Normal source map works better but takes longer to build
// Eval source map doesn't work with CSS but is faster to build