storybook-react-rsbuild 3.4.2 → 3.6.0

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.
@@ -0,0 +1,86 @@
1
+ import "node:module";
2
+ import magic_string from "magic-string";
3
+ import typescript from "typescript";
4
+ function createLiteral(value) {
5
+ switch(typeof value){
6
+ case 'string':
7
+ return typescript.factory.createStringLiteral(value);
8
+ case 'number':
9
+ return typescript.factory.createNumericLiteral(value);
10
+ case 'boolean':
11
+ return value ? typescript.factory.createTrue() : typescript.factory.createFalse();
12
+ }
13
+ }
14
+ function insertTsIgnoreBeforeStatement(statement) {
15
+ typescript.setSyntheticLeadingComments(statement, [
16
+ {
17
+ text: ' @ts-ignore',
18
+ kind: typescript.SyntaxKind.SingleLineCommentTrivia,
19
+ pos: -1,
20
+ end: -1
21
+ }
22
+ ]);
23
+ return statement;
24
+ }
25
+ function setDisplayName(d) {
26
+ return insertTsIgnoreBeforeStatement(typescript.factory.createExpressionStatement(typescript.factory.createBinaryExpression(typescript.factory.createPropertyAccessExpression(typescript.factory.createIdentifier(d.displayName), typescript.factory.createIdentifier('displayName')), typescript.SyntaxKind.EqualsToken, typescript.factory.createStringLiteral(d.displayName))));
27
+ }
28
+ function createPropDefinition(propName, prop, options) {
29
+ const setDefaultValue = (defaultValue)=>typescript.factory.createPropertyAssignment(typescript.factory.createStringLiteral('defaultValue'), defaultValue?.value !== void 0 && ('string' == typeof defaultValue.value || 'number' == typeof defaultValue.value || 'boolean' == typeof defaultValue.value) ? typescript.factory.createObjectLiteralExpression([
30
+ typescript.factory.createPropertyAssignment(typescript.factory.createIdentifier('value'), createLiteral(defaultValue.value))
31
+ ]) : typescript.factory.createNull());
32
+ const setStringLiteralField = (fieldName, fieldValue)=>typescript.factory.createPropertyAssignment(typescript.factory.createStringLiteral(fieldName), typescript.factory.createStringLiteral(fieldValue));
33
+ const setDescription = (description)=>setStringLiteralField("description", description);
34
+ const setName = (name)=>setStringLiteralField('name', name);
35
+ const setRequired = (required)=>typescript.factory.createPropertyAssignment(typescript.factory.createStringLiteral('required'), required ? typescript.factory.createTrue() : typescript.factory.createFalse());
36
+ const setValue = (typeValue)=>Array.isArray(typeValue) && typeValue.every((value)=>'string' == typeof value.value) ? typescript.factory.createPropertyAssignment(typescript.factory.createStringLiteral('value'), typescript.factory.createArrayLiteralExpression(typeValue.map((value)=>typescript.factory.createObjectLiteralExpression([
37
+ setStringLiteralField('value', value.value)
38
+ ])))) : void 0;
39
+ const setType = (typeName, typeValue)=>{
40
+ const objectFields = [
41
+ setStringLiteralField('name', typeName)
42
+ ];
43
+ const valueField = setValue(typeValue);
44
+ if (valueField) objectFields.push(valueField);
45
+ return typescript.factory.createPropertyAssignment(typescript.factory.createStringLiteral(options.typePropName), typescript.factory.createObjectLiteralExpression(objectFields));
46
+ };
47
+ return typescript.factory.createPropertyAssignment(typescript.factory.createStringLiteral(propName), typescript.factory.createObjectLiteralExpression([
48
+ setDefaultValue(prop.defaultValue),
49
+ setDescription(prop.description),
50
+ setName(prop.name),
51
+ setRequired(prop.required),
52
+ setType(prop.type.name, prop.type.value)
53
+ ]));
54
+ }
55
+ function setComponentDocGen(d, options) {
56
+ return insertTsIgnoreBeforeStatement(typescript.factory.createExpressionStatement(typescript.factory.createBinaryExpression(typescript.factory.createPropertyAccessExpression(typescript.factory.createIdentifier(d.displayName), typescript.factory.createIdentifier('__docgenInfo')), typescript.SyntaxKind.EqualsToken, typescript.factory.createObjectLiteralExpression([
57
+ typescript.factory.createPropertyAssignment(typescript.factory.createStringLiteral("description"), typescript.factory.createStringLiteral(d.description)),
58
+ typescript.factory.createPropertyAssignment(typescript.factory.createStringLiteral('displayName'), typescript.factory.createStringLiteral(d.displayName)),
59
+ typescript.factory.createPropertyAssignment(typescript.factory.createStringLiteral('props'), typescript.factory.createObjectLiteralExpression(Object.entries(d.props).map(([propName, prop])=>createPropDefinition(propName, prop, options))))
60
+ ]))));
61
+ }
62
+ function generateDocgenCodeBlock(options) {
63
+ const sourceFile = typescript.createSourceFile(options.filename, options.source, typescript.ScriptTarget.ESNext);
64
+ const wrapInTryStatement = (statements)=>typescript.factory.createTryStatement(typescript.factory.createBlock(statements, true), typescript.factory.createCatchClause(typescript.factory.createVariableDeclaration(typescript.factory.createIdentifier("__react_docgen_typescript_loader_error")), typescript.factory.createBlock([])), void 0);
65
+ const codeBlocks = options.componentDocs.map((d)=>wrapInTryStatement([
66
+ options.setDisplayName ? setDisplayName(d) : null,
67
+ setComponentDocGen(d, options)
68
+ ].filter((s)=>null !== s)));
69
+ const printer = typescript.createPrinter({
70
+ newLine: typescript.NewLineKind.LineFeed
71
+ });
72
+ const printNode = (sourceNode)=>printer.printNode(typescript.EmitHint.Unspecified, sourceNode, sourceFile);
73
+ const s = new magic_string(options.source, {
74
+ filename: options.filename
75
+ });
76
+ for (const node of codeBlocks)s.append(printNode(node));
77
+ return {
78
+ code: s.toString(),
79
+ map: s.generateMap({
80
+ source: options.filename,
81
+ includeContent: true,
82
+ hires: true
83
+ }).toString()
84
+ };
85
+ }
86
+ export { generateDocgenCodeBlock };
@@ -0,0 +1,9 @@
1
+ import "node:module";
2
+ function getGenerateOptions(options) {
3
+ const { setDisplayName = true, typePropName = 'type' } = options;
4
+ return {
5
+ setDisplayName,
6
+ typePropName
7
+ };
8
+ }
9
+ export { getGenerateOptions };
@@ -0,0 +1,99 @@
1
+ import "node:module";
2
+ import { createFilter } from "@rollup/pluginutils";
3
+ const defaultPropFilter = (prop)=>!prop.parent?.fileName.includes('node_modules');
4
+ const getDocgen = async (config)=>{
5
+ const docGen = await import("react-docgen-typescript");
6
+ const { tsconfigPath, compilerOptions, propFilter = defaultPropFilter, setDisplayName, typePropName, ...rest } = config;
7
+ const docgenOptions = {
8
+ propFilter,
9
+ ...rest
10
+ };
11
+ return docGen.withCompilerOptions({}, docgenOptions);
12
+ };
13
+ const startWatch = async (config, onProgramCreatedOrUpdated)=>{
14
+ const { default: ts } = await import("typescript");
15
+ const { getTSConfigFile } = await import("./typescript-69cd7fa9.js");
16
+ let compilerOptions = {
17
+ jsx: ts.JsxEmit.React,
18
+ module: ts.ModuleKind.CommonJS,
19
+ target: ts.ScriptTarget.Latest
20
+ };
21
+ const tsconfigPath = config.tsconfigPath ?? './tsconfig.json';
22
+ if (config.compilerOptions) compilerOptions = {
23
+ ...compilerOptions,
24
+ ...config.compilerOptions
25
+ };
26
+ else {
27
+ const { options: tsOptions } = getTSConfigFile(tsconfigPath);
28
+ compilerOptions = {
29
+ ...compilerOptions,
30
+ ...tsOptions
31
+ };
32
+ }
33
+ const host = ts.createWatchCompilerHost(tsconfigPath, compilerOptions, ts.sys, ts.createSemanticDiagnosticsBuilderProgram, void 0, ()=>{});
34
+ host.afterProgramCreate = (program)=>{
35
+ onProgramCreatedOrUpdated(program.getProgram());
36
+ };
37
+ return new Promise((resolve)=>{
38
+ const watch = ts.createWatchProgram(host);
39
+ resolve([
40
+ watch.getProgram().getProgram(),
41
+ ()=>watch.close()
42
+ ]);
43
+ });
44
+ };
45
+ const react_docgen_typescript = (config = {})=>{
46
+ let tsProgram;
47
+ let docGenParser;
48
+ let generateDocgenCodeBlock;
49
+ let generateOptions;
50
+ let filter;
51
+ const moduleInvalidationQueue = new Map();
52
+ let closeWatch;
53
+ return {
54
+ name: "rsbuild-plugin-react-docgen-typescript",
55
+ setup (api) {
56
+ api.modifyRsbuildConfig(async ()=>{
57
+ const { getGenerateOptions } = await import("./options-7e849a08.js");
58
+ generateDocgenCodeBlock = (await import("./generate-1782ed11.js")).generateDocgenCodeBlock;
59
+ docGenParser = await getDocgen(config);
60
+ generateOptions = getGenerateOptions(config);
61
+ [tsProgram, closeWatch] = await startWatch(config, (program)=>{
62
+ tsProgram = program;
63
+ for (const [filepath, invalidateModule] of moduleInvalidationQueue.entries()){
64
+ invalidateModule();
65
+ moduleInvalidationQueue.delete(filepath);
66
+ }
67
+ });
68
+ filter = createFilter(config.include ?? [
69
+ '**/**.tsx'
70
+ ], config.exclude ?? [
71
+ '**/**.stories.tsx'
72
+ ]);
73
+ });
74
+ api.transform({
75
+ test: (id)=>filter(id)
76
+ }, async ({ code: src, resource: id })=>{
77
+ try {
78
+ const componentDocs = docGenParser.parseWithProgramProvider(id, ()=>tsProgram);
79
+ if (!componentDocs.length) return {
80
+ code: src
81
+ };
82
+ const res = generateDocgenCodeBlock({
83
+ filename: id,
84
+ source: src,
85
+ componentDocs,
86
+ ...generateOptions
87
+ });
88
+ return res;
89
+ } catch (_e) {
90
+ return src;
91
+ }
92
+ });
93
+ api.onCloseBuild(()=>{
94
+ closeWatch();
95
+ });
96
+ }
97
+ };
98
+ };
99
+ export default react_docgen_typescript;
@@ -0,0 +1,13 @@
1
+ import "node:module";
2
+ import { dirname } from "node:path";
3
+ import typescript from "typescript";
4
+ function getTSConfigFile(tsconfigPath) {
5
+ try {
6
+ const basePath = dirname(tsconfigPath);
7
+ const configFile = typescript.readConfigFile(tsconfigPath, (p)=>typescript.sys.readFile(p));
8
+ return typescript.parseJsonConfigFileContent(configFile.config, typescript.sys, basePath, {}, tsconfigPath);
9
+ } catch (_error) {
10
+ return {};
11
+ }
12
+ }
13
+ export { getTSConfigFile };
package/dist/index.d.ts CHANGED
@@ -1,54 +1,75 @@
1
- export * from '@storybook/react';
2
- export { __definePreview as definePreview } from '@storybook/react';
3
- import { PluginOptions } from '@storybook/react-docgen-typescript-plugin';
4
- import { StorybookConfig as StorybookConfig$1, CompatibleString, TypescriptOptions } from 'storybook/internal/types';
5
- import { BuilderOptions, StorybookConfigRsbuild, TypescriptOptions as TypescriptOptions$1 } from 'storybook-builder-rsbuild';
6
-
7
- type FrameworkName = CompatibleString<'storybook-react-rsbuild'>;
8
- type BuilderName = CompatibleString<'storybook-builder-rsbuild'>;
9
- type FrameworkOptions = {
10
- builder?: BuilderOptions;
11
- strictMode?: boolean;
12
- /**
13
- * Use React's legacy root API to mount components
14
- * @description
15
- * React has introduced a new root API with React 18.x to enable a whole set of new features (e.g. concurrent features)
16
- * If this flag is true, the legacy Root API is used to mount components to make it easier to migrate step by step to React 18.
17
- * @default false
18
- */
19
- legacyRootApi?: boolean;
20
- };
21
- type TypescriptOptionsReact = {
22
- /**
23
- * Sets the type of Docgen when working with React and TypeScript
24
- *
25
- * @default `'react-docgen'`
26
- */
27
- reactDocgen: 'react-docgen-typescript' | 'react-docgen' | false;
28
- /**
29
- * Configures `react-docgen-typescript-plugin`
30
- *
31
- * @default
32
- * @see https://github.com/storybookjs/storybook/blob/next/code/builders/builder-webpack5/src/config/defaults.js#L4-L6
33
- */
34
- reactDocgenTypescriptOptions: PluginOptions;
35
- };
36
- type StorybookConfigFramework = {
37
- framework: FrameworkName | {
38
- name: FrameworkName;
39
- options: FrameworkOptions;
40
- };
41
- core?: StorybookConfig$1['core'] & {
42
- builder?: BuilderName | {
43
- name: BuilderName;
44
- options: BuilderOptions;
45
- };
46
- };
47
- typescript?: Partial<TypescriptOptions & TypescriptOptions$1 & TypescriptOptionsReact>;
48
- };
49
- /**
50
- * The interface for Storybook configuration in `main.ts` files.
51
- */
52
- type StorybookConfig = Omit<StorybookConfig$1, keyof StorybookConfigRsbuild | keyof StorybookConfigFramework> & StorybookConfigRsbuild & StorybookConfigFramework;
53
-
54
- export type { FrameworkOptions, StorybookConfig };
1
+ import type { BuilderOptions } from 'storybook-builder-rsbuild';
2
+ import type { CompatibleString } from 'storybook/internal/types';
3
+ import { __definePreview as definePreview } from '@storybook/react';
4
+ import type { PluginOptions } from '@storybook/react-docgen-typescript-plugin';
5
+ import type { StorybookConfig as StorybookConfig_2 } from 'storybook/internal/types';
6
+ import type { StorybookConfigRsbuild } from 'storybook-builder-rsbuild';
7
+ import type { TypescriptOptions } from 'storybook/internal/types';
8
+ import type { TypescriptOptions as TypescriptOptions_2 } from 'storybook-builder-rsbuild';
9
+
10
+ declare type BuilderName = CompatibleString<'storybook-builder-rsbuild'>;
11
+
12
+ export { definePreview }
13
+
14
+ declare type FrameworkName = CompatibleString<'storybook-react-rsbuild'>;
15
+
16
+ export declare type FrameworkOptions = {
17
+ builder?: BuilderOptions;
18
+ strictMode?: boolean;
19
+ /**
20
+ * Use React's legacy root API to mount components
21
+ * @description
22
+ * React has introduced a new root API with React 18.x to enable a whole set of new features (e.g. concurrent features)
23
+ * If this flag is true, the legacy Root API is used to mount components to make it easier to migrate step by step to React 18.
24
+ * @default false
25
+ */
26
+ legacyRootApi?: boolean;
27
+ };
28
+
29
+ /**
30
+ * The interface for Storybook configuration in `main.ts` files.
31
+ */
32
+ export declare type StorybookConfig = Omit<StorybookConfig_2, keyof StorybookConfigRsbuild | keyof StorybookConfigFramework> & StorybookConfigRsbuild & StorybookConfigFramework;
33
+
34
+ declare type StorybookConfigFramework = {
35
+ framework: FrameworkName | {
36
+ name: FrameworkName;
37
+ options: FrameworkOptions;
38
+ };
39
+ core?: StorybookConfig_2['core'] & {
40
+ builder?: BuilderName | {
41
+ name: BuilderName;
42
+ options: BuilderOptions;
43
+ };
44
+ };
45
+ features?: StorybookConfig_2['features'] & {
46
+ /**
47
+ * Enable the experimental `.test` function in CSF Next
48
+ *
49
+ * @see https://storybook.js.org/docs/api/main-config/main-config-features#experimentaltestsyntax
50
+ */
51
+ experimentalTestSyntax?: boolean;
52
+ };
53
+ typescript?: Partial<TypescriptOptions & TypescriptOptions_2 & TypescriptOptionsReact>;
54
+ };
55
+
56
+ declare type TypescriptOptionsReact = {
57
+ /**
58
+ * Sets the type of Docgen when working with React and TypeScript
59
+ *
60
+ * @default `'react-docgen'`
61
+ */
62
+ reactDocgen: 'react-docgen-typescript' | 'react-docgen' | false;
63
+ /**
64
+ * Configures `react-docgen-typescript-plugin`
65
+ *
66
+ * @default
67
+ * @see https://github.com/storybookjs/storybook/blob/next/code/builders/builder-webpack5/src/config/defaults.js#L4-L6
68
+ */
69
+ reactDocgenTypescriptOptions: PluginOptions;
70
+ };
71
+
72
+
73
+ export * from "@storybook/react";
74
+
75
+ export { }
package/dist/index.js CHANGED
@@ -1,6 +1,2 @@
1
- // src/index.ts
2
1
  export * from "@storybook/react";
3
- import { __definePreview } from "@storybook/react";
4
- export {
5
- __definePreview as definePreview
6
- };
2
+ export { __definePreview as definePreview } from "@storybook/react";
@@ -1,10 +1,12 @@
1
- import * as react_docgen from 'react-docgen';
2
- import * as TsconfigPaths from 'tsconfig-paths';
3
- import { LoaderContext } from 'webpack';
4
-
5
- declare function reactDocgenLoader(this: LoaderContext<{
6
- debug: boolean;
7
- }>, source: string, map: any): Promise<void>;
8
- declare function getReactDocgenImporter(matchingPath: TsconfigPaths.MatchPath | undefined): react_docgen.Importer;
9
-
10
- export { reactDocgenLoader as default, getReactDocgenImporter };
1
+ import { Importer } from 'react-docgen';
2
+ import type { LoaderContext } from 'webpack';
3
+ import * as TsconfigPaths from 'tsconfig-paths';
4
+
5
+ export declare function getReactDocgenImporter(matchingPath: TsconfigPaths.MatchPath | undefined): Importer;
6
+
7
+ declare function reactDocgenLoader(this: LoaderContext<{
8
+ debug: boolean;
9
+ }>, source: string, map: any): Promise<void>;
10
+ export default reactDocgenLoader;
11
+
12
+ export { }