From 2dacd08bea7f76cc4f93d98de132cd9ec337c977 Mon Sep 17 00:00:00 2001 From: Ben Amor <43776482+benamorbe@users.noreply.github.com> Date: Mon, 27 Jul 2020 13:52:02 +0200 Subject: [PATCH] Add override mechanism for webpack config (#5057) --- scripts/README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ webpack.config.js | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 scripts/README.md diff --git a/scripts/README.md b/scripts/README.md new file mode 100644 index 00000000..7945e781 --- /dev/null +++ b/scripts/README.md @@ -0,0 +1,44 @@ +You can use this folder to add scripts and configurations to customize Redash build and development loop. + +## How to customize Webpack + +### Configurable parameters + +You can override the values of configurable parameters by exporting a `CONFIG` object from the module located at `scripts/config`. + +Currently the following parameters are supported: + +- **staticPath**: Override the location of Redash static files (default = `/static/`). + +#### Example Configuration (`scripts/config.js`): + +```javascript +module.exports = { + staticPath: "my/redash/static/path" +}; +``` + +### Programmatically + +For advanced customization, you can provide a script to apply any kind of overrides to the default config as provided by `webpack.config.js`. + +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. + +#### Example Override Script (`scripts/webpack/overrides.js`): + +This is an example of an override that enables Webpack stats. + +```javascript +function applyOverrides(webpackConfig) { + return { + ...webpackConfig, + stats: { + children: true, + modules: true, + chunkModules: true + } + }; +} + +module.exports = applyOverrides; +``` diff --git a/webpack.config.js b/webpack.config.js index e1af0b07..f2d94a8d 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -13,9 +13,26 @@ const fs = require("fs"); const path = require("path"); +function optionalRequire(module, defaultReturn = undefined) { + try { + require.resolve(module); + } catch (e) { + if (e && e.code === "MODULE_NOT_FOUND") { + // Module was not found, return default value if any + return defaultReturn; + } + throw e; + } + return require(module); +} + +// Load optionally configuration object (see scripts/README) +const CONFIG = optionalRequire("./scripts/config", {}); + const isProduction = process.env.NODE_ENV === "production"; const redashBackend = process.env.REDASH_BACKEND || "http://localhost:5000"; +const staticPath = CONFIG.staticPath || "/static/"; const basePath = path.join(__dirname, "client"); const appPath = path.join(__dirname, "client", "app"); @@ -24,6 +41,19 @@ const extensionsRelativePath = process.env.EXTENSIONS_DIRECTORY || path.join("client", "app", "extensions"); const extensionPath = path.join(__dirname, extensionsRelativePath); +// Function to apply configuration overrides (see scripts/README) +function maybeApplyOverrides(config) { + const overridesLocation = process.env.REDASH_WEBPACK_OVERRIDES || "./scripts/webpack/overrides"; + const applyOverrides = optionalRequire(overridesLocation); + if (!applyOverrides) { + return config; + } + console.info("Custom overrides found. Applying them..."); + const newConfig = applyOverrides(config); + console.info("Custom overrides applied successfully."); + return newConfig; +} + const config = { mode: isProduction ? "production" : "development", entry: { @@ -37,7 +67,7 @@ const config = { output: { path: path.join(basePath, "./dist"), filename: isProduction ? "[name].[chunkhash].js" : "[name].js", - publicPath: "/static/" + publicPath: staticPath }, resolve: { symlinks: false, @@ -55,7 +85,8 @@ const config = { template: "./client/app/index.html", filename: "index.html", excludeChunks: ["server"], - release: process.env.BUILD_VERSION || "dev" + release: process.env.BUILD_VERSION || "dev", + staticPath }), new HtmlWebpackPlugin({ template: "./client/app/multi_org.html", @@ -194,7 +225,7 @@ const config = { rewrites: [{ from: /./, to: "/static/index.html" }] }, contentBase: false, - publicPath: "/static/", + publicPath: staticPath, proxy: [ { context: [ @@ -238,4 +269,4 @@ if (process.env.BUNDLE_ANALYZER) { config.plugins.push(new BundleAnalyzerPlugin()); } -module.exports = config; +module.exports = maybeApplyOverrides(config);