@aliretail/vite-config-xixi
Version:
瓴羊客服vite配置脚手架
111 lines (110 loc) • 5.37 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const url_1 = require("url");
const _ = require("lodash");
const http_proxy_1 = require("http-proxy");
const util_1 = require("../util");
const virtualFileId = '/@entry.ts';
const mode = process.env.NODE_ENV;
function EntryPlugin(target) {
return {
name: 'xixi-entry',
apply: 'serve',
resolveId(id) {
if (id === virtualFileId) {
// return virtualFileId
return virtualFileId.slice(1);
}
},
load(id) {
if (id === virtualFileId.slice(1)) {
let enterPath = path.join(__dirname, '../../res/entry.ts');
if (mode == 'production') {
enterPath = path.join(__dirname, '../../res/product-entry.ts');
}
return (0, util_1.readFile)(enterPath, 'utf8').then(source => {
if (process.env.MODIFY_CONFIG) {
return `import __modifyConfig from '${process.env.MODIFY_CONFIG}';${source}`;
}
return source;
});
}
},
...(target && {
configureServer(server) {
const urlMap = new WeakMap();
server.middlewares.use(async (req, res, next) => {
urlMap.set(req, req.url);
next();
});
return () => {
server.middlewares.use(async (req, res, next) => {
// custom handle request...
const url = req.url && (0, util_1.cleanUrl)(req.url);
// spa-fallback always redirects to /index.html
if (!url?.endsWith('.html') ||
req.headers['sec-fetch-dest'] === 'script') {
return next();
}
req.url = urlMap.get(req);
const proxy = (0, http_proxy_1.createProxyServer)({
target,
changeOrigin: true,
autoRewrite: true,
selfHandleResponse: true,
secure: false,
});
proxy.once('proxyRes', async (proxyRes, _req, _res) => {
if (_res.headersSent)
return;
const { statusCode, headers } = proxyRes;
if (statusCode !== 200) {
if (statusCode === 302 && headers.location) {
const location = new url_1.URL(headers.location);
const redirectUrl = location.searchParams.get('redirect_url');
if (redirectUrl) {
const redirect = new url_1.URL(redirectUrl);
redirect.host = _req.headers.host;
if (server.config.server.https) {
// 预发重定向到了 http 站点
redirect.protocol = 'https';
}
location.searchParams.set('redirect_url', redirect.href);
headers.location = location.href;
}
}
_res.writeHead(statusCode, headers);
return proxyRes.pipe(_res);
}
const body = await (0, util_1.streamToString)(proxyRes.pipe(require('zlib').createGunzip()));
const oldEntryRe = /<script>[\s]*require\(\[.@ali\/servos-core.\][\s\S]+<\/script>/m;
try {
const html = await server.transformIndexHtml(url, body.replace(oldEntryRe, '<script type="module" src="/@entry.ts"></script>'), req.originalUrl);
const result = {
statusCode,
headers: {
'content-type': 'text/html;charset=UTF-8',
..._.pick(headers, 'set-cookie', 'eagleeye-traceid'),
},
body: html,
};
// TODO: cache
if (_res.headersSent)
return;
_res.writeHead(result.statusCode, result.headers);
_res.end(result.body);
}
catch (e) {
return next(e);
}
});
proxy.once('error', next);
proxy.web(req, res);
});
};
},
}),
};
}
exports.default = EntryPlugin;
;