webpack的proxy代理路径改写

 devServer: {
    proxy: {
        context: () => true,
        target: configJSON.proxy,
        pathRewrite: function (path, req) {
            const staticRegExp = /\.(js|css|map|json|html|svg|jpg|jpeg|png)/;
            if (
                configJSON.develop &&
                staticRegExp.test(req.url) &&
                req.url.indexOf('/模块名/') < 0 &&
                req.url.indexOf('/xxx/resources') < 0
            ) {
                return `/xxx/resources${req.url}`;
            }
            return path;
        },
        bypass: function (req, res, proxyOptions) {
            if (req.url.indexOf('/xxx/resources/模块名') >= 0) {
                return req.url.replace('/xxx/resources', '');
            }
            return null;
        }
    }
}

 

posted @ 2021-10-08 14:45  全玉  阅读(377)  评论(0编辑  收藏  举报