This repository was archived by the owner on Sep 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarko.ts
More file actions
96 lines (89 loc) · 3.14 KB
/
marko.ts
File metadata and controls
96 lines (89 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* eslint-disable @typescript-eslint/no-shadow */
import fs from 'fs';
import path from 'path';
// import { createRequire } from 'module';
// @ts-ignore
import Marko from 'marko';
import 'marko/node-require';
// import FastGlob from 'fast-glob';
import type { Repack } from './types';
// const require = createRequire(import.meta.url);
const defaultRewrite = (path: string) => path.replace(/^.*?tpl\//, 'web/html/').replace(/\.marko$/, '.html');
const trimDirectoryPrefix = (str: string) =>
str.replace(/^.*?tpl\//, '').replace(/^.*?web\/html\//, '');
// const markoComponentFiles = FastGlob('tpl/**/components/*.marko');
// const preloadComponentsWorkaround = async () => {
// let iterations = 0;
// const LIMIT = 10;
// const load = (file: string) => {
// try {
// Marko.load(file);
// } catch (error) {
// return false;
// }
// return true;
// };
// const files = await markoComponentFiles;
// const set = new Set([...files]);
// while (set.size > 0) {
// if (++iterations > LIMIT) {
// throw new Error(`Marko component preloading workaround
// failed after trying to load ${[...set]} after ${iterations} times.`);
// }
// for (const file of set) {
// const result = load(file);
// if (result === false) {
// // console.debug(`${file} failed`);
// // eslint-disable-next-line no-use-before-define
// marko.delete(file);
// } else {
// set.delete(file);
// }
// }
// }
// };
const marko = (repack: Repack) => {
const marko = ({ rewrite = defaultRewrite } = {}) =>
async (filename: string, config: any) => {
// await preloadComponentsWorkaround();
const outFile = rewrite(filename);
console.debug(`… ${trimDirectoryPrefix(filename)} → ${trimDirectoryPrefix(outFile)}`);
// const template = Marko.load(filename);
const template = await import(path.join(process.cwd(), filename));
const dirname = path.dirname(outFile);
await fs.promises.mkdir(dirname, { recursive: true });
const data = { ...(await config), $global: { repack } };
const output = (await (template.default ?? template).render(data)).getOutput();
await fs.promises.writeFile(outFile, output);
console.debug(`✓ ${trimDirectoryPrefix(filename)} → ${trimDirectoryPrefix(outFile)}`);
};
marko.render = async (markup: string, data: any) => {
if (!markup) {
return undefined;
}
// await preloadComponentsWorkaround();
const fauxTemplate = Marko
.load(
`${process.cwd()}/tpl/${data.uri || Math.random()}.marko`,
markup,
{ buffer: true, writeToDisk: false },
);
const payload = { ...data, $global: { repack, ...data.$global } };
const renderedTemplate = await fauxTemplate.render(payload);
return renderedTemplate.getOutput();
};
return marko;
};
marko.delete = (filename?: string) => {
for (const file of Object.keys(require.cache)) {
if (filename) {
if (file.includes(filename)) {
delete require.cache[file];
// console.debug(`purge ${file}`);
}
} else {
delete require.cache[file];
}
}
};
export default marko;