@leafage/renderer
Version:
The React Server Side Render Framework
73 lines (70 loc) • 1.98 kB
JavaScript
/**
* @leafage/renderer v1.3.2
*
* Copyright (c) Leafage.
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*/
import path from 'node:path';
import fs from 'node:fs';
import { match } from 'path-to-regexp';
import { utils } from '@leafage/toolkit';
var loadResourcePreset = function loadResourcePreset(ctx) {
var loadResourceHandle = function loadResourceHandle(mfs) {
var resources = [];
try {
var fullPath = path.join(ctx.options.dir.root, ctx.options.dir.dist, ctx.options.dir.manifest);
if (mfs.existsSync(fullPath)) {
var contents = mfs.readFileSync(fullPath, 'utf-8');
resources = JSON.parse(contents) || [];
}
} catch (err) {
resources = [];
}
return resources;
};
if (ctx.isDev) {
ctx.context.hook('bundle:resources', function (mfs) {
ctx.resources = loadResourceHandle(mfs);
});
return;
}
ctx.resources = loadResourceHandle(fs);
};
var findResourcePreset = function findResourcePreset(ctx) {
var map = new Map();
ctx.findResource = function (id) {
if (!id) return null;
if (map.has(id)) return map.get(id);
for (var i = 0, len = ctx.resources.length; i < len; i += 1) {
var row = ctx.resources[i];
// view find
if (row.view === id) {
map.set(id, row);
return row;
}
if (path.isAbsolute(id)) {
// path find
var match$1 = match(row.path, {
sensitive: true
});
var result = match$1(id);
if (result) {
var resource = utils.mergeProps(row, {
params: result.params || {}
});
map.set(id, resource);
return resource;
}
}
}
return null;
};
};
var resourcePreset = function resourcePreset(ctx) {
utils.applyPresets(ctx, [loadResourcePreset, findResourcePreset]);
};
export { resourcePreset };