one
Version:
One is a new React Framework that makes Vite serve both native and web.
29 lines (28 loc) • 1.01 kB
JavaScript
const routeIdReturnRegex = /return\s*["'`]\.\/[^"'`]+["'`]/;
const routeIdReturnRegexFor = routeId => new RegExp(`return\\s*["'\`]${routeId.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}["'\`]`);
function replaceLoader({
code,
loaderData,
routeId
}) {
const stringifiedData = JSON.stringify(loaderData);
const safeData = stringifiedData.replace(/\$/g, "$$$$");
const out = (() => {
if (code.includes("__vxrn__loader__")) {
return code.replace(/["']__vxrn__loader__['"]/,
// make sure this ' ' is added in front,
// minifiers will do `return"something"
// but if its null then it becomes returnnull
" " + safeData);
}
const stubRegex = routeId ? routeIdReturnRegexFor(routeId) : routeIdReturnRegex;
if (stubRegex.test(code)) {
return code.replace(stubRegex, "return " + safeData);
}
return code + `
export const loader = () => (${stringifiedData})`;
})();
return out;
}
export { replaceLoader };
//# sourceMappingURL=replaceLoader.mjs.map