convex
Version:
Client for the Convex Cloud
196 lines (195 loc) • 7.09 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__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(
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var bundler_exports = {};
__export(bundler_exports, {
BundleError: () => BundleError,
RecordingFs: () => import_fs2.RecordingFs,
actionsDir: () => actionsDir,
actionsEntryPoints: () => actionsEntryPoints,
allEntryPoints: () => allEntryPoints,
bundle: () => bundle,
bundleSchema: () => bundleSchema,
databaseEntryPoints: () => databaseEntryPoints,
nodeFs: () => import_fs2.nodeFs,
walkDir: () => walkDir
});
module.exports = __toCommonJS(bundler_exports);
var import_path = __toESM(require("path"));
var import_chalk = __toESM(require("chalk"));
var import_esbuild = __toESM(require("esbuild"));
var import_fs2 = require("./fs.js");
const actionsDir = "actions";
function* walkDir(fs, dirPath) {
for (const dirEntry of fs.listDir(dirPath)) {
const childPath = import_path.default.join(dirPath, dirEntry.name);
if (dirEntry.isDirectory()) {
yield { isDir: true, path: childPath };
yield* walkDir(fs, childPath);
} else if (dirEntry.isFile()) {
yield { isDir: false, path: childPath };
}
}
}
class BundleError extends Error {
}
async function doEsbuild(fs, dir, entryPoints2, generateSourceMaps, platform, chunksFolder) {
try {
const result = await import_esbuild.default.build({
entryPoints: entryPoints2,
bundle: true,
platform,
format: "esm",
target: "esnext",
outdir: "out",
outbase: dir,
write: false,
sourcemap: generateSourceMaps,
splitting: true,
chunkNames: import_path.default.join(chunksFolder, "[hash]"),
treeShaking: true,
minify: false,
metafile: true
});
for (const [relPath, input] of Object.entries(result.metafile.inputs)) {
if (relPath.indexOf("(disabled):") !== -1) {
continue;
}
const absPath = import_path.default.resolve(relPath);
const st = fs.stat(absPath);
if (st.size !== input.bytes) {
throw new Error(
`Bundled file ${absPath} changed right after esbuild invocation`
);
}
fs.registerPath(absPath, st);
}
return result;
} catch (err) {
throw new BundleError(`esbuild failed: ${err.toString()}`);
}
}
async function bundle(fs, dir, entryPoints2, generateSourceMaps, platform, chunksFolder = "_deps") {
const result = await doEsbuild(
fs,
dir,
entryPoints2,
generateSourceMaps,
platform,
chunksFolder
);
if (result.errors.length) {
for (const error of result.errors) {
console.log(import_chalk.default.red(`esbuild error: ${error.text}`));
}
throw new BundleError("esbuild failed");
}
for (const warning of result.warnings) {
console.log(import_chalk.default.yellow(`esbuild warning: ${warning.text}`));
}
const sourceMaps = /* @__PURE__ */ new Map();
const modules = [];
for (const outputFile of result.outputFiles) {
const relPath = import_path.default.relative(import_path.default.normalize("out"), outputFile.path);
if (import_path.default.extname(relPath) === ".map") {
sourceMaps.set(relPath, outputFile.text);
continue;
}
const posixRelPath = relPath.split(import_path.default.sep).join(import_path.default.posix.sep);
modules.push({ path: posixRelPath, source: outputFile.text });
}
for (const module2 of modules) {
const sourceMapPath = module2.path + ".map";
const sourceMap = sourceMaps.get(sourceMapPath);
if (sourceMap) {
module2.sourceMap = sourceMap;
}
}
return modules;
}
async function bundleSchema(fs, dir) {
return bundle(fs, dir, [import_path.default.resolve(dir, "schema.ts")], true, "neutral");
}
function escapeRegex(s) {
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
async function entryPoints(fs, dir, includePattern, verbose) {
const entryPoints2 = [];
for (const { isDir, path: fpath } of walkDir(fs, dir)) {
if (isDir) {
continue;
}
const relPath = import_path.default.relative(dir, fpath);
const base = import_path.default.parse(fpath).base;
const log = (line) => {
if (verbose) {
console.log(line);
}
};
if (!relPath.match(includePattern)) {
continue;
} else if (relPath.startsWith("_deps" + import_path.default.sep)) {
throw new Error(
`The path "${fpath}" is within the "_deps" directory, which is reserved for dependencies. Please move your code to another directory.`
);
} else if (relPath.startsWith("_generated" + import_path.default.sep)) {
log(import_chalk.default.yellow(`Skipping ${fpath}`));
} else if (base.startsWith(".")) {
log(import_chalk.default.yellow(`Skipping dotfile ${fpath}`));
} else if (base === "README.md") {
log(import_chalk.default.yellow(`Skipping ${fpath}`));
} else if (base === "_generated.ts") {
log(import_chalk.default.yellow(`Skipping ${fpath}`));
} else if (base === "schema.ts") {
log(import_chalk.default.yellow(`Skipping ${fpath}`));
} else if (base.includes(".test.")) {
log(import_chalk.default.yellow(`Skipping ${fpath}`));
} else if (base === "tsconfig.json") {
log(import_chalk.default.yellow(`Skipping ${fpath}`));
} else if (relPath.includes(" ")) {
log(import_chalk.default.yellow(`Skipping ${relPath} because it contains a space`));
} else {
log(import_chalk.default.green(`Preparing ${fpath}`));
entryPoints2.push(fpath);
}
}
return entryPoints2;
}
async function allEntryPoints(fs, dir, verbose) {
return entryPoints(fs, dir, new RegExp(".*"), verbose);
}
async function databaseEntryPoints(fs, dir, verbose) {
const excludePrefix = actionsDir + import_path.default.sep;
return entryPoints(
fs,
dir,
RegExp(`^(?!${escapeRegex(excludePrefix)})`),
verbose
);
}
async function actionsEntryPoints(fs, dir, verbose) {
const prefix = actionsDir + import_path.default.sep;
return entryPoints(fs, dir, RegExp(`^${escapeRegex(prefix)}`), verbose);
}
//# sourceMappingURL=index.js.map