@knightly/vite
Version:
Native-ESM powered web dev build tool. It's fast.
49 lines • 2.26 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.clientPlugin = exports.clientPublicPath = exports.clientFilePath = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const chalk_1 = __importDefault(require("chalk"));
const config_1 = require("../config");
exports.clientFilePath = path_1.default.resolve(__dirname, '../../client/client.js');
exports.clientPublicPath = `/vite/client`;
const legacyPublicPath = '/vite/hmr';
exports.clientPlugin = ({ app, config }) => {
const clientCode = fs_1.default
.readFileSync(exports.clientFilePath, 'utf-8')
.replace(`__MODE__`, JSON.stringify(config.mode || 'development'))
.replace(`__DEFINES__`, JSON.stringify({
...config_1.defaultDefines,
...config.define
}));
app.use(async (ctx, next) => {
if (ctx.path === exports.clientPublicPath) {
const socketProtocol = ctx.protocol.toString() === 'https' ? 'wss' : 'ws';
let socketUrl = `${socketProtocol}://${ctx.host.toString()}`;
if (config.hmr && typeof config.hmr === 'object') {
// hmr option has highest priory
const protocol = config.hmr.protocol || socketProtocol;
const hostname = config.hmr.hostname || ctx.hostname;
const port = config.hmr.port || ctx.port;
socketUrl = `${protocol}://${hostname}:${port}`;
if (config.hmr.path) {
socketUrl = socketUrl + '/' + config.hmr.path;
}
}
ctx.type = 'js';
ctx.status = 200;
ctx.body = clientCode.replace(`__HOST__`, JSON.stringify(socketUrl));
}
else {
if (ctx.path === legacyPublicPath) {
console.error(chalk_1.default.red(`[vite] client import path has changed from "/vite/hmr" to "/vite/client". ` +
`please update your code accordingly.`));
}
return next();
}
});
};
//# sourceMappingURL=serverPluginClient.js.map