-
Notifications
You must be signed in to change notification settings - Fork 5
/
webpack.config.js
executable file
·138 lines (128 loc) · 4.94 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
/*
NB The webpack.config.js files in my-loop, adserver and wwappbase.js are identical but cannot be symlinked!
If it's a symlink, NPM will resolve paths (including module names) relative to the symlink source - and
complain that it can't find webpack, because it's not installed in /wwappbase.js/templates/node_modules
Keep this copy in sync with the others - if the same file can't be used for all three, there should be a good reason.
*/
const webpack = require('webpack');
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// Needed to check hostname & try to load local config file
const os = require('os');
const fs = require('fs');
const webDir = process.env.OUTPUT_WEB_DIR || 'web';
// Check for file "config/$HOSTNAME.js" and look for ServerIO overrides in it
let SERVERIO_OVERRIDES = JSON.stringify({});
const configFile = './config/' + os.hostname() + '.js';
if (fs.existsSync(configFile)) {
console.log('*********************************** FOUND GRAVITAS.JS')
/* eslint-disable-next-line global-require, import/no-dynamic-require */
let hostConfig = require(configFile);
if (hostConfig.ServerIOOverrides) SERVERIO_OVERRIDES = JSON.stringify(hostConfig.ServerIOOverrides);
}
const baseConfig = {
// NB When editing keep the "our code" entry point last in this list - makeConfig override depends on this position.
entry: ['core-js', './src/js/app.jsx'],
output: {
path: path.resolve(__dirname, './' + webDir + '/build/'), // NB: this should include js and css outputs
// filename: is left undefined and filled in by makeConfig
},
devtool: 'source-map',
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx'],
symlinks: false,
alias: {
querystring: 'querystring-es3',
util: 'util'
}
},
module: {
rules: [
{ // Typescript
test: /\.tsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets: [
['@babel/preset-typescript', { targets: { ie: '11' }, loose: true }],
['@babel/preset-env', { targets: { ie: '11' }, loose: true }],
'@babel/react'
],
plugins: [
'@babel/plugin-transform-typescript',
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-transform-runtime',
'babel-plugin-const-enum',
// loose: true specified to silence warnings about mismatch with preset-env loose setting
['@babel/plugin-proposal-class-properties', { loose: true }],
['@babel/plugin-proposal-private-methods', { loose: true }],
['@babel/plugin-proposal-private-property-in-object', { loose: true }]
]
}
},
{ // .js or .jsx
test: /.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets: [
['@babel/preset-env', { targets: { ie: '11' }, loose: true }]
],
plugins: [
'@babel/plugin-transform-react-jsx',
'@babel/plugin-transform-runtime',
// loose: true specified to silence warnings about mismatch with preset-env loose setting
['@babel/plugin-proposal-class-properties', { loose: true }],
['@babel/plugin-proposal-private-methods', { loose: true }],
['@babel/plugin-proposal-private-property-in-object', { loose: true }]
]
}
}, {
test: /\.less$/,
use: [MiniCssExtractPlugin.loader, {loader: 'css-loader', options: {url: false}}, 'less-loader'],
}
],
},
plugins: [
new MiniCssExtractPlugin({ filename: 'style/main.css' }),
new webpack.DefinePlugin({
'process.env': { SERVERIO_OVERRIDES }
}),
]
};
/**
* Copy and fill out the baseConfig object with
* @param {!string} filename Set the bundle output.filename
* @param {?string} mode "production" or "development", determines if JS will be minified
* @param {?string} entry (unusual) Compile a different entry-point file instead of app.jsx
* ## process.env
* process is always globally available to runtime code.
*/
const makeConfig = ({ filename, mode, entry }) => {
const config = { ...baseConfig, mode };
config.output = { ...baseConfig.output, filename };
// Has an entry point other than ./src/js/app.jsx been requested?
if (entry) config.entry = [...config.entry.slice(0, -1), entry]; // Copy, don't modify in-place
return config;
};
const configs = [
makeConfig({filename: 'js/bundle-debug.js', mode: 'development' }),
// Add additional configs (eg with different entry points) like this:
// makeConfig({filename: 'js/other-bundle-debug.js', mode: 'development', entry:'./src/js/other.js'}),
];
// Default behaviour: Create a production config (with mode & output filename amended) for each dev config.
// Allow debug-only compilation for faster iteration in dev
if (process.env.NO_PROD !== 'true') {
const prodconfigs = configs.map(devc => ({
...devc,
mode: 'production',
output: {
...devc.output,
filename: devc.output.filename.replace('-debug', '')
}
}));
// Put new production configs in the main list
prodconfigs.forEach(prodc => configs.push(prodc));
}
// Output bundle files for production and dev/debug
module.exports = configs;