-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebpack.config.js
More file actions
149 lines (136 loc) · 5.61 KB
/
Copy pathwebpack.config.js
File metadata and controls
149 lines (136 loc) · 5.61 KB
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
139
140
141
142
143
144
145
146
147
148
149
/*
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');
// Uncomment if this project needs to run git commands - e.g. to get current branch
// const { execSync } = require('child_process');
const webDir = process.env.OUTPUT_WEB_DIR || 'web';
// Check for file "config/$HOSTNAME.js" so it can be require()d to pull in host-specific overrides.
let CONFIG_FILE = './config/' + os.hostname() + '.js';
if (fs.existsSync(CONFIG_FILE)) {
console.log('Using host-specific config file:', CONFIG_FILE);
CONFIG_FILE = CONFIG_FILE.replace(/^\./, ''); // strip initial . and treat as project-absolute path
} else {
console.log('No host-specific config file found, using defaults.');
CONFIG_FILE = null;
}
CONFIG_FILE = JSON.stringify(CONFIG_FILE);
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$/,
// If we use css-loader with default options it will try to inline any url('/img/whatever.png') rules it finds.
// We don't want this (plus the URLs don't resolve correctly, throwing an error on compile) so {url: false} disables it.
use: [MiniCssExtractPlugin.loader, {loader: 'css-loader', options: {url: false}}, 'less-loader'],
}
],
},
plugins: [
new MiniCssExtractPlugin({ filename: 'style/main.css' }),
new webpack.DefinePlugin({
'process.env': { CONFIG_FILE }
}),
]
};
/**
* 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' }),
makeConfig({filename: 'js/newtab-bundle-debug.js', mode: 'development', entry:'./src/js/newtab.jsx'}),
makeConfig({filename: 'js/static-bundle-debug.js', mode: 'development', entry:'./src/js/static.js'}),
// makeConfig({filename: 'js/card-bundle-debug.js', mode: 'development', entry:'./src/js/card.jsx'}),
// 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;