one
Version:
One is a new React Framework that makes Vite serve both native and web.
132 lines (131 loc) • 8.46 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 createApiServerlessFunction_exports = {};
__export(createApiServerlessFunction_exports, {
createApiServerlessFunction: () => createApiServerlessFunction
});
module.exports = __toCommonJS(createApiServerlessFunction_exports);
var import_node_path = require("node:path"), import_generator = __toESM(require("@babel/generator"), 1), import_parser = __toESM(require("@babel/parser"), 1), import_traverse = __toESM(require("@babel/traverse"), 1), import_types = __toESM(require("@babel/types"), 1), import_resolve = require("@vxrn/resolve"), import_fs_extra = __toESM(require("fs-extra"), 1), import_fs_extra2 = __toESM(require("fs-extra"), 1), import_vc_config_base = require("../config/vc-config-base"), import_vc_package_base = require("../config/vc-package-base"), import_getPathFromRoute = require("../getPathFromRoute");
async function createApiServerlessFunction(route, code, oneOptionsRoot, postBuildLogs) {
try {
const path = (0, import_getPathFromRoute.getPathFromRoute)(route, { includeIndex: !0 });
postBuildLogs.push(`[one.build][vercel.createSsrServerlessFunction] pageName: ${path}`);
const funcFolder = (0, import_node_path.join)(oneOptionsRoot, `.vercel/output/functions/${path}.func`);
if (await import_fs_extra2.default.ensureDir(funcFolder), code.includes("react")) {
postBuildLogs.push(
`[one.build][vercel.createSsrServerlessFunction] detected react in depenency tree for ${path}`
);
const reactPath = (0, import_node_path.dirname)((0, import_resolve.resolvePath)("react/package.json", oneOptionsRoot));
await import_fs_extra2.default.copy((0, import_node_path.resolve)(reactPath), (0, import_node_path.resolve)((0, import_node_path.join)(funcFolder, "node_modules", "react")));
}
const distAssetsFolder = (0, import_node_path.resolve)((0, import_node_path.join)(funcFolder, "assets"));
postBuildLogs.push(
`[one.build][vercel.createSsrServerlessFunction] copy shared assets to ${distAssetsFolder}`
);
const sourceAssetsFolder = (0, import_node_path.resolve)((0, import_node_path.join)(oneOptionsRoot, "dist", "api", "assets"));
await import_fs_extra.default.pathExists(sourceAssetsFolder) && await import_fs_extra2.default.copy(sourceAssetsFolder, distAssetsFolder), await import_fs_extra2.default.ensureDir((0, import_node_path.resolve)((0, import_node_path.join)(funcFolder, "entrypoint")));
const entrypointFilePath = (0, import_node_path.resolve)((0, import_node_path.join)(funcFolder, "entrypoint", "index.js"));
postBuildLogs.push(
`[one.build][vercel.createSsrServerlessFunction] writing entrypoint to ${entrypointFilePath}`
), await import_fs_extra2.default.writeFile(entrypointFilePath, wrapHandlerFunctions(code));
const packageJsonFilePath = (0, import_node_path.resolve)((0, import_node_path.join)(funcFolder, "package.json"));
return postBuildLogs.push(
`[one.build][vercel.createSsrServerlessFunction] writing package.json to ${packageJsonFilePath}`
), await import_fs_extra2.default.writeJSON(packageJsonFilePath, import_vc_package_base.serverlessVercelPackageJson), postBuildLogs.push(
`[one.build][vercel.createSsrServerlessFunction] writing .vc-config.json to ${(0, import_node_path.join)(funcFolder, ".vc-config.json")}`
), import_fs_extra2.default.writeJson((0, import_node_path.join)(funcFolder, ".vc-config.json"), {
...import_vc_config_base.serverlessVercelNodeJsConfig,
handler: "entrypoint/index.js"
});
} catch (e) {
console.error(
`[one.build][vercel.createSsrServerlessFunction] failed to generate func for ${route.file}`,
e
);
}
}
function wrapHandlerFunctions(code) {
const ast = import_parser.default.parse(code, {
sourceType: "module"
});
return import_traverse.default.default(ast, {
FunctionDeclaration(path) {
const { node } = path, functionNamesToHandle = [
"GET",
"POST",
"PUT",
"PATCH",
"DELETE",
"HEAD",
"OPTIONS"
// TODO: more possibilities?
];
if (!node.id || !functionNamesToHandle.includes(node.id.name) || node.extra && node.extra.isWrapper || node.extra && node.extra.isWrapped) return;
const originalName = `orig_${node.id.name}`, originalFunction = import_types.default.functionDeclaration(
import_types.default.identifier(originalName),
node.params,
node.body,
node.generator,
node.async
), requestIdentifier = import_types.default.identifier("request"), wrapperParams = [requestIdentifier], urlIdentifier = import_types.default.identifier("url"), paramsIdentifier = import_types.default.identifier("params"), urlDecl = import_types.default.variableDeclaration("const", [
import_types.default.variableDeclarator(
urlIdentifier,
import_types.default.newExpression(import_types.default.identifier("URL"), [
import_types.default.memberExpression(requestIdentifier, import_types.default.identifier("url"))
])
)
]), paramsDecl = import_types.default.variableDeclaration("const", [
import_types.default.variableDeclarator(
paramsIdentifier,
import_types.default.callExpression(
import_types.default.memberExpression(import_types.default.identifier("Object"), import_types.default.identifier("fromEntries")),
[
import_types.default.callExpression(
import_types.default.memberExpression(
import_types.default.memberExpression(
urlIdentifier,
import_types.default.identifier("searchParams")
),
import_types.default.identifier("entries")
),
[]
)
]
)
)
]), callOrigFnStatement = import_types.default.callExpression(import_types.default.identifier(originalName), [
requestIdentifier,
import_types.default.objectExpression([import_types.default.objectProperty(import_types.default.identifier("params"), paramsIdentifier)])
]), wrapperFunction = import_types.default.functionDeclaration(
import_types.default.identifier(node.id.name + ""),
wrapperParams,
import_types.default.blockStatement([urlDecl, paramsDecl, import_types.default.returnStatement(callOrigFnStatement)])
// No need to care if the wrapper function should be async,
// since we didn't use any await in the wrapper function, and we'll
// just return what the original function returns.
);
node.extra = node.extra || {}, node.extra.isWrapped = !0, wrapperFunction.extra = wrapperFunction.extra || {}, wrapperFunction.extra.isWrapper = !0, path.parentPath.isExportNamedDeclaration() ? path.replaceWithMultiple([originalFunction, import_types.default.exportNamedDeclaration(wrapperFunction, [])]) : path.replaceWithMultiple([originalFunction, wrapperFunction]);
}
}), import_generator.default.default(ast, {}).code;
}
//# sourceMappingURL=createApiServerlessFunction.js.map