forked from fuma-nama/fumadocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-config.ts
36 lines (28 loc) · 1.03 KB
/
build-config.ts
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
import path from 'node:path';
import fs from 'node:fs/promises';
function getOutPath(src: string): string {
const replacedPath = src
.split('/')
.map((v) => (v === 'src' ? 'dist' : v))
.join('/');
const info = path.parse(replacedPath);
return path.join('./', info.dir, `${info.name}.js`).replace(path.sep, '/');
}
/**
* Inject imports for client components
*/
export async function injectImport(src: string): Promise<void> {
const srcOut = getOutPath(src);
const sourceContent = (await fs.readFile(src)).toString();
let outContent = (await fs.readFile(srcOut)).toString();
const regex =
/^declare const {(?<names>(?:.|\n)*?)}: typeof import\((?<from>.+)\)/gm;
let result;
while ((result = regex.exec(sourceContent)) && result.groups) {
const { from, names } = result.groups;
const importName = from.slice(1, from.length - 1);
const replaceTo = `import {${names}} from ${JSON.stringify(importName)}`;
outContent = `${replaceTo}\n${outContent}`;
}
await fs.writeFile(srcOut, outContent);
}