/
rollup-plugin.ts
132 lines (112 loc) · 3.54 KB
/
rollup-plugin.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
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
import * as fs from "fs-extra";
import * as path from "path";
import * as fg from "fast-glob";
import writeTsDefinitions, { WriteTsDefinitionsOptions } from "./writer/writer-ts-definitions";
import writeJson, { WriteJsonOptions } from "./writer/writer-json";
import writeMarkdown, { WriteMarkdownOptions } from "./writer/writer-markdown";
import ComponentParser, { ParsedComponent } from "./ComponentParser";
import { getSvelteEntry } from "./get-svelte-entry";
import { ParsedExports, parseExports } from "./parse-exports";
import { preprocess } from "svelte/compiler";
import { replace, typescript } from "svelte-preprocess";
export interface PluginSveldOptions {
glob?: boolean;
types?: boolean;
typesOptions?: Partial<Omit<WriteTsDefinitionsOptions, "inputDir">>;
json?: boolean;
jsonOptions?: Partial<Omit<WriteJsonOptions, "inputDir">>;
markdown?: boolean;
markdownOptions?: Partial<WriteMarkdownOptions>;
}
type ComponentModuleName = string;
export interface ComponentDocApi extends ParsedComponent {
filePath: string;
moduleName: string;
}
export type ComponentDocs = Map<ComponentModuleName, ComponentDocApi>;
export default function pluginSveld(opts?: PluginSveldOptions) {
let result: GenerateBundleResult;
let input: string | null;
return {
name: "plugin-sveld",
buildStart() {
input = getSvelteEntry();
},
async generateBundle() {
if (input != null) {
result = await generateBundle(input, opts?.glob === true);
}
},
writeBundle() {
if (input != null) writeOutput(result, opts || {}, input);
},
};
}
interface GenerateBundleResult {
exports: ParsedExports;
components: ComponentDocs;
}
export async function generateBundle(input: string, glob: boolean) {
const dir = fs.lstatSync(input).isFile() ? path.dirname(input) : input;
const entry = fs.readFileSync(input, "utf-8");
const exports = parseExports(entry);
if (glob) {
fg.sync([`${dir}/**/*.svelte`]).forEach((file) => {
const moduleName = path.parse(file).name.replace(/\-/g, "");
const source = "./" + path.relative(dir, file);
if (exports[moduleName]) {
exports[moduleName].source = source;
}
});
}
const components: ComponentDocs = new Map();
const parser = new ComponentParser();
for (const [moduleName, entry] of Object.entries(exports)) {
const filePath = entry.source,
ext = path.parse(filePath).ext;
if (ext === ".svelte") {
const source = await fs.readFile(path.resolve(dir, filePath), "utf-8");
const { code: processed } = await preprocess(source, [typescript(), replace([[/<style.+<\/style>/gims, ""]])], {
filename: path.basename(filePath),
});
components.set(moduleName, {
moduleName,
filePath,
...parser.parseSvelteComponent(processed, {
moduleName,
filePath,
}),
});
}
}
return {
exports,
components,
};
}
export function writeOutput(result: GenerateBundleResult, opts: PluginSveldOptions, input: string) {
const inputDir = path.dirname(input);
if (opts?.types !== false) {
writeTsDefinitions(result.components, {
outDir: "types",
preamble: "",
...opts?.typesOptions,
exports: result.exports,
inputDir,
});
}
if (opts?.json) {
writeJson(result.components, {
outFile: "COMPONENT_API.json",
...opts?.jsonOptions,
input,
inputDir,
});
}
if (opts?.markdown) {
writeMarkdown(result.components, {
outFile: "COMPONENT_INDEX.md",
...opts?.markdownOptions,
});
}
}