@maizzle/framework
Version:
Maizzle is a framework that helps you quickly build HTML emails with Tailwind CSS.
50 lines (49 loc) • 1.8 kB
JavaScript
import { relative, resolve } from "pathe";
//#region src/utils/componentSources.ts
/**
* Normalize a user-supplied `components.source` value into an array
* of absolute, fully-defaulted entries.
*/
function normalizeComponentSources(sources, cwd) {
if (!sources) return [];
return (Array.isArray(sources) ? sources : [sources]).map((s) => {
if (typeof s === "string") return {
path: resolve(cwd, s),
prefix: void 0,
pathPrefix: true
};
return {
path: resolve(cwd, s.path),
prefix: s.prefix,
pathPrefix: s.pathPrefix ?? true
};
});
}
function pascalCase(s) {
return s.replace(/[-_\s]+(.)/g, (_, c) => c.toUpperCase()).replace(/^(.)/, (c) => c.toUpperCase());
}
/**
* Compute the component name unplugin-vue-components would assign for a
* given file under a given dir, mirroring the plugin's
* `directoryAsNamespace: true` + `collapseSamePrefixes: true` behavior
* and layering custom-prefix sources on top.
*
* Used both at render time (to register a custom resolver) and at lint
* time (so the linter can follow component graph correctly).
*/
function componentNameFromPath(opts) {
const { filePath, dirRoot, prefix, pathPrefix } = opts;
const segments = relative(dirRoot, filePath).replace(/\.(vue|md)$/, "").split("/").map(pascalCase);
const fileName = segments.pop() ?? "";
if (prefix !== void 0) {
const folderPart = pathPrefix ? segments.join("") : "";
const stripped = prefix && fileName.startsWith(prefix) ? fileName.slice(prefix.length) : fileName;
return prefix + folderPart + stripped;
}
const folderPart = segments.join("");
if (folderPart && fileName.startsWith(folderPart)) return fileName;
return folderPart + fileName;
}
//#endregion
export { componentNameFromPath, normalizeComponentSources };
//# sourceMappingURL=componentSources.js.map