one
Version:
One is a new React Framework that makes Vite serve both native and web.
105 lines • 5.51 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf,
__hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: !0
});
},
__copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: !0
}) : target, mod)),
__toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
value: !0
}), mod);
var remove_server_code_exports = {};
__export(remove_server_code_exports, {
default: () => remove_server_code_default
});
module.exports = __toCommonJS(remove_server_code_exports);
var t = __toESM(require("@babel/types"), 1),
import_babel_dead_code_elimination = require("babel-dead-code-elimination");
const SERVER_EXPORTS = ["loader", "generateStaticParams"];
function removeServerCodePlugin(_, options) {
const {
routerRoot = "app"
} = options;
return {
name: "one-remove-server-code",
visitor: {
Program: {
enter(path, state) {
const filename = state.filename;
if (!filename || !new RegExp(`[/\\\\]${routerRoot}[/\\\\]`).test(filename) || filename.includes("node_modules")) return;
const code = path.toString();
if (/generateStaticParams|loader/.test(code)) try {
state.referenced = (0, import_babel_dead_code_elimination.findReferencedIdentifiers)(path.node);
} catch (error) {
console.warn(`[one/metro] Skipping tree shaking for ${filename} due to identifier analysis error:`, error instanceof Error ? error.message : String(error));
}
},
exit(path, state) {
const filename = state.filename;
if (!filename || !new RegExp(`[/\\\\]${routerRoot}[/\\\\]`).test(filename) || filename.includes("node_modules") || !state.referenced) return;
const removedExports = /* @__PURE__ */new Set();
if (path.traverse({
ExportNamedDeclaration(exportPath) {
const declaration = exportPath.node.declaration;
if (t.isFunctionDeclaration(declaration) && declaration.id) {
const name = declaration.id.name;
SERVER_EXPORTS.includes(name) && (removedExports.add(name), exportPath.remove());
} else if (t.isVariableDeclaration(declaration)) for (let i = declaration.declarations.length - 1; i >= 0; i--) {
const declarator = declaration.declarations[i];
if (t.isIdentifier(declarator.id)) {
const name = declarator.id.name;
SERVER_EXPORTS.includes(name) && (removedExports.add(name), declaration.declarations.length === 1 ? exportPath.remove() : declaration.declarations.splice(i, 1));
}
}
},
// Also remove helper functions created by babel's async-to-generator transform
// These are named _loader, _generateStaticParams, etc.
FunctionDeclaration(funcPath) {
if (!funcPath.node.id) return;
const name = funcPath.node.id.name;
for (const serverExport of SERVER_EXPORTS) if (name === `_${serverExport}`) {
let isAsyncHelper = !1;
funcPath.traverse({
Identifier(idPath) {
idPath.node.name === "_asyncToGenerator" && (isAsyncHelper = !0, idPath.stop());
}
}), isAsyncHelper && (removedExports.add(serverExport), funcPath.remove());
}
}
}), removedExports.size === 0) return;
try {
(0, import_babel_dead_code_elimination.deadCodeElimination)(path.node, state.referenced);
} catch (error) {
console.warn(`[one/metro] Dead code elimination failed for ${filename}:`, error instanceof Error ? error.message : String(error));
}
const stubs = [];
removedExports.has("loader") && stubs.push(t.exportNamedDeclaration(t.functionDeclaration(t.identifier("loader"), [], t.blockStatement([t.returnStatement(t.stringLiteral("__vxrn__loader__"))])))), removedExports.has("generateStaticParams") && stubs.push(t.exportNamedDeclaration(t.functionDeclaration(t.identifier("generateStaticParams"), [], t.blockStatement([]))));
for (const stub of stubs) path.pushContainer("body", stub);
console.info(` \u{1F9F9} [one/metro] ${filename} removed ${removedExports.size} server-only exports`);
}
}
}
};
}
var remove_server_code_default = removeServerCodePlugin;