@renr/parcel-reporter-docgen 0.0.1-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/esm/generate-props.mjs +89 -0
- package/dist/esm/generate-props.mjs.map +1 -0
- package/dist/esm/index.d.mts +7 -0
- package/dist/esm/index.d.mts.map +1 -0
- package/dist/esm/index.mjs +70 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/package.mjs +6 -0
- package/dist/esm/package.mjs.map +1 -0
- package/package.json +45 -0
- package/src/index.ts +96 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Ren
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fileURLToPath } from "url";
|
|
3
|
+
import fs from "fs/promises";
|
|
4
|
+
import { withCustomConfig } from "react-docgen-typescript";
|
|
5
|
+
import { glob } from "tinyglobby";
|
|
6
|
+
//#region src/generate-props.ts
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
path.dirname(__filename);
|
|
9
|
+
async function generateProps(pagesPattern, rootDir, outputPath, log = console.log) {
|
|
10
|
+
const docgenParser = withCustomConfig(path.resolve(rootDir, "./tsconfig.json"), {
|
|
11
|
+
shouldExtractLiteralValuesFromEnum: true,
|
|
12
|
+
savePropValueAsString: true,
|
|
13
|
+
propFilter: (prop) => {
|
|
14
|
+
if (Array.isArray(prop.declarations) && prop.declarations[0]) {
|
|
15
|
+
if (prop.declarations[0].fileName.includes("node_modules/@types/react")) {
|
|
16
|
+
if (prop.name !== "ref" && prop.name !== "className") return false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
const files = await glob(pagesPattern, { cwd: rootDir });
|
|
23
|
+
if (files.length === 0) {
|
|
24
|
+
log(`No files found for pattern: ${pagesPattern}`);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
log(`Found ${files.length} files for pattern: ${pagesPattern}`);
|
|
28
|
+
await fs.mkdir(path.resolve(rootDir, outputPath), { recursive: true });
|
|
29
|
+
try {
|
|
30
|
+
for (const file of files) {
|
|
31
|
+
log(`parsing file: : ${file}`);
|
|
32
|
+
const componentName = path.basename(file, path.extname(file));
|
|
33
|
+
const filePath = path.join(rootDir, file);
|
|
34
|
+
const docs = docgenParser.parse(filePath);
|
|
35
|
+
const props = docs[0] ? docs[0].props : {};
|
|
36
|
+
for (const propName in props) {
|
|
37
|
+
const prop = props[propName];
|
|
38
|
+
if (!prop) throw new Error(`No prop found for ${propName} in ${componentName}`);
|
|
39
|
+
if (prop.type && prop.type.name) {
|
|
40
|
+
const { type: shortPropTypeName, detailedType } = getShortPropType(propName, prop.type.name);
|
|
41
|
+
const hasExpandedType = Boolean(detailedType);
|
|
42
|
+
prop.type.name = hasExpandedType && prop.type.name.split("|").length > 3 ? prop.type.name.split("|").map((line) => `| ${line}\n`).join("") : prop.type.name;
|
|
43
|
+
prop.shortPropTypeName = hasExpandedType ? shortPropTypeName : null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const componentJSON = JSON.stringify({
|
|
47
|
+
name: componentName,
|
|
48
|
+
path: file,
|
|
49
|
+
fileName: file.split("/").pop(),
|
|
50
|
+
props
|
|
51
|
+
}, null, 2);
|
|
52
|
+
const outputFilePath = path.join(outputPath, `${componentName}.json`);
|
|
53
|
+
await fs.writeFile(outputFilePath, componentJSON + "\n", "utf-8");
|
|
54
|
+
log(`Generated props for ${componentName}`);
|
|
55
|
+
}
|
|
56
|
+
} catch (err) {
|
|
57
|
+
log(`Error generating props: ${err.message}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function getShortPropType(name, type) {
|
|
61
|
+
if (/^(on|get)[A-Z].*/.test(name)) return {
|
|
62
|
+
type: "function",
|
|
63
|
+
detailedType: true
|
|
64
|
+
};
|
|
65
|
+
if (type === void 0 || type === null) return {
|
|
66
|
+
type: String(type),
|
|
67
|
+
detailedType: false
|
|
68
|
+
};
|
|
69
|
+
if (name === "className") return {
|
|
70
|
+
type: "string | function",
|
|
71
|
+
detailedType: true
|
|
72
|
+
};
|
|
73
|
+
if (name === "render") return {
|
|
74
|
+
type: "ReactElement | function",
|
|
75
|
+
detailedType: true
|
|
76
|
+
};
|
|
77
|
+
if (name.endsWith("Ref") || name === "children" || type === "boolean" || type === "string" || type === "number" || type.indexOf(" | ") === -1 || type.split("|").length < 3 && type.length < 30) return {
|
|
78
|
+
type,
|
|
79
|
+
detailedType: false
|
|
80
|
+
};
|
|
81
|
+
return {
|
|
82
|
+
type: "Union",
|
|
83
|
+
detailedType: true
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
//#endregion
|
|
87
|
+
export { generateProps };
|
|
88
|
+
|
|
89
|
+
//# sourceMappingURL=generate-props.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate-props.mjs","names":[],"sources":["../../src/generate-props.ts"],"sourcesContent":["import fs from \"fs/promises\";\nimport path from \"path\";\nimport { PropItem, withCustomConfig } from \"react-docgen-typescript\";\nimport { glob } from \"tinyglobby\";\nimport { fileURLToPath } from \"url\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\nexport async function generateProps(\n pagesPattern: string,\n rootDir: string,\n outputPath: string,\n log = console.log\n) {\n // TODO - should this be the root tsconfig, the parcel project tsconfig, or the ui library tsconfig?\n // TODO - make this configurable\n const tsconfigPath = path.resolve(rootDir, \"./tsconfig.json\");\n\n const docgenParser = withCustomConfig(tsconfigPath, {\n shouldExtractLiteralValuesFromEnum: true,\n savePropValueAsString: true,\n propFilter: (prop) => {\n if (Array.isArray(prop.declarations) && prop.declarations[0]) {\n const declaration = prop.declarations[0];\n\n if (declaration.fileName.includes(\"node_modules/@types/react\")) {\n if (prop.name !== \"ref\" && prop.name !== \"className\") {\n return false;\n }\n }\n }\n return true;\n },\n });\n\n const files = await glob(pagesPattern, {\n cwd: rootDir,\n });\n\n if (files.length === 0) {\n log(`No files found for pattern: ${pagesPattern}`);\n return;\n }\n\n log(`Found ${files.length} files for pattern: ${pagesPattern}`);\n\n await fs.mkdir(path.resolve(rootDir, outputPath), { recursive: true });\n\n try {\n for (const file of files) {\n log(`parsing file: : ${file}`);\n\n const componentName = path.basename(file, path.extname(file));\n const filePath = path.join(rootDir, file);\n\n const docs = docgenParser.parse(filePath);\n const props = docs[0] ? docs[0].props : {};\n\n for (const propName in props) {\n const prop = props[propName] as\n | (PropItem & {\n shortPropTypeName: string | null;\n })\n | undefined;\n\n if (!prop) {\n throw new Error(`No prop found for ${propName} in ${componentName}`);\n }\n\n if (prop.type && prop.type.name) {\n const { type: shortPropTypeName, detailedType } = getShortPropType(\n propName,\n prop.type.name\n );\n\n const hasExpandedType = Boolean(detailedType);\n\n prop.type.name =\n hasExpandedType && prop.type.name.split(\"|\").length > 3\n ? prop.type.name\n .split(\"|\")\n .map((line) => `| ${line}\\n`)\n .join(\"\")\n : prop.type.name;\n\n prop.shortPropTypeName = hasExpandedType ? shortPropTypeName : null;\n }\n }\n\n const componentJSON = JSON.stringify(\n {\n name: componentName,\n path: file,\n fileName: file.split(\"/\").pop(),\n props,\n },\n null,\n 2\n );\n\n const outputFilePath = path.join(outputPath, `${componentName}.json`);\n await fs.writeFile(outputFilePath, componentJSON + \"\\n\", \"utf-8\");\n\n log(`Generated props for ${componentName}`);\n }\n } catch (err) {\n log(`Error generating props: ${(err as Error).message}`);\n }\n}\n\n// based on https://github.com/mui/base-ui/blob/master/docs/src/components/ReferenceTable/PropsReferenceAccordion.tsx\nfunction getShortPropType(name: string, type: string) {\n if (/^(on|get)[A-Z].*/.test(name)) {\n return { type: \"function\", detailedType: true };\n }\n\n if (type === undefined || type === null) {\n return { type: String(type), detailedType: false };\n }\n\n if (name === \"className\") {\n return { type: \"string | function\", detailedType: true };\n }\n\n if (name === \"render\") {\n return { type: \"ReactElement | function\", detailedType: true };\n }\n\n if (\n name.endsWith(\"Ref\") ||\n name === \"children\" ||\n type === \"boolean\" ||\n type === \"string\" ||\n type === \"number\" ||\n type.indexOf(\" | \") === -1 ||\n (type.split(\"|\").length < 3 && type.length < 30)\n ) {\n return { type, detailedType: false };\n }\n\n return { type: \"Union\", detailedType: true };\n}\n"],"mappings":";;;;;;AAMA,MAAM,aAAa,cAAc,OAAO,KAAK,IAAI;AAC/B,KAAK,QAAQ,WAAW;AAE1C,eAAsB,cACpB,cACA,SACA,YACA,MAAM,QAAQ,KACd;CAKA,MAAM,eAAe,iBAFA,KAAK,QAAQ,SAAS,kBAEO,EAAE;EAClD,oCAAoC;EACpC,uBAAuB;EACvB,aAAa,SAAS;GACpB,IAAI,MAAM,QAAQ,KAAK,aAAa,IAAI,KAAK,aAAa;QACpC,KAAK,aAAa,GAEtB,SAAS,SAAS,4BAA4B;SACxD,KAAK,SAAS,SAAS,KAAK,SAAS,aACvC,OAAO;;;GAIb,OAAO;;EAEV,CAAC;CAEF,MAAM,QAAQ,MAAM,KAAK,cAAc,EACrC,KAAK,SACN,CAAC;CAEF,IAAI,MAAM,WAAW,GAAG;EACtB,IAAI,+BAA+B,eAAe;EAClD;;CAGF,IAAI,SAAS,MAAM,OAAO,sBAAsB,eAAe;CAE/D,MAAM,GAAG,MAAM,KAAK,QAAQ,SAAS,WAAW,EAAE,EAAE,WAAW,MAAM,CAAC;CAEtE,IAAI;EACF,KAAK,MAAM,QAAQ,OAAO;GACxB,IAAI,mBAAmB,OAAO;GAE9B,MAAM,gBAAgB,KAAK,SAAS,MAAM,KAAK,QAAQ,KAAK,CAAC;GAC7D,MAAM,WAAW,KAAK,KAAK,SAAS,KAAK;GAEzC,MAAM,OAAO,aAAa,MAAM,SAAS;GACzC,MAAM,QAAQ,KAAK,KAAK,KAAK,GAAG,QAAQ,EAAE;GAE1C,KAAK,MAAM,YAAY,OAAO;IAC5B,MAAM,OAAO,MAAM;IAMnB,IAAI,CAAC,MACH,MAAM,IAAI,MAAM,qBAAqB,SAAS,MAAM,gBAAgB;IAGtE,IAAI,KAAK,QAAQ,KAAK,KAAK,MAAM;KAC/B,MAAM,EAAE,MAAM,mBAAmB,iBAAiB,iBAChD,UACA,KAAK,KAAK,KACX;KAED,MAAM,kBAAkB,QAAQ,aAAa;KAE7C,KAAK,KAAK,OACR,mBAAmB,KAAK,KAAK,KAAK,MAAM,IAAI,CAAC,SAAS,IAClD,KAAK,KAAK,KACP,MAAM,IAAI,CACV,KAAK,SAAS,KAAK,KAAK,IAAI,CAC5B,KAAK,GAAG,GACX,KAAK,KAAK;KAEhB,KAAK,oBAAoB,kBAAkB,oBAAoB;;;GAInE,MAAM,gBAAgB,KAAK,UACzB;IACE,MAAM;IACN,MAAM;IACN,UAAU,KAAK,MAAM,IAAI,CAAC,KAAK;IAC/B;IACD,EACD,MACA,EACD;GAED,MAAM,iBAAiB,KAAK,KAAK,YAAY,GAAG,cAAc,OAAO;GACrE,MAAM,GAAG,UAAU,gBAAgB,gBAAgB,MAAM,QAAQ;GAEjE,IAAI,uBAAuB,gBAAgB;;UAEtC,KAAK;EACZ,IAAI,2BAA4B,IAAc,UAAU;;;AAK5D,SAAS,iBAAiB,MAAc,MAAc;CACpD,IAAI,mBAAmB,KAAK,KAAK,EAC/B,OAAO;EAAE,MAAM;EAAY,cAAc;EAAM;CAGjD,IAAI,SAAS,KAAA,KAAa,SAAS,MACjC,OAAO;EAAE,MAAM,OAAO,KAAK;EAAE,cAAc;EAAO;CAGpD,IAAI,SAAS,aACX,OAAO;EAAE,MAAM;EAAqB,cAAc;EAAM;CAG1D,IAAI,SAAS,UACX,OAAO;EAAE,MAAM;EAA2B,cAAc;EAAM;CAGhE,IACE,KAAK,SAAS,MAAM,IACpB,SAAS,cACT,SAAS,aACT,SAAS,YACT,SAAS,YACT,KAAK,QAAQ,MAAM,KAAK,MACvB,KAAK,MAAM,IAAI,CAAC,SAAS,KAAK,KAAK,SAAS,IAE7C,OAAO;EAAE;EAAM,cAAc;EAAO;CAGtC,OAAO;EAAE,MAAM;EAAS,cAAc;EAAM"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/index.ts"],"mappings":";;;cAC0C,QAAA"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { generateProps } from "./generate-props.mjs";
|
|
2
|
+
import { name } from "./package.mjs";
|
|
3
|
+
import ThrowableDiagnostic from "@parcel/diagnostic";
|
|
4
|
+
import { Reporter } from "@parcel/plugin";
|
|
5
|
+
import path from "path";
|
|
6
|
+
import { pathToFileURL } from "url";
|
|
7
|
+
import dedent from "dedent";
|
|
8
|
+
//#region src/index.ts
|
|
9
|
+
const origin = name;
|
|
10
|
+
var src_default = new Reporter({ async report({ event, options, logger }) {
|
|
11
|
+
if (event.type === "buildStart") {
|
|
12
|
+
try {
|
|
13
|
+
var _config$origin, _config$origin2;
|
|
14
|
+
const packageJSON = path.join(options.projectRoot, "package.json");
|
|
15
|
+
const config = await import(pathToFileURL(packageJSON).toString(), { with: { type: "json" } }).then((mod) => mod.default);
|
|
16
|
+
const source = (_config$origin = config[origin]) === null || _config$origin === void 0 ? void 0 : _config$origin.source;
|
|
17
|
+
const output = (_config$origin2 = config[origin]) === null || _config$origin2 === void 0 ? void 0 : _config$origin2.output;
|
|
18
|
+
if (!source) throw new ThrowableDiagnostic.default({ diagnostic: {
|
|
19
|
+
origin,
|
|
20
|
+
message: dedent`No "@renr/parcel-reporter-docgen" source found in package.json.
|
|
21
|
+
NOTE: if you are using a monorepo, you need to run parcel from the package that contains your pages rather than the project root.
|
|
22
|
+
See https://github.com/parcel-bundler/parcel/issues/7206 for a workaround.`,
|
|
23
|
+
hints: ["Add a \"@renr/parcel-reporter-docgen\" source to your package.json with a source e.g `../packages/ui/src/**/*.{ts,tsx}`."],
|
|
24
|
+
codeFrames: [{
|
|
25
|
+
codeHighlights: [{
|
|
26
|
+
start: {
|
|
27
|
+
line: 3,
|
|
28
|
+
column: 2
|
|
29
|
+
},
|
|
30
|
+
end: {
|
|
31
|
+
line: 7,
|
|
32
|
+
column: 44
|
|
33
|
+
},
|
|
34
|
+
message: "Add a \"@renr/parcel-reporter-docgen\" source to generate docs for your UI components."
|
|
35
|
+
}],
|
|
36
|
+
language: "json",
|
|
37
|
+
filePath: packageJSON,
|
|
38
|
+
code: dedent`{
|
|
39
|
+
"@renr/parcel-reporter-docgen": {
|
|
40
|
+
"source": "../packages/ui/src/**/*.{ts,tsx}",
|
|
41
|
+
"output": "docs/components"
|
|
42
|
+
}
|
|
43
|
+
} `
|
|
44
|
+
}]
|
|
45
|
+
} });
|
|
46
|
+
try {
|
|
47
|
+
await generateProps(source, options.projectRoot, output || "docgen", (...args) => logger.info({
|
|
48
|
+
origin,
|
|
49
|
+
message: args.join(" ")
|
|
50
|
+
}));
|
|
51
|
+
} catch (error) {
|
|
52
|
+
throwError(error);
|
|
53
|
+
}
|
|
54
|
+
} catch (error) {
|
|
55
|
+
throwError(error);
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
} });
|
|
60
|
+
function throwError(error) {
|
|
61
|
+
throw new ThrowableDiagnostic.default({ diagnostic: {
|
|
62
|
+
origin,
|
|
63
|
+
message: error.message,
|
|
64
|
+
stack: error.stack
|
|
65
|
+
} });
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
export { src_default as default };
|
|
69
|
+
|
|
70
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["pacakgeJSON.name"],"sources":["../../src/index.ts"],"sourcesContent":["import ThrowableDiagnostic from \"@parcel/diagnostic\";\nimport { Reporter } from \"@parcel/plugin\";\nimport path from \"path\";\nimport { pathToFileURL } from \"url\";\nimport { generateProps } from \"./generate-props.js\";\nimport pacakgeJSON from \"../package.json\" with { type: \"json\" };\nimport dedent from \"dedent\";\n\nconst origin = pacakgeJSON.name;\n\nexport default new Reporter({\n async report({ event, options, logger }) {\n if (event.type === \"buildStart\") {\n try {\n const packageJSON = path.join(options.projectRoot, \"package.json\");\n\n const fileURL = pathToFileURL(packageJSON).toString();\n\n const config = (await import(fileURL, {\n with: {\n type: \"json\",\n },\n }).then((mod) => mod.default)) as {\n [origin]?: {\n source: string;\n output?: string;\n };\n };\n\n const source = config[origin]?.source;\n const output = config[origin]?.output;\n\n if (!source) {\n // @ts-expect-error compiled verison needs to use default\n throw new ThrowableDiagnostic.default({\n diagnostic: {\n origin,\n message: dedent`No \"@renr/parcel-reporter-docgen\" source found in package.json. \n NOTE: if you are using a monorepo, you need to run parcel from the package that contains your pages rather than the project root.\n See https://github.com/parcel-bundler/parcel/issues/7206 for a workaround.`,\n // TODO why isn't the hint/documentation showing up in the terminal/error page?\n hints: [\n 'Add a \"@renr/parcel-reporter-docgen\" source to your package.json with a source e.g `../packages/ui/src/**/*.{ts,tsx}`.',\n ],\n\n codeFrames: [\n {\n codeHighlights: [\n {\n start: { line: 3, column: 2 },\n end: { line: 7, column: 44 },\n message:\n 'Add a \"@renr/parcel-reporter-docgen\" source to generate docs for your UI components.',\n },\n ],\n language: \"json\",\n filePath: packageJSON,\n code: dedent`{\n \"@renr/parcel-reporter-docgen\": {\n \"source\": \"../packages/ui/src/**/*.{ts,tsx}\",\n \"output\": \"docs/components\"\n }\n } `,\n },\n ],\n },\n });\n }\n try {\n await generateProps(\n source,\n options.projectRoot,\n output || \"docgen\",\n (...args) => logger.info({ origin, message: args.join(\" \") })\n );\n } catch (error) {\n throwError(error as Error);\n }\n } catch (error) {\n throwError(error as Error);\n }\n return;\n }\n },\n});\n\nfunction throwError(error: Error) {\n // @ts-expect-error compiled verison needs to use default\n throw new ThrowableDiagnostic.default({\n diagnostic: {\n origin,\n message: error.message,\n stack: error.stack,\n },\n });\n}\n"],"mappings":";;;;;;;;AAQA,MAAM,SAASA;AAEf,IAAA,cAAe,IAAI,SAAS,EAC1B,MAAM,OAAO,EAAE,OAAO,SAAS,UAAU;CACvC,IAAI,MAAM,SAAS,cAAc;EAC/B,IAAI;;GACF,MAAM,cAAc,KAAK,KAAK,QAAQ,aAAa,eAAe;GAIlE,MAAM,SAAU,MAAM,OAFN,cAAc,YAAY,CAAC,UAEP,EAAE,EACpC,MAAM,EACJ,MAAM,QACP,EACF,EAAE,MAAM,QAAQ,IAAI,QAAQ;GAO7B,MAAM,UAAA,iBAAS,OAAO,aAAA,QAAA,mBAAA,KAAA,IAAA,KAAA,IAAA,eAAS;GAC/B,MAAM,UAAA,kBAAS,OAAO,aAAA,QAAA,oBAAA,KAAA,IAAA,KAAA,IAAA,gBAAS;GAE/B,IAAI,CAAC,QAEH,MAAM,IAAI,oBAAoB,QAAQ,EACpC,YAAY;IACV;IACA,SAAS,MAAM;;;IAIf,OAAO,CACL,2HACD;IAED,YAAY,CACV;KACE,gBAAgB,CACd;MACE,OAAO;OAAE,MAAM;OAAG,QAAQ;OAAG;MAC7B,KAAK;OAAE,MAAM;OAAG,QAAQ;OAAI;MAC5B,SACE;MACH,CACF;KACD,UAAU;KACV,UAAU;KACV,MAAM,MAAM;;;;;;KAMb,CACF;IACF,EACF,CAAC;GAEJ,IAAI;IACF,MAAM,cACJ,QACA,QAAQ,aACR,UAAU,WACT,GAAG,SAAS,OAAO,KAAK;KAAE;KAAQ,SAAS,KAAK,KAAK,IAAI;KAAE,CAAC,CAC9D;YACM,OAAO;IACd,WAAW,MAAe;;WAErB,OAAO;GACd,WAAW,MAAe;;EAE5B;;GAGL,CAAC;AAEF,SAAS,WAAW,OAAc;CAEhC,MAAM,IAAI,oBAAoB,QAAQ,EACpC,YAAY;EACV;EACA,SAAS,MAAM;EACf,OAAO,MAAM;EACd,EACF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package.mjs","names":[],"sources":["../../package.json"],"sourcesContent":[""],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@renr/parcel-reporter-docgen",
|
|
3
|
+
"version": "0.0.1-alpha.3",
|
|
4
|
+
"author": "Ren Rizzolo",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"description": "Parcel reporter that generates ui docs via react-docgen-typescript",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"parcel",
|
|
9
|
+
"rsc"
|
|
10
|
+
],
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/renrizzolo/parcel-rsc-react.git"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "src/index.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"exports": {
|
|
22
|
+
"default": {
|
|
23
|
+
"types": "./dist/esm/index.d.mts",
|
|
24
|
+
"default": "./dist/esm/index.mjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"react-docgen-typescript": "^2.4.0",
|
|
29
|
+
"@parcel/diagnostic": "^2.16.4",
|
|
30
|
+
"@parcel/plugin": "^2.16.4",
|
|
31
|
+
"dedent": "^1.7.2",
|
|
32
|
+
"tinyglobby": "^0.2.16",
|
|
33
|
+
"prettier": "^3.8.3"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^25.6.2"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsdown",
|
|
43
|
+
"type-check": "tsgo --noEmit"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import ThrowableDiagnostic from "@parcel/diagnostic";
|
|
2
|
+
import { Reporter } from "@parcel/plugin";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { pathToFileURL } from "url";
|
|
5
|
+
import { generateProps } from "./generate-props.js";
|
|
6
|
+
import pacakgeJSON from "../package.json" with { type: "json" };
|
|
7
|
+
import dedent from "dedent";
|
|
8
|
+
|
|
9
|
+
const origin = pacakgeJSON.name;
|
|
10
|
+
|
|
11
|
+
export default new Reporter({
|
|
12
|
+
async report({ event, options, logger }) {
|
|
13
|
+
if (event.type === "buildStart") {
|
|
14
|
+
try {
|
|
15
|
+
const packageJSON = path.join(options.projectRoot, "package.json");
|
|
16
|
+
|
|
17
|
+
const fileURL = pathToFileURL(packageJSON).toString();
|
|
18
|
+
|
|
19
|
+
const config = (await import(fileURL, {
|
|
20
|
+
with: {
|
|
21
|
+
type: "json",
|
|
22
|
+
},
|
|
23
|
+
}).then((mod) => mod.default)) as {
|
|
24
|
+
[origin]?: {
|
|
25
|
+
source: string;
|
|
26
|
+
output?: string;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const source = config[origin]?.source;
|
|
31
|
+
const output = config[origin]?.output;
|
|
32
|
+
|
|
33
|
+
if (!source) {
|
|
34
|
+
// @ts-expect-error compiled verison needs to use default
|
|
35
|
+
throw new ThrowableDiagnostic.default({
|
|
36
|
+
diagnostic: {
|
|
37
|
+
origin,
|
|
38
|
+
message: dedent`No "@renr/parcel-reporter-docgen" source found in package.json.
|
|
39
|
+
NOTE: if you are using a monorepo, you need to run parcel from the package that contains your pages rather than the project root.
|
|
40
|
+
See https://github.com/parcel-bundler/parcel/issues/7206 for a workaround.`,
|
|
41
|
+
// TODO why isn't the hint/documentation showing up in the terminal/error page?
|
|
42
|
+
hints: [
|
|
43
|
+
'Add a "@renr/parcel-reporter-docgen" source to your package.json with a source e.g `../packages/ui/src/**/*.{ts,tsx}`.',
|
|
44
|
+
],
|
|
45
|
+
|
|
46
|
+
codeFrames: [
|
|
47
|
+
{
|
|
48
|
+
codeHighlights: [
|
|
49
|
+
{
|
|
50
|
+
start: { line: 3, column: 2 },
|
|
51
|
+
end: { line: 7, column: 44 },
|
|
52
|
+
message:
|
|
53
|
+
'Add a "@renr/parcel-reporter-docgen" source to generate docs for your UI components.',
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
language: "json",
|
|
57
|
+
filePath: packageJSON,
|
|
58
|
+
code: dedent`{
|
|
59
|
+
"@renr/parcel-reporter-docgen": {
|
|
60
|
+
"source": "../packages/ui/src/**/*.{ts,tsx}",
|
|
61
|
+
"output": "docs/components"
|
|
62
|
+
}
|
|
63
|
+
} `,
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
await generateProps(
|
|
71
|
+
source,
|
|
72
|
+
options.projectRoot,
|
|
73
|
+
output || "docgen",
|
|
74
|
+
(...args) => logger.info({ origin, message: args.join(" ") })
|
|
75
|
+
);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
throwError(error as Error);
|
|
78
|
+
}
|
|
79
|
+
} catch (error) {
|
|
80
|
+
throwError(error as Error);
|
|
81
|
+
}
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
function throwError(error: Error) {
|
|
88
|
+
// @ts-expect-error compiled verison needs to use default
|
|
89
|
+
throw new ThrowableDiagnostic.default({
|
|
90
|
+
diagnostic: {
|
|
91
|
+
origin,
|
|
92
|
+
message: error.message,
|
|
93
|
+
stack: error.stack,
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
}
|