Skip to content

Commit

Permalink
fix: Update serializeDefine implementation from upstream vite (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasiliicuhar authored Jun 4, 2024
1 parent d1604e2 commit 088e074
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,26 @@ export function hmrRewritePlugin(config: {
: undefined;

// Coped from node_modules/vite, do a global search for: vite:client-inject
function serializeDefine(define: any): string {
function serializeDefine(define: Record<string, any>): string {
let res = `{`;
for (const key in define) {
const keys = Object.keys(define);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const val = define[key];
res += `${JSON.stringify(key)}: ${
typeof val === "string" ? `(${val})` : JSON.stringify(val)
}, `;
res += `${JSON.stringify(key)}: ${handleDefineValue(val)}`;
if (i !== keys.length - 1) {
res += `, `;
}
}
return res + `}`;
}

function handleDefineValue(value: any): string {
if (typeof value === "undefined") return "undefined";
if (typeof value === "string") return value;
return JSON.stringify(value);
}

return {
name: HMR_REWRITE_PLUGIN_NAME,

Expand Down

0 comments on commit 088e074

Please sign in to comment.