-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
140 lines (109 loc) · 3.57 KB
/
build.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import mdx from '@mdx-js/esbuild';
import esbuild from 'esbuild';
import * as fs from 'fs/promises';
import minimist from 'minimist';
import * as path from 'path';
import * as ReactDOMServer from 'react-dom/server';
import { jsx } from 'react/jsx-runtime';
import remarkGfm from 'remark-gfm';
import glob from 'tiny-glob';
import { fileURLToPath } from 'url';
const args = minimist(process.argv.slice(2));
let siteUrl = args['site-url'] ?? '/';
siteUrl = siteUrl.endsWith('/') ? siteUrl : siteUrl + '/';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Compile pages
let pagesSourceDirPath = await glob(__dirname + '/src/**/*.mdx', { absolute: true });
let pagesCompiledDirPath = __dirname + '/tmp/build/01';
await esbuild.build({
entryPoints: pagesSourceDirPath,
outdir: pagesCompiledDirPath,
format: 'esm',
plugins: [mdx({
allowDangerousRemoteMdx: true,
remarkPlugins: [remarkGfm]
})],
bundle: true,
minify: true
});
// Compile templates
let templatesCompiledDirPath = __dirname + '/tmp/build/02';
await esbuild.build({
entryPoints: await glob(__dirname + '/templates/*.jsx', { absolute: true }),
outdir: templatesCompiledDirPath,
inject: [__dirname + '/templates/jsx-factory.js']
});
// Import templates
let { default: template } = await import(templatesCompiledDirPath + '/index.js');
// Execute pages
let procPaths = await glob(pagesCompiledDirPath + '/**/*.js', { absolute: true });
let entries = [];
let tree = {
children: {},
entry: null
};
for (let absPath of procPaths) {
let relPath = path.relative(pagesCompiledDirPath, absPath);
// let outPath = __dirname + '/dist/' + relPath.replace(/\.js$/, '.html');
let imported = await import(absPath);
let addr = relPath.slice(0, -3);
let segments = addr.split('/');
let lastSegment = segments.at(-1);
let addrSegments = segments.slice(0, (lastSegment === 'index') ? -1 : Infinity);
let relTree = tree;
for (let segment of addrSegments) {
if (!(segment in relTree.children)) {
relTree.children[segment] = {
children: {},
entry: null
};
}
relTree = relTree.children[segment];
}
let entry = {
imported,
title: imported.title,
outPath: [...addrSegments, 'index'].join('/') + '.html',
outUrl: [siteUrl.slice(0, -1), ...addrSegments].join('/') + '/',
segments: addrSegments,
sourcePath: relPath
};
relTree.entry = entry;
entries.push(entry);
}
for (let entry of entries) {
let lastAncestor = tree;
let ancestors = entry.segments.map((segment) => {
let ancestor = lastAncestor.children[segment];
lastAncestor = ancestor;
return ancestor.entry;
});
let navigation;
if (entry.segments.length > 0) {
let firstAncestor = tree.children[entry.segments[0]];
navigation = Object.values(firstAncestor.children).map(({ children, entry }) => ({
children: Object.values(children).map(({ entry }) => ({ entry })),
entry
}));
} else {
navigation = Object.values(tree.children).map(({ entry }) => ({
children: null,
entry
}));
}
let outPath = __dirname + '/dist/' + entry.outPath;
let outDirPath = path.dirname(outPath);
let element = jsx(entry.imported.default, {});
let elementText = ReactDOMServer.renderToString(element);
let fullText = '<!DOCTYPE html>' + template({
breadcumb: [...ancestors.slice(0, -1), entry],
contents: elementText,
entry,
navigation,
sectionEntry: (ancestors[0] ?? null),
siteUrl,
title: entry.title ?? 'Untitled'
});
await fs.mkdir(outDirPath, { recursive: true });
await fs.writeFile(outPath, fullText);
}