UNPKG

2.66 kBJavaScriptView Raw
1/**
2 * Stencil Doc Outputs don't seem to support custom-elements.json as suggested
3 * here: https://github.com/w3c/webcomponents/issues/776#issuecomment-536749457.
4 * This generator implements this standard, which is used by Storybook to display
5 * documentation.
6 * Thanks to @jagreehal https://github.com/jagreehal/stencil-boilerplate/blob/master/src/customElementDocGenerator.ts
7 */
8var __rest = (this && this.__rest) || function (s, e) {
9 var t = {};
10 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
11 t[p] = s[p];
12 if (s != null && typeof Object.getOwnPropertySymbols === "function")
13 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
14 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
15 t[p[i]] = s[p[i]];
16 }
17 return t;
18};
19function isOutputTargetCustomElementDocsJson(o) {
20 return o.name === 'custom-element-docs';
21}
22export async function writeDocsOutput(compilerCtx, jsonContent, root) {
23 return compilerCtx.fs.writeFile(`${root}/dist/docs/custom-elements.json`, jsonContent);
24}
25export async function generateJsonDocs(config, compilerCtx, _buildCtx, docsData) {
26 const jsonOutputTargets = config.outputTargets.filter(isOutputTargetCustomElementDocsJson);
27 const { components } = docsData, docsDataWithoutComponents = __rest(docsData, ["components"]);
28 const json = Object.assign(Object.assign({}, docsDataWithoutComponents), { tags: components.map(cmp => ({
29 filePath: cmp.filePath,
30 encapsulation: cmp.encapsulation,
31 tag: cmp.tag,
32 name: cmp.tag,
33 readme: cmp.readme,
34 description: cmp.docs,
35 docsTags: cmp.docsTags,
36 usage: cmp.usage,
37 properties: cmp.props.map(prop => (Object.assign(Object.assign({}, prop), { description: prop.docs }))),
38 attributes: cmp.props.map(prop => (Object.assign(Object.assign({}, prop), { name: prop.attr, description: prop.docs }))),
39 methods: cmp.methods,
40 events: cmp.events.map(e => (Object.assign(Object.assign({}, e), { name: e.event, description: e.docs, type: e.detail }))),
41 styles: cmp.styles,
42 slots: cmp.slots,
43 dependents: cmp.dependents,
44 dependencies: cmp.dependencies,
45 dependencyGraph: cmp.dependencyGraph,
46 deprecation: cmp.deprecation,
47 })) });
48 const jsonContent = JSON.stringify(json, null, 2);
49 await Promise.all(jsonOutputTargets.map(() => {
50 return writeDocsOutput(compilerCtx, jsonContent, config.rootDir);
51 }));
52}