Skip to content
Snippets Groups Projects
webpack.config.js 9.45 KiB
Newer Older
/* eslint-env node */
/* eslint-disable import/no-commonjs */

require("babel-register");
require("babel-polyfill");
Tom Robinson's avatar
Tom Robinson committed
var webpack = require('webpack');

var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');
var UnusedFilesWebpackPlugin = require("unused-files-webpack-plugin").default;
var BannerWebpackPlugin = require('banner-webpack-plugin');
var UglifyJSPlugin = require('uglifyjs-webpack-plugin')
Tom Robinson's avatar
Tom Robinson committed

Tom Robinson's avatar
Tom Robinson committed
var chevrotain = require("chevrotain");
var allTokens = require("./frontend/src/metabase/lib/expressions/tokens").allTokens;

var SRC_PATH = __dirname + '/frontend/src/metabase';
var LIB_SRC_PATH = __dirname + '/frontend/src/metabase-lib';
var TEST_SUPPORT_PATH = __dirname + '/frontend/test/__support__';
var BUILD_PATH = __dirname + '/resources/frontend_client';
Tom Robinson's avatar
Tom Robinson committed

// default NODE_ENV to development
var NODE_ENV = process.env["NODE_ENV"] || "development";
Tom Robinson's avatar
Tom Robinson committed

// Babel:
var BABEL_CONFIG = {
    cacheDirectory: process.env.BABEL_DISABLE_CACHE ? null : ".babel_cache"
    localIdentName: NODE_ENV !== "production" ?
        "[name]__[local]___[hash:base64:5]" :
        "[hash:base64:5]",
    url: false, // disabled because we need to use relative url()
Tom Robinson's avatar
Tom Robinson committed
var config = module.exports = {
Tom Robinson's avatar
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
Tom Robinson's avatar
Tom Robinson committed
    entry: {
        "app-main": './app-main.js',
        "app-public": './app-public.js',
        "app-embed": './app-embed.js',
        styles: './css/index.css',
Tom Robinson's avatar
Tom Robinson committed
    },

Tom Robinson's avatar
Tom Robinson committed
    // output to "dist"
Tom Robinson's avatar
Tom Robinson committed
    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]',
Tom Robinson's avatar
Tom Robinson committed
    },
Tom Robinson's avatar
Tom Robinson committed

Tom Robinson's avatar
Tom Robinson committed
    module: {
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: [
                    { loader: "babel-loader", options: BABEL_CONFIG }
                ]
            },
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules|\.spec\.js/,
Tom Robinson's avatar
Tom Robinson committed
            {
                test: /\.(eot|woff2?|ttf|svg|png)$/,
Tom Robinson's avatar
Tom Robinson committed
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: [
                        { loader: "css-loader", options: CSS_CONFIG },
                        { loader: "postcss-loader" }
                    ]
                })
Tom Robinson's avatar
Tom Robinson committed
        ]
    },
    resolve: {
        extensions: [".webpack.js", ".web.js", ".js", ".jsx", ".css"],
            'metabase': SRC_PATH,
            'metabase-lib': LIB_SRC_PATH,
            '__support__': TEST_SUPPORT_PATH,
            'style': SRC_PATH + '/css/core/index',
            'ace': __dirname + '/node_modules/ace-builds/src-min-noconflict',
Tom Robinson's avatar
Tom Robinson committed
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            minChunks(module) {
                return module.context && module.context.indexOf('node_modules') >= 0
            }
        }),
        new UnusedFilesWebpackPlugin({
            globOptions: {
                ignore: [
                    "**/*.spec.*",
Tom Robinson's avatar
Tom Robinson committed
                    "**/__mocks__/*.js*",
                    "internal/lib/components-node.js"
Tom Robinson's avatar
Tom Robinson committed
        // 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({
            filename: '[name].bundle.css?[contenthash]'
        }),
        new HtmlWebpackPlugin({
            chunks: ["vendor", "styles", "app-main"],
            template: __dirname + '/resources/frontend_client/index_template.html',
        }),
        new HtmlWebpackPlugin({
            filename: '../../public.html',
            chunks: ["vendor", "styles", "app-public"],
            template: __dirname + '/resources/frontend_client/index_template.html',
        }),
        new HtmlWebpackPlugin({
            filename: '../../embed.html',
            chunks: ["vendor", "styles", "app-embed"],
            template: __dirname + '/resources/frontend_client/index_template.html',
            inject: 'head',
            alwaysWriteToDisk: true,
        }),
        new HtmlWebpackHarddiskPlugin({
            outputPath: __dirname + '/resources/frontend_client/app/dist'
            '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",
                },
            }
        }),
    // 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;
        test: /\.jsx$/,
        // NOTE: our verison of react-hot-loader doesn't play nice with react-dnd's DragLayer, so we exclude files named `*DragLayer.jsx`
        exclude: /node_modules|DragLayer\.jsx$/,
            // NOTE Atte Keinänen 10/19/17: We are currently sticking to an old version of react-hot-loader
            // because newer versions would require us to upgrade to react-router v4 and possibly deal with
            // asynchronous route issues as well. See https://github.com/gaearon/react-hot-loader/issues/249
            { loader: 'react-hot-loader' },
            { loader: 'babel-loader', options: BABEL_CONFIG }
        ]
    // disable ExtractTextPlugin
    config.module.rules[config.module.rules.length - 1].use = [
        { loader: "style-loader" },
        { loader: "css-loader", options: CSS_CONFIG },
    config.devServer = {
        hot: true,
        inline: true,
        contentBase: "frontend",
        headers: {
            'Access-Control-Allow-Origin': '*'
        }
        // if webpack doesn't reload UI after code change in development
        // watchOptions: {
        //     aggregateTimeout: 300,
        //     poll: 1000
        // }
        // if you want to reduce stats noise
        // stats: 'minimal' // values: none, errors-only, minimal, normal, verbose
Tom Robinson's avatar
Tom Robinson committed
    config.plugins.unshift(
        new webpack.NoEmitOnErrorsPlugin(),
        new webpack.NamedModulesPlugin(),
        new webpack.HotModuleReplacementPlugin()
if (NODE_ENV !== "production") {
    // replace minified files with un-minified versions
Tom Robinson's avatar
Tom Robinson committed
    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)) {
Tom Robinson's avatar
Tom Robinson committed
            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;
    config.plugins.push(new UglifyJSPlugin({
        uglifyOptions: {
            mangle: {
                // this is required to ensure we don't minify Chevrotain token identifiers
                // https://github.com/SAP/chevrotain/tree/master/examples/parser/minification
                except: allTokens.map(function (currTok) {
                    return chevrotain.tokenName(currTok);
                })
            }
    config.devtool = "source-map";