diff --git a/packages/fes-build-webpack/package.json b/packages/fes-build-webpack/package.json index f7e84eeb..d7ef6cd9 100644 --- a/packages/fes-build-webpack/package.json +++ b/packages/fes-build-webpack/package.json @@ -42,6 +42,7 @@ "babel-plugin-import": "1.13.3", "cli-highlight": "^2.1.4", "cliui": "7.0.4", + "connect-history-api-fallback": "^1.6.0", "copy-webpack-plugin": "^7.0.0", "css-loader": "^5.0.1", "css-minimizer-webpack-plugin": "^3.0.0", diff --git a/packages/fes-build-webpack/src/plugins/commands/dev/connectHistoryMiddleware.js b/packages/fes-build-webpack/src/plugins/commands/dev/connectHistoryMiddleware.js new file mode 100644 index 00000000..0eada6f2 --- /dev/null +++ b/packages/fes-build-webpack/src/plugins/commands/dev/connectHistoryMiddleware.js @@ -0,0 +1,23 @@ +import { extname } from 'path'; +import historyFallback from 'connect-history-api-fallback'; + +const ASSET_EXT_NAMES = ['.ico', '.png', '.jpg', '.jpeg', '.gif', '.svg']; +const SKIP_PATHS_PREFIX = ['/@']; + +const proxyMiddleware = (api) => (req, res, next) => { + const proxyConfig = api.config.proxy; + if (proxyConfig && Object.keys(proxyConfig).some((path) => req.url.startsWith(path))) { + return next(); + } + if (SKIP_PATHS_PREFIX.find((prefix) => req.url.startsWith(prefix))) { + return next(); + } + if (ASSET_EXT_NAMES.includes(extname(req.url))) { + return next(); + } + + const history = historyFallback(); + history(req, res, next); +}; + +export default proxyMiddleware; diff --git a/packages/fes-build-webpack/src/plugins/commands/dev/index.js b/packages/fes-build-webpack/src/plugins/commands/dev/index.js index 3f8c485c..aed64328 100644 --- a/packages/fes-build-webpack/src/plugins/commands/dev/index.js +++ b/packages/fes-build-webpack/src/plugins/commands/dev/index.js @@ -36,6 +36,7 @@ export default (api) => { ], async fn({ args = {} }) { const { cleanTmpPathExceptCache, getBundleAndConfigs } = require('../buildDevUtils'); + const connectHistoryMiddleware = require('./connectHistoryMiddleware').default; port = await getPort(args.port || api.config.devServer?.port); changePort(port); @@ -80,7 +81,7 @@ export default (api) => { port, proxy: api.config.proxy, https: isHTTPS, - beforeMiddlewares: [...beforeMiddlewares], + beforeMiddlewares: [connectHistoryMiddleware(api), ...beforeMiddlewares], afterMiddlewares: [...middlewares], customerDevServerConfig: api.config.devServer, }); diff --git a/packages/fes-preset-built-in/package.json b/packages/fes-preset-built-in/package.json index cd802f30..c28ea090 100644 --- a/packages/fes-preset-built-in/package.json +++ b/packages/fes-preset-built-in/package.json @@ -28,7 +28,6 @@ "@fesjs/compiler": "^2.0.5", "@fesjs/utils": "^2.0.4", "body-parser": "^1.19.0", - "connect-history-api-fallback": "^1.6.0", "cookie": "^0.4.2", "cookie-parser": "^1.4.5", "envinfo": "^7.7.3", diff --git a/packages/fes-preset-built-in/src/plugins/features/proxy.js b/packages/fes-preset-built-in/src/plugins/features/proxy.js index 2fe3d630..54680879 100644 --- a/packages/fes-preset-built-in/src/plugins/features/proxy.js +++ b/packages/fes-preset-built-in/src/plugins/features/proxy.js @@ -1,25 +1,3 @@ -import { extname } from 'path'; -import historyFallback from 'connect-history-api-fallback'; - -const ASSET_EXT_NAMES = ['.ico', '.png', '.jpg', '.jpeg', '.gif', '.svg']; -const SKIP_PATHS_PREFIX = ['/@']; - -const proxyMiddleware = (api) => (req, res, next) => { - const proxyConfig = api.config.proxy; - if (proxyConfig && Object.keys(proxyConfig).some((path) => req.url.startsWith(path))) { - return next(); - } - if (SKIP_PATHS_PREFIX.find((prefix) => req.url.startsWith(prefix))) { - return next(); - } - if (ASSET_EXT_NAMES.includes(extname(req.url))) { - return next(); - } - - const history = historyFallback(); - history(req, res, next); -}; - export default (api) => { api.describe({ key: 'proxy', @@ -32,6 +10,4 @@ export default (api) => { }, }, }); - - api.addBeforeMiddlewares(() => proxyMiddleware(api)); };