This repository was archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
executable file
·128 lines (118 loc) · 4.62 KB
/
webpack.config.js
File metadata and controls
executable file
·128 lines (118 loc) · 4.62 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
/*
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');
// Needed IF you want to run git commands & get current branch
// const { execSync } = require('child_process');
const webDir = process.env.OUTPUT_WEB_DIR || 'extension';
// 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)) {
/* 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: ['@babel/polyfill'], //'./src/js/contentscript.js'], No contentscript needed for T4G
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
},
module: {
rules: [
{ // Typescript
test: /\.tsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets: [
['@babel/preset-typescript', { targets: { ie: "11" }, loose: true }],
'@babel/react'
],
plugins: [
'@babel/plugin-transform-typescript',
'@babel/plugin-proposal-object-rest-spread',
'babel-plugin-const-enum'
]
}
},
{ // .js or .jsx
test: /.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets: [
['@babel/preset-env', { targets: { ie: '11' }, loose: true }]
],
plugins: [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-transform-react-jsx',
]
}
}, {
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': { 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' }),
makeConfig({entry:'./src/js/setup-iframe.js', filename: 'js/bundle-setup-iframe-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;