Skip to content

Commit 2dacd08

Browse files
authored
Add override mechanism for webpack config (#5057)
1 parent fd76a2e commit 2dacd08

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

scripts/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
You can use this folder to add scripts and configurations to customize Redash build and development loop.
2+
3+
## How to customize Webpack
4+
5+
### Configurable parameters
6+
7+
You can override the values of configurable parameters by exporting a `CONFIG` object from the module located at `scripts/config`.
8+
9+
Currently the following parameters are supported:
10+
11+
- **staticPath**: Override the location of Redash static files (default = `/static/`).
12+
13+
#### Example Configuration (`scripts/config.js`):
14+
15+
```javascript
16+
module.exports = {
17+
staticPath: "my/redash/static/path"
18+
};
19+
```
20+
21+
### Programmatically
22+
23+
For advanced customization, you can provide a script to apply any kind of overrides to the default config as provided by `webpack.config.js`.
24+
25+
The override module must be located under `scripts/webpack/overrides`. It should export a `function` that receives the Webpack configuration object and returns the overridden version.
26+
27+
#### Example Override Script (`scripts/webpack/overrides.js`):
28+
29+
This is an example of an override that enables Webpack stats.
30+
31+
```javascript
32+
function applyOverrides(webpackConfig) {
33+
return {
34+
...webpackConfig,
35+
stats: {
36+
children: true,
37+
modules: true,
38+
chunkModules: true
39+
}
40+
};
41+
}
42+
43+
module.exports = applyOverrides;
44+
```

webpack.config.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,26 @@ const fs = require("fs");
1313

1414
const path = require("path");
1515

16+
function optionalRequire(module, defaultReturn = undefined) {
17+
try {
18+
require.resolve(module);
19+
} catch (e) {
20+
if (e && e.code === "MODULE_NOT_FOUND") {
21+
// Module was not found, return default value if any
22+
return defaultReturn;
23+
}
24+
throw e;
25+
}
26+
return require(module);
27+
}
28+
29+
// Load optionally configuration object (see scripts/README)
30+
const CONFIG = optionalRequire("./scripts/config", {});
31+
1632
const isProduction = process.env.NODE_ENV === "production";
1733

1834
const redashBackend = process.env.REDASH_BACKEND || "http://localhost:5000";
35+
const staticPath = CONFIG.staticPath || "/static/";
1936

2037
const basePath = path.join(__dirname, "client");
2138
const appPath = path.join(__dirname, "client", "app");
@@ -24,6 +41,19 @@ const extensionsRelativePath =
2441
process.env.EXTENSIONS_DIRECTORY || path.join("client", "app", "extensions");
2542
const extensionPath = path.join(__dirname, extensionsRelativePath);
2643

44+
// Function to apply configuration overrides (see scripts/README)
45+
function maybeApplyOverrides(config) {
46+
const overridesLocation = process.env.REDASH_WEBPACK_OVERRIDES || "./scripts/webpack/overrides";
47+
const applyOverrides = optionalRequire(overridesLocation);
48+
if (!applyOverrides) {
49+
return config;
50+
}
51+
console.info("Custom overrides found. Applying them...");
52+
const newConfig = applyOverrides(config);
53+
console.info("Custom overrides applied successfully.");
54+
return newConfig;
55+
}
56+
2757
const config = {
2858
mode: isProduction ? "production" : "development",
2959
entry: {
@@ -37,7 +67,7 @@ const config = {
3767
output: {
3868
path: path.join(basePath, "./dist"),
3969
filename: isProduction ? "[name].[chunkhash].js" : "[name].js",
40-
publicPath: "/static/"
70+
publicPath: staticPath
4171
},
4272
resolve: {
4373
symlinks: false,
@@ -55,7 +85,8 @@ const config = {
5585
template: "./client/app/index.html",
5686
filename: "index.html",
5787
excludeChunks: ["server"],
58-
release: process.env.BUILD_VERSION || "dev"
88+
release: process.env.BUILD_VERSION || "dev",
89+
staticPath
5990
}),
6091
new HtmlWebpackPlugin({
6192
template: "./client/app/multi_org.html",
@@ -194,7 +225,7 @@ const config = {
194225
rewrites: [{ from: /./, to: "/static/index.html" }]
195226
},
196227
contentBase: false,
197-
publicPath: "/static/",
228+
publicPath: staticPath,
198229
proxy: [
199230
{
200231
context: [
@@ -238,4 +269,4 @@ if (process.env.BUNDLE_ANALYZER) {
238269
config.plugins.push(new BundleAnalyzerPlugin());
239270
}
240271

241-
module.exports = config;
272+
module.exports = maybeApplyOverrides(config);

0 commit comments

Comments
 (0)