From 0735e2fc5266e23b99b24e7ffb3bb4b205313897 Mon Sep 17 00:00:00 2001 From: yihu Date: Fri, 1 Apr 2022 22:52:45 +0800 Subject: [PATCH] feat: add devServer config entry for users --- templates/js/config/devServerProxy.js | 57 +++++++++++++++++++++++++++ templates/js/direflow-webpack.js | 22 +++++++++-- templates/ts/config/devServerProxy.js | 57 +++++++++++++++++++++++++++ templates/ts/direflow-webpack.js | 22 +++++++++-- 4 files changed, 150 insertions(+), 8 deletions(-) create mode 100644 templates/js/config/devServerProxy.js create mode 100644 templates/ts/config/devServerProxy.js diff --git a/templates/js/config/devServerProxy.js b/templates/js/config/devServerProxy.js new file mode 100644 index 0000000..eecb918 --- /dev/null +++ b/templates/js/config/devServerProxy.js @@ -0,0 +1,57 @@ +/** + * Use the mock proxy + * + * If isMock is true, mockTarget is valid, target is invalid. + */ + const isMock = false; + +/** + * Change the proxy env + * 'test', 'dev', etc. target is valid, mockTarget is invalid. + */ + +const env = "dev"; + + +/** + * This is the default devServer proxy config, you can use it in proxyCustomConfig or not. + */ +const DEFAULT_PROXY_CONFIG = { + secure: false, + changeOrigin: true, +}; + +/** + * Config your proxy api here. + */ +const config = { + "/api": { + target: { + dev: "https://api.dev.com/", + test: "https://api.test.com/", + }, + mockTarget: "https://api.mock.com/", + proxyCustomConfig: { ...DEFAULT_PROXY_CONFIG }, + }, + "/apiAnother": { + target: { + dev: "https://api.another.dev.com/", + test: "https://api.another.test.com/", + }, + mockTarget: "https://api.another.mock.com/", + proxyCustomConfig: { ...DEFAULT_PROXY_CONFIG }, + }, +}; + +const proxyConfig = () => + Object.entries(config).reduce((acc, [configKey, configValue]) => { + // Avoid incorrect 'target' config in proxyCustomConfig + delete configValue.proxyCustomConfig["target"]; + acc[configKey] = { + target: isMock ? configValue.mockTarget : configValue.target[env], + ...configValue.proxyCustomConfig, + }; + return acc; + }, {}); + +module.exports = () => proxyConfig(); diff --git a/templates/js/direflow-webpack.js b/templates/js/direflow-webpack.js index bfedf62..4a92a1d 100644 --- a/templates/js/direflow-webpack.js +++ b/templates/js/direflow-webpack.js @@ -1,10 +1,24 @@ const { webpackConfig } = require('direflow-scripts'); +const devServerProxy = require("./config/devServerProxy"); /** * Webpack configuration for Direflow Component * Additional webpack plugins / overrides can be provided here */ -module.exports = (config, env) => ({ - ...webpackConfig(config, env), - // Add your own webpack config here (optional) -}); +module.exports = { + webpack: (config, env) => { + return { + ...webpackConfig(config, env), + // Add your own webpack config here (optional) + }; + }, + devServer: function (configFunction) { + return function (proxy, allowedHost) { + // Config your own devServer proxy in ./config/devServerProxy.js + proxy = devServerProxy(); + + const config = configFunction(proxy, allowedHost); + return config; + }; + }, +}; diff --git a/templates/ts/config/devServerProxy.js b/templates/ts/config/devServerProxy.js new file mode 100644 index 0000000..eecb918 --- /dev/null +++ b/templates/ts/config/devServerProxy.js @@ -0,0 +1,57 @@ +/** + * Use the mock proxy + * + * If isMock is true, mockTarget is valid, target is invalid. + */ + const isMock = false; + +/** + * Change the proxy env + * 'test', 'dev', etc. target is valid, mockTarget is invalid. + */ + +const env = "dev"; + + +/** + * This is the default devServer proxy config, you can use it in proxyCustomConfig or not. + */ +const DEFAULT_PROXY_CONFIG = { + secure: false, + changeOrigin: true, +}; + +/** + * Config your proxy api here. + */ +const config = { + "/api": { + target: { + dev: "https://api.dev.com/", + test: "https://api.test.com/", + }, + mockTarget: "https://api.mock.com/", + proxyCustomConfig: { ...DEFAULT_PROXY_CONFIG }, + }, + "/apiAnother": { + target: { + dev: "https://api.another.dev.com/", + test: "https://api.another.test.com/", + }, + mockTarget: "https://api.another.mock.com/", + proxyCustomConfig: { ...DEFAULT_PROXY_CONFIG }, + }, +}; + +const proxyConfig = () => + Object.entries(config).reduce((acc, [configKey, configValue]) => { + // Avoid incorrect 'target' config in proxyCustomConfig + delete configValue.proxyCustomConfig["target"]; + acc[configKey] = { + target: isMock ? configValue.mockTarget : configValue.target[env], + ...configValue.proxyCustomConfig, + }; + return acc; + }, {}); + +module.exports = () => proxyConfig(); diff --git a/templates/ts/direflow-webpack.js b/templates/ts/direflow-webpack.js index bfedf62..4a92a1d 100644 --- a/templates/ts/direflow-webpack.js +++ b/templates/ts/direflow-webpack.js @@ -1,10 +1,24 @@ const { webpackConfig } = require('direflow-scripts'); +const devServerProxy = require("./config/devServerProxy"); /** * Webpack configuration for Direflow Component * Additional webpack plugins / overrides can be provided here */ -module.exports = (config, env) => ({ - ...webpackConfig(config, env), - // Add your own webpack config here (optional) -}); +module.exports = { + webpack: (config, env) => { + return { + ...webpackConfig(config, env), + // Add your own webpack config here (optional) + }; + }, + devServer: function (configFunction) { + return function (proxy, allowedHost) { + // Config your own devServer proxy in ./config/devServerProxy.js + proxy = devServerProxy(); + + const config = configFunction(proxy, allowedHost); + return config; + }; + }, +};