one
Version:
One is a new React Framework that makes Vite serve both native and web.
127 lines (125 loc) • 4.56 kB
JavaScript
import { extname, relative } from "node:path";
import BabelGenerate from "@babel/generator";
import { parse } from "@babel/parser";
import BabelTraverse from "@babel/traverse";
import {
deadCodeElimination,
findReferencedIdentifiers
} from "babel-dead-code-elimination";
import { EMPTY_LOADER_STRING } from "../constants";
const traverse = BabelTraverse.default || BabelTraverse, generate = BabelGenerate.default || BabelGenerate;
function collectTypeImports(ast) {
const typeImports = [];
return traverse(ast, {
ImportDeclaration(path) {
path.node.importKind === "type" && typeImports.push(path.node);
}
}), typeImports;
}
function restoreTypeImports(ast, typeImports) {
if (typeImports.length === 0) return;
const existingSources = /* @__PURE__ */ new Set();
traverse(ast, {
ImportDeclaration(path) {
path.node.importKind === "type" && existingSources.add(path.node.source.value);
}
});
for (const typeImport of typeImports)
existingSources.has(typeImport.source.value) || ast.program.body.unshift(typeImport);
}
const clientTreeShakePlugin = () => ({
name: "one-client-tree-shake",
enforce: "pre",
applyToEnvironment(env) {
return env.name === "client" || env.name === "ios" || env.name === "android";
},
transform: {
order: "pre",
async handler(code, id, settings) {
return this.environment.name === "ssr" || !/\.(js|jsx|ts|tsx)/.test(extname(id)) || /node_modules/.test(id) ? void 0 : await transformTreeShakeClient(code, id);
}
}
});
async function transformTreeShakeClient(code, id) {
if (!/generateStaticParams|loader/.test(code))
return;
let ast;
try {
ast = parse(code, {
sourceType: "module",
plugins: ["typescript", "jsx"]
});
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
console.warn(
`[one] Skipping tree shaking for ${id} due to syntax error:`,
errorMessage
);
return;
}
let referenced;
try {
referenced = findReferencedIdentifiers(ast);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
console.warn(
`[one] Skipping tree shaking for ${id} due to identifier analysis error:`,
errorMessage
);
return;
}
const removed = {
loader: !1,
generateStaticParams: !1
};
try {
traverse(ast, {
ExportNamedDeclaration(path) {
if (path.node.declaration && path.node.declaration.type === "FunctionDeclaration") {
if (!path.node.declaration.id) return;
const functionName = path.node.declaration.id.name;
(functionName === "loader" || functionName === "generateStaticParams") && (path.remove(), removed[functionName] = !0);
} else path.node.declaration && path.node.declaration.type === "VariableDeclaration" && path.node.declaration.declarations.forEach((declarator, index) => {
if (declarator.id.type === "Identifier" && (declarator.id.name === "loader" || declarator.id.name === "generateStaticParams")) {
const declaration = path.get("declaration.declarations." + index);
!Array.isArray(declaration) && declaration && (declaration.remove(), removed[declarator.id.name] = !0);
}
});
}
});
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
console.warn(
`[one] Skipping tree shaking for ${id} due to traversal error:`,
errorMessage
);
return;
}
const removedFunctions = Object.keys(removed).filter((key) => removed[key]);
if (removedFunctions.length)
try {
const typeImports = collectTypeImports(ast);
deadCodeElimination(ast, referenced), restoreTypeImports(ast, typeImports);
const out = generate(ast), codeOut = out.code + `
` + removedFunctions.map((key) => key === "loader" ? EMPTY_LOADER_STRING : "export function generateStaticParams() {};").join(`
`);
return console.info(
` \u{1F9F9} [one] ${relative(process.cwd(), id)} removed ${removedFunctions.length} server-only exports`
), {
code: codeOut,
map: out.map
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
console.warn(
`[one] Skipping tree shaking for ${id} due to code generation error:`,
errorMessage
);
return;
}
}
export {
clientTreeShakePlugin,
transformTreeShakeClient
};
//# sourceMappingURL=clientTreeShakePlugin.js.map