storybook
Version:
Storybook: Develop, document, and test UI components in isolation
144 lines (134 loc) • 6 kB
JavaScript
import CJS_COMPAT_NODE_URL_qjinduibsyq from 'node:url';
import CJS_COMPAT_NODE_PATH_qjinduibsyq from 'node:path';
import CJS_COMPAT_NODE_MODULE_qjinduibsyq from "node:module";
var __filename = CJS_COMPAT_NODE_URL_qjinduibsyq.fileURLToPath(import.meta.url);
var __dirname = CJS_COMPAT_NODE_PATH_qjinduibsyq.dirname(__filename);
var require = CJS_COMPAT_NODE_MODULE_qjinduibsyq.createRequire(import.meta.url);
// ------------------------------------------------------------
// end of CJS compatibility banner, injected by Storybook's esbuild configuration
// ------------------------------------------------------------
import {
createServiceRuntime,
getRegisteredServices,
getService,
isPlainObject,
isPrimitive,
isTypedArray,
registerService,
resolveStaticPath,
serviceRegistryApi,
validateSchema
} from "./chunk-5EYYAT2W.js";
import {
dirname,
join
} from "./chunk-PPVCVHOS.js";
// src/shared/open-service/server.ts
import { mkdir, writeFile } from "node:fs/promises";
// ../../node_modules/es-toolkit/dist/object/clone.mjs
function clone(obj) {
if (isPrimitive(obj))
return obj;
if (Array.isArray(obj) || isTypedArray(obj) || obj instanceof ArrayBuffer || typeof SharedArrayBuffer < "u" && obj instanceof SharedArrayBuffer)
return obj.slice(0);
let prototype = Object.getPrototypeOf(obj), Constructor = prototype.constructor;
if (obj instanceof Date || obj instanceof Map || obj instanceof Set)
return new Constructor(obj);
if (obj instanceof RegExp) {
let newRegExp = new Constructor(obj);
return newRegExp.lastIndex = obj.lastIndex, newRegExp;
}
if (obj instanceof DataView)
return new Constructor(obj.buffer.slice(0));
if (obj instanceof Error) {
let newError = new Constructor(obj.message);
return newError.stack = obj.stack, newError.name = obj.name, newError.cause = obj.cause, newError;
}
if (typeof File < "u" && obj instanceof File)
return new Constructor([obj], obj.name, { type: obj.type, lastModified: obj.lastModified });
if (typeof obj == "object") {
let newObject = Object.create(prototype);
return Object.assign(newObject, obj);
}
return obj;
}
// ../../node_modules/es-toolkit/dist/_internal/isUnsafeProperty.mjs
function isUnsafeProperty(key) {
return key === "__proto__";
}
// ../../node_modules/es-toolkit/dist/object/mergeWith.mjs
function mergeWith(target, source, merge2) {
let sourceKeys = Object.keys(source);
for (let i = 0; i < sourceKeys.length; i++) {
let key = sourceKeys[i];
if (isUnsafeProperty(key))
continue;
let sourceValue = source[key], targetValue = target[key], merged = merge2(targetValue, sourceValue, key, target, source);
merged !== void 0 ? target[key] = merged : Array.isArray(sourceValue) ? Array.isArray(targetValue) ? target[key] = mergeWith(targetValue, sourceValue, merge2) : target[key] = mergeWith([], sourceValue, merge2) : isPlainObject(sourceValue) ? isPlainObject(targetValue) ? target[key] = mergeWith(targetValue, sourceValue, merge2) : target[key] = mergeWith({}, sourceValue, merge2) : (targetValue === void 0 || sourceValue !== void 0) && (target[key] = sourceValue);
}
return target;
}
// ../../node_modules/es-toolkit/dist/object/toMerged.mjs
function toMerged(target, source) {
return mergeWith(clone(target), source, function mergeRecursively(targetValue, sourceValue) {
if (Array.isArray(sourceValue))
return Array.isArray(targetValue) ? mergeWith(clone(targetValue), sourceValue, mergeRecursively) : mergeWith([], sourceValue, mergeRecursively);
if (isPlainObject(sourceValue))
return isPlainObject(targetValue) ? mergeWith(clone(targetValue), sourceValue, mergeRecursively) : mergeWith({}, sourceValue, mergeRecursively);
});
}
// src/shared/open-service/server.ts
var getService2 = getService;
function registerService2(definition, registration) {
return registerService(definition, registration, { relay: !0 });
}
async function buildStaticFiles() {
let store = {}, buildTasks = [];
for (let service of getRegisteredServices())
for (let [queryName, query] of Object.entries(service.queries)) {
let { load, staticPath, staticInputs } = query;
!staticPath || !load || !staticInputs || buildTasks.push(
(async () => {
let inputsRuntime = createServiceRuntime(
service,
{ registryApi: serviceRegistryApi },
structuredClone(service.initialState)
), inputs = await staticInputs(inputsRuntime.loadCtxForStatic);
return Promise.all(
inputs.map(async (input) => {
let buildRuntime = createServiceRuntime(
service,
{ registryApi: serviceRegistryApi },
structuredClone(service.initialState)
), validatedInput = await validateSchema(query.input, input, {
kind: "query",
serviceId: service.id,
name: queryName,
phase: "input"
}), path = resolveStaticPath(service.id, queryName, { staticPath }, validatedInput);
return await buildRuntime.runLoadOnce(queryName, validatedInput), { path, state: buildRuntime.getStateSnapshot() };
})
);
})()
);
}
let builtStates = (await Promise.all(buildTasks)).flat();
for (let { path, state } of builtStates)
store[path] = path in store ? toMerged(store[path], state) : state;
return store;
}
async function writeOpenServiceStaticFiles(outputDir) {
let staticStore = await buildStaticFiles();
await Promise.all(
Object.entries(staticStore).map(async ([relativePath, state]) => {
let outputPath = join(outputDir, "services", ...relativePath.split("/"));
await mkdir(dirname(outputPath), { recursive: !0 }), await writeFile(outputPath, JSON.stringify(state, null, 2));
})
);
}
export {
toMerged,
getService2 as getService,
registerService2 as registerService,
writeOpenServiceStaticFiles
};