fix: connect-history-api-fallback 挪回 webpack,vite 后续再考虑支持

This commit is contained in:
winixt 2022-04-08 14:56:19 +08:00
parent 562fc003b2
commit 0100b10567
5 changed files with 26 additions and 26 deletions

View File

@ -42,6 +42,7 @@
"babel-plugin-import": "1.13.3", "babel-plugin-import": "1.13.3",
"cli-highlight": "^2.1.4", "cli-highlight": "^2.1.4",
"cliui": "7.0.4", "cliui": "7.0.4",
"connect-history-api-fallback": "^1.6.0",
"copy-webpack-plugin": "^7.0.0", "copy-webpack-plugin": "^7.0.0",
"css-loader": "^5.0.1", "css-loader": "^5.0.1",
"css-minimizer-webpack-plugin": "^3.0.0", "css-minimizer-webpack-plugin": "^3.0.0",

View File

@ -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;

View File

@ -36,6 +36,7 @@ export default (api) => {
], ],
async fn({ args = {} }) { async fn({ args = {} }) {
const { cleanTmpPathExceptCache, getBundleAndConfigs } = require('../buildDevUtils'); const { cleanTmpPathExceptCache, getBundleAndConfigs } = require('../buildDevUtils');
const connectHistoryMiddleware = require('./connectHistoryMiddleware').default;
port = await getPort(args.port || api.config.devServer?.port); port = await getPort(args.port || api.config.devServer?.port);
changePort(port); changePort(port);
@ -80,7 +81,7 @@ export default (api) => {
port, port,
proxy: api.config.proxy, proxy: api.config.proxy,
https: isHTTPS, https: isHTTPS,
beforeMiddlewares: [...beforeMiddlewares], beforeMiddlewares: [connectHistoryMiddleware(api), ...beforeMiddlewares],
afterMiddlewares: [...middlewares], afterMiddlewares: [...middlewares],
customerDevServerConfig: api.config.devServer, customerDevServerConfig: api.config.devServer,
}); });

View File

@ -28,7 +28,6 @@
"@fesjs/compiler": "^2.0.5", "@fesjs/compiler": "^2.0.5",
"@fesjs/utils": "^2.0.4", "@fesjs/utils": "^2.0.4",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"connect-history-api-fallback": "^1.6.0",
"cookie": "^0.4.2", "cookie": "^0.4.2",
"cookie-parser": "^1.4.5", "cookie-parser": "^1.4.5",
"envinfo": "^7.7.3", "envinfo": "^7.7.3",

View File

@ -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) => { export default (api) => {
api.describe({ api.describe({
key: 'proxy', key: 'proxy',
@ -32,6 +10,4 @@ export default (api) => {
}, },
}, },
}); });
api.addBeforeMiddlewares(() => proxyMiddleware(api));
}; };