@cfp-studio/plugin-react 17.1.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.
- package/LICENSE +21 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.mjs +141 -0
- package/package.json +35 -0
- package/preview/error-boundary.tsx +49 -0
- package/preview/index.tsx +95 -0
- package/preview/render-16.tsx +6 -0
- package/preview/render-18.tsx +11 -0
- package/preview/root.ts +2 -0
- package/preview/tsconfig.json +9 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Zenc Labs Pty Ltd
|
|
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.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FrameworkPluginFactory } from '@cfp-studio/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 引入 @cfp-studio/core 包中的 FrameworkPluginFactory 类型,用于定义框架插件工厂
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 定义一个名为 reactFrameworkPlugin 的框架插件工厂对象
|
|
9
|
+
*/
|
|
10
|
+
declare const reactFrameworkPlugin: FrameworkPluginFactory;
|
|
11
|
+
|
|
12
|
+
export { reactFrameworkPlugin as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FrameworkPluginFactory } from '@cfp-studio/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 引入 @cfp-studio/core 包中的 FrameworkPluginFactory 类型,用于定义框架插件工厂
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 定义一个名为 reactFrameworkPlugin 的框架插件工厂对象
|
|
9
|
+
*/
|
|
10
|
+
declare const reactFrameworkPlugin: FrameworkPluginFactory;
|
|
11
|
+
|
|
12
|
+
export { reactFrameworkPlugin as default };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { createAnalyzer } from '@cfp-studio/analyzer-react';
|
|
2
|
+
import react from '@vitejs/plugin-react';
|
|
3
|
+
import { polyfillNode } from 'esbuild-plugin-polyfill-node';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import url from 'node:url';
|
|
6
|
+
|
|
7
|
+
const reactImportRegExp = /import (\* as )?React[ ,]/;
|
|
8
|
+
function reactImportsPlugin() {
|
|
9
|
+
return {
|
|
10
|
+
/**
|
|
11
|
+
* 插件名称,用于在 Vite 构建过程中标识此插件。
|
|
12
|
+
*/
|
|
13
|
+
name: "previewjs:react-js-imports",
|
|
14
|
+
/**
|
|
15
|
+
* 转换钩子,在 Vite 处理模块时调用。
|
|
16
|
+
*
|
|
17
|
+
* @param code - 模块的原始代码。
|
|
18
|
+
* @param id - 模块的唯一标识符,通常是文件路径。
|
|
19
|
+
* @returns 返回转换后的代码,如果不需要转换则返回原始代码。
|
|
20
|
+
*/
|
|
21
|
+
async transform(code, id) {
|
|
22
|
+
if (id.includes("node_modules") || path.extname(id) !== ".js" || reactImportRegExp.test(code)) {
|
|
23
|
+
return code;
|
|
24
|
+
}
|
|
25
|
+
return `import React from 'react';${code}`;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const reactFrameworkPlugin = {
|
|
31
|
+
/**
|
|
32
|
+
* 插件的基本信息
|
|
33
|
+
*/
|
|
34
|
+
info: {
|
|
35
|
+
/**
|
|
36
|
+
* 插件的 API 版本
|
|
37
|
+
*/
|
|
38
|
+
apiVersion: 5,
|
|
39
|
+
/**
|
|
40
|
+
* 插件的名称
|
|
41
|
+
*/
|
|
42
|
+
name: "@cfp-studio/plugin-react"
|
|
43
|
+
},
|
|
44
|
+
/**
|
|
45
|
+
* 检查当前项目是否与该插件兼容
|
|
46
|
+
* @param dependencies - 项目依赖项对象
|
|
47
|
+
* @returns 如果兼容返回 true,否则返回 false
|
|
48
|
+
*/
|
|
49
|
+
isCompatible: async (dependencies) => {
|
|
50
|
+
const version = await dependencies.react?.readInstalledVersion();
|
|
51
|
+
if (!version) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
const [major, minor] = version.split(".").map((n) => Number.parseInt(n));
|
|
55
|
+
if (Number.isNaN(major) || Number.isNaN(minor)) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
return major >= 17 || major === 16 && minor >= 14;
|
|
59
|
+
},
|
|
60
|
+
/**
|
|
61
|
+
* 创建插件实例
|
|
62
|
+
* @param param0 - 包含根目录、读取器、日志记录器和依赖项的对象
|
|
63
|
+
* @returns 插件实例对象
|
|
64
|
+
*/
|
|
65
|
+
async create({ rootDir, reader, logger, dependencies }) {
|
|
66
|
+
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
|
|
67
|
+
const analyzerPlugin = createAnalyzer({
|
|
68
|
+
rootDir,
|
|
69
|
+
reader,
|
|
70
|
+
logger
|
|
71
|
+
});
|
|
72
|
+
const previewDirPath = path.join(__dirname, "..", "preview");
|
|
73
|
+
return {
|
|
74
|
+
// 默认的包装器文件路径
|
|
75
|
+
defaultWrapperPath: "__previewjs__/Wrapper.tsx",
|
|
76
|
+
// 预览目录的路径
|
|
77
|
+
previewDirPath,
|
|
78
|
+
// 展开分析器插件实例的属性
|
|
79
|
+
...analyzerPlugin,
|
|
80
|
+
/**
|
|
81
|
+
* 配置 Vite 的配置选项
|
|
82
|
+
* @param configuredPlugins - 已配置的 Vite 插件数组
|
|
83
|
+
* @returns Vite 配置对象
|
|
84
|
+
*/
|
|
85
|
+
viteConfig: (configuredPlugins) => {
|
|
86
|
+
const hasReactPlugin = configuredPlugins.find(
|
|
87
|
+
(plugin) => plugin.name.startsWith("vite:react-")
|
|
88
|
+
);
|
|
89
|
+
return {
|
|
90
|
+
// 优化依赖项的配置
|
|
91
|
+
optimizeDeps: {
|
|
92
|
+
esbuildOptions: {
|
|
93
|
+
// 如果项目依赖中包含 next,则添加 polyfillNode 插件
|
|
94
|
+
plugins: dependencies.next ? [polyfillNode()] : []
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
// 路径解析配置
|
|
98
|
+
resolve: {
|
|
99
|
+
alias: {
|
|
100
|
+
// 将 react-native 别名映射到 react-native-web
|
|
101
|
+
"react-native": "react-native-web"
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
// Vite 插件数组
|
|
105
|
+
plugins: [
|
|
106
|
+
// 添加 reactImportsPlugin 插件
|
|
107
|
+
reactImportsPlugin(),
|
|
108
|
+
// 展开已配置的插件
|
|
109
|
+
...configuredPlugins,
|
|
110
|
+
// 如果已配置的插件中不包含 Vite 的 React 插件,则添加 react 插件
|
|
111
|
+
...!hasReactPlugin ? [react()] : [],
|
|
112
|
+
{
|
|
113
|
+
// 插件名称
|
|
114
|
+
name: "previewjs:update-react-import",
|
|
115
|
+
/**
|
|
116
|
+
* 转换代码的钩子函数
|
|
117
|
+
* @param code - 要转换的代码
|
|
118
|
+
* @param id - 文件的 ID
|
|
119
|
+
* @returns 转换后的代码,如果不需要转换则返回 undefined
|
|
120
|
+
*/
|
|
121
|
+
async transform(code, id) {
|
|
122
|
+
if (!id.endsWith("__previewjs_internal__/renderer/index.tsx")) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const reactVersion = Number.parseInt(
|
|
126
|
+
await dependencies.react?.readInstalledVersion() || "0"
|
|
127
|
+
);
|
|
128
|
+
return code.replace(
|
|
129
|
+
/__PREVIEWJS_PLUGIN_REACT_IMPORT_PATH__/g,
|
|
130
|
+
reactVersion >= 18 ? "./render-18" : "./render-16"
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
]
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export { reactFrameworkPlugin as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cfp-studio/plugin-react",
|
|
3
|
+
"version": "17.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"preview"
|
|
12
|
+
],
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@cfp-studio/analyzer-react": "^0.1.11",
|
|
15
|
+
"@vitejs/plugin-react": "4.3.4",
|
|
16
|
+
"esbuild-plugin-polyfill-node": "0.3.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@playwright/test": "1.50.1",
|
|
20
|
+
"@types/react": "18.3.18",
|
|
21
|
+
"@types/react-dom": "18.3.5",
|
|
22
|
+
"pino": "9.5.0",
|
|
23
|
+
"pino-pretty": "11.2.1",
|
|
24
|
+
"unbuild": "2.0.0",
|
|
25
|
+
"vite": "6.2.0",
|
|
26
|
+
"@cfp-studio/testing": "14.0.1",
|
|
27
|
+
"@cfp-studio/iframe": "18.0.0",
|
|
28
|
+
"@cfp-studio/core": "1.0.0",
|
|
29
|
+
"@cfp-studio/api": "16.1.0"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc && tsc -p preview/tsconfig.json && unbuild",
|
|
33
|
+
"e2e-test": "playwright test"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { Component } from "react";
|
|
3
|
+
|
|
4
|
+
type ErrorBoundaryProps = {
|
|
5
|
+
renderId: number;
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
let errorBoundaryInstance: ErrorBoundary | null = null;
|
|
10
|
+
|
|
11
|
+
export async function expectErrorBoundary(
|
|
12
|
+
renderId: number,
|
|
13
|
+
shouldAbortRender: () => boolean
|
|
14
|
+
) {
|
|
15
|
+
while (
|
|
16
|
+
!errorBoundaryInstance ||
|
|
17
|
+
errorBoundaryInstance.props.renderId !== renderId
|
|
18
|
+
) {
|
|
19
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
20
|
+
if (shouldAbortRender()) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return errorBoundaryInstance;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class ErrorBoundary extends Component<ErrorBoundaryProps> {
|
|
28
|
+
state = {
|
|
29
|
+
error: null as Error | null,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
constructor(props: ErrorBoundaryProps) {
|
|
33
|
+
super(props);
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
35
|
+
errorBoundaryInstance = this;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static getDerivedStateFromError(error: Error) {
|
|
39
|
+
return { error };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
render() {
|
|
43
|
+
if (this.state.error) {
|
|
44
|
+
return <></>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return this.props.children;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { GetPropsFn, RendererLoader } from "@cfp-studio/iframe";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { ErrorBoundary, expectErrorBoundary } from "./error-boundary";
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
import { render } from "__PREVIEWJS_PLUGIN_REACT_IMPORT_PATH__";
|
|
6
|
+
import { rootContainer } from "./root";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 加载渲染器的函数,用于渲染可预览的组件或故事。
|
|
10
|
+
*
|
|
11
|
+
* @param wrapperModule - 包装器模块,包含可能的包装组件。
|
|
12
|
+
* @param wrapperName - 包装器组件的名称,默认为 "Wrapper"。
|
|
13
|
+
* @param previewableModule - 包含可预览组件或故事的模块。
|
|
14
|
+
* @param previewableName - 可预览组件或故事的名称。
|
|
15
|
+
* @param renderId - 渲染的唯一标识符。
|
|
16
|
+
* @param shouldAbortRender - 一个函数,用于检查是否应中止渲染。
|
|
17
|
+
* @returns 一个对象,包含渲染函数和 JSX 工厂函数。
|
|
18
|
+
*/
|
|
19
|
+
export const loadRenderer: RendererLoader = async ({
|
|
20
|
+
wrapperModule,
|
|
21
|
+
wrapperName,
|
|
22
|
+
previewableModule,
|
|
23
|
+
previewableName,
|
|
24
|
+
renderId,
|
|
25
|
+
shouldAbortRender,
|
|
26
|
+
}) => {
|
|
27
|
+
const isStoryModule = !!previewableModule.default?.component;
|
|
28
|
+
const Wrapper = wrapperModule?.[wrapperName || "Wrapper"] || React.Fragment;
|
|
29
|
+
const Previewable =
|
|
30
|
+
previewableModule[previewableName] ||
|
|
31
|
+
previewableModule.PreviewJsEval(
|
|
32
|
+
`try { return ${previewableName}; } catch { return null; }`
|
|
33
|
+
);
|
|
34
|
+
if (!Previewable) {
|
|
35
|
+
throw new Error(`No component or story named '${previewableName}'`);
|
|
36
|
+
}
|
|
37
|
+
const decorators = [
|
|
38
|
+
...(Previewable.decorators || []),
|
|
39
|
+
...(previewableModule.default?.decorators || []),
|
|
40
|
+
];
|
|
41
|
+
const RenderComponent = isStoryModule
|
|
42
|
+
? typeof Previewable === "function"
|
|
43
|
+
? Previewable
|
|
44
|
+
: Previewable.render ||
|
|
45
|
+
Previewable.component ||
|
|
46
|
+
previewableModule.default?.render ||
|
|
47
|
+
previewableModule.default?.component ||
|
|
48
|
+
Previewable
|
|
49
|
+
: Previewable;
|
|
50
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
51
|
+
const Renderer = (props: any) => {
|
|
52
|
+
return (
|
|
53
|
+
<ErrorBoundary renderId={renderId}>
|
|
54
|
+
<Wrapper>
|
|
55
|
+
{decorators.reduce(
|
|
56
|
+
(component, decorator) => () => decorator(component),
|
|
57
|
+
() => <RenderComponent {...props} />
|
|
58
|
+
)()}
|
|
59
|
+
</Wrapper>
|
|
60
|
+
</ErrorBoundary>
|
|
61
|
+
);
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
render: async (getProps: GetPropsFn) => {
|
|
65
|
+
if (shouldAbortRender()) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
await render(
|
|
69
|
+
<Renderer
|
|
70
|
+
{...getProps({
|
|
71
|
+
presetGlobalProps: previewableModule.default?.args || {},
|
|
72
|
+
presetProps: Previewable.args || {},
|
|
73
|
+
})}
|
|
74
|
+
/>
|
|
75
|
+
);
|
|
76
|
+
if (shouldAbortRender()) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const errorBoundary = await expectErrorBoundary(
|
|
80
|
+
renderId,
|
|
81
|
+
shouldAbortRender
|
|
82
|
+
);
|
|
83
|
+
if (!errorBoundary) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (errorBoundary.state.error) {
|
|
87
|
+
throw errorBoundary.state.error;
|
|
88
|
+
}
|
|
89
|
+
if (Previewable.play) {
|
|
90
|
+
await Previewable.play({ canvasElement: rootContainer });
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
jsxFactory: React.createElement,
|
|
94
|
+
};
|
|
95
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Root } from "react-dom/client";
|
|
2
|
+
import { createRoot } from "react-dom/client";
|
|
3
|
+
import { rootContainer } from "./root";
|
|
4
|
+
|
|
5
|
+
let root: Root | null = null;
|
|
6
|
+
export function render(node: JSX.Element) {
|
|
7
|
+
if (!root) {
|
|
8
|
+
root = createRoot(rootContainer);
|
|
9
|
+
}
|
|
10
|
+
root.render(node);
|
|
11
|
+
}
|
package/preview/root.ts
ADDED