aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/webpack.config.js
blob: 284276c1a51805eb191d576859c8f213f1bf2dc4 (plain) (blame)
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
const childProcess = require('child_process');
const ip = require('ip');
const path = require('path');
const webpack = require('webpack');

// The common js bundle (not this one) is built using tsc.
// The umd bundle (this one) has a different entrypoint.

const ACCEPTABLE_ENV_NAMES = ['production_standalone', 'production_cdn', 'staging', 'dogfood', 'development'];
const getEnvironmentName = env => {
    if (!env) {
        throw new Error('Please specify env via --env to webpack');
    }
    const foundName = ACCEPTABLE_ENV_NAMES.find(e => (env[e] ? e : false));
    if (!foundName) {
        throw new Error(
            `Couldn't find env name, please specify via one of the following CLI arguments: ${acceptableEnvNames.map(
                i => `--env.${i}`,
            )}`,
        );
    }
    return foundName;
};

const getConfigForEnv = environmentName => {
    switch (environmentName) {
        case 'production_standalone':
        case 'production_cdn':
            return {
                heapAnalyticsIdEnvName: 'INSTANT_HEAP_ANALYTICS_ID_PRODUCTION',
                heapAnalyticsIdRequired: environmentName !== 'production_standalone',
            };
        case 'staging':
        case 'dogfood':
        case 'development':
            return {
                heapAnalyticsIdEnvName: 'INSTANT_HEAP_ANALYTICS_ID_DEVELOPMENT',
                heapAnalyticsIdRequired: environmentName !== 'development',
            };
    }
};

const GIT_SHA = childProcess
    .execSync('git rev-parse HEAD')
    .toString()
    .trim();
const generateConfig = (environmentName, configOptions) => {
    const outputPath = process.env.WEBPACK_OUTPUT_PATH || 'umd';

    const { heapAnalyticsIdEnvName, heapAnalyticsIdRequired } = configOptions;
    const heapAnalyticsId = process.env[heapAnalyticsIdEnvName];
    if (heapAnalyticsIdRequired && !heapAnalyticsId) {
        throw new Error(
            `Must define heap analytics id in ENV var ${heapAnalyticsIdEnvName} when building for ${environmentName}`,
        );
    }

    const envVars = {
        GIT_SHA: JSON.stringify(GIT_SHA),
        INSTANT_ENVIRONMENT: JSON.stringify(environmentName),
        NPM_PACKAGE_VERSION: JSON.stringify(process.env.npm_package_version),
    };
    if (heapAnalyticsId) {
        envVars.HEAP_ANALYTICS_ID = JSON.stringify(heapAnalyticsId);
    }

    const config = {
        entry: {
            instant: './src/index.umd.ts',
        },
        output: {
            filename: '[name].js',
            path: path.resolve(__dirname, outputPath),
            library: 'zeroExInstant',
            libraryTarget: 'umd',
        },
        plugins: [
            new webpack.DefinePlugin({
                'process.env': envVars,
            }),
        ],
        devtool: 'source-map',
        resolve: {
            extensions: ['.js', '.json', '.ts', '.tsx'],
        },
        module: {
            rules: [
                {
                    test: /\.(ts|tsx)$/,
                    loader: 'awesome-typescript-loader',
                },
                {
                    test: /\.svg$/,
                    loader: 'svg-react-loader',
                },
            ],
        },
        devServer: {
            contentBase: path.join(__dirname, 'public'),
            port: 5000,
            host: '0.0.0.0',
            after: () => {
                if (config.devServer.host === '0.0.0.0') {
                    console.log(
                        `webpack-dev-server can be accessed externally at: http://${ip.address()}:${
                            config.devServer.port
                        }`,
                    );
                }
            },
        },
    };
    return config;
};

module.exports = (env, _argv) => {
    const environmentName = getEnvironmentName(env);
    const configOptions = getConfigForEnv(environmentName);
    return generateConfig(environmentName, configOptions);
};