@kne/modules-dev 2.1.17 → 2.1.18

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.
Files changed (2) hide show
  1. package/README.md +373 -26
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -16,19 +16,28 @@ npm i --save @kne/modules-dev
16
16
 
17
17
  ### 概述
18
18
 
19
- modules-dev为所有的项目提供了一个开发环境,让你在开发远程组件或者前端库时有一个运行时的示例展示,并且当对你的项目进行发布的时候可以集成到kne-union文档之中
19
+ modules-dev 是一个强大的组件开发辅助工具,为你的远程组件或前端库项目提供完整的开发环境。无论是实时预览组件示例,还是生成可集成到文档系统的代码,modules-dev 都能轻松应对。
20
20
 
21
- modules-dev分为三个部分
21
+ ### 核心特性
22
22
 
23
- 1. 构建工具部分,它在craco基础上封装了一系列插件用以支持文档解析已经远程组件支持
24
- 2. Example部分,它给前端提供了一个集成化的开发环境用来实时预览组件的example部分
25
- 3. 脚手架模板部分,它提供了两个命令行工具 modules-dev-create
26
- modules-dev-libs-init,modules-dev-create可以在远程组件项目中添加一个新的组件即文档目录结构,modules-dev-libs-init可以给前端库提供一个可以运行的开发环境。
23
+ - **零配置开箱即用**:基于 Craco 封装的构建插件,支持远程组件和组件库两种场景
24
+ - **实时预览**:开发环境支持组件示例的实时预览,所见即所得
25
+ - **文档自动生成**:自动解析 `doc/` 目录下的文档和示例代码
26
+ - **命令行工具**:提供 `modules-dev-create` 和 `modules-dev-libs-init` 快速创建组件目录结构
27
+ - **模块联邦支持**:内置 Webpack Module Federation 支持,方便远程组件开发
28
+ - **灵活的路径配置**:通过环境变量自定义组件目录和别名
29
+
30
+ ### 应用场景
31
+
32
+ 1. **远程组件开发**:在开发远程组件时提供实时预览环境,组件发布后可集成到 kne-union 文档系统
33
+ 2. **组件库开发**:为前端组件库提供统一的示例展示和文档生成能力
34
+ 3. **业务项目调试**:在开发模式下集成示例预览,快速验证组件功能
27
35
 
28
36
  ### 构建工具使用
29
37
 
30
- 1. 远程组件
31
- craco.config.js
38
+ #### 远程组件项目
39
+
40
+ 在项目的 `craco.config.js` 中配置 CracoRemoteComponentsPlugin:
32
41
 
33
42
  ```js
34
43
  const {CracoRemoteComponentsPlugin} = require("@kne/modules-dev");
@@ -40,9 +49,9 @@ module.exports = {
40
49
  };
41
50
  ```
42
51
 
43
- 2. 组件库
52
+ #### 组件库项目
44
53
 
45
- example/craco.config.js
54
+ 在示例目录的 `example/craco.config.js` 中配置 CracoLibsExamplePlugin:
46
55
 
47
56
  ```js
48
57
  const {CracoLibsExamplePlugin} = require("@kne/modules-dev");
@@ -54,13 +63,9 @@ module.exports = {
54
63
  };
55
64
  ```
56
65
 
57
- 注意:以上代码通常由命令行工具生成,不需要自己编写
58
-
59
- ### Example部分使用
60
-
61
- 1. 远程组件库中
66
+ ### Example 组件使用
62
67
 
63
- src/App.js
68
+ #### 远程组件库项目
64
69
 
65
70
  ```jsx
66
71
  import createEntry from "@kne/modules-dev/dist/create-entry";
@@ -84,9 +89,7 @@ const App = ({preset, themeToken, ...props}) => {
84
89
  };
85
90
  ```
86
91
 
87
- 2. 业务项目中
88
-
89
- src/bootstrap.js
92
+ #### 业务项目集成开发模式
90
93
 
91
94
  ```jsx
92
95
  if (process.env.NODE_ENV === 'development') {
@@ -104,42 +107,386 @@ if (process.env.NODE_ENV === 'development') {
104
107
  }
105
108
  ```
106
109
 
107
- 注意:以上代码通常由命令行工具生成,不需要自己编写
110
+ ### 命令行工具
111
+
112
+ - `modules-dev-create`:在远程组件项目中创建新的组件目录结构和文档模板
113
+ - `modules-dev-libs-init`:为前端库项目初始化示例开发环境
114
+
115
+ ### 环境变量配置
116
+
117
+ | 变量名 | 说明 | 默认值 |
118
+ |--------|------|--------|
119
+ | MODULES_DEV_BASE_DIR | 组件基础目录 | ./src/components |
120
+ | MODULES_DEV_ALIAS_NAME | 组件别名 | @components |
121
+ | MODULES_DEV_STATIC_BASE_URL | 静态资源基础路径 | /ui_components |
122
+ | MODULES_DEV_PUBLIC_URL | 公共 URL | 自动生成 |
123
+ | CURRENT_VERSION | 当前版本 | 从 package.json 读取 |
124
+ | OPEN_CURRENT_VERSION | 是否在 URL 中显示版本号 | true |
108
125
 
109
126
 
110
127
  ### 示例
111
128
 
112
129
  #### 示例代码
113
130
 
131
+ - 基础用法
132
+ - 展示 createEntry 的基本使用方式,包装业务组件并集成开发环境功能
133
+ - _ModulesDev(@kne/modules-dev)[import * as _ModulesDev from "@kne/modules-dev"],remoteLoader(@kne/remote-loader),antd(antd)
134
+
135
+ ```jsx
136
+ const { createEntry, ExampleRoutes } = _ModulesDev;
137
+ const { createWithRemoteLoader, getPublicPath } = remoteLoader;
138
+ const { Card, Typography, Space } = antd;
139
+ const { useState } = React;
140
+
141
+ const BaseExample = createWithRemoteLoader({
142
+ modules: ['components-core:Global@PureGlobal', 'components-core:InfoPage']
143
+ })(({ remoteModules }) => {
144
+ const [PureGlobal, InfoPage] = remoteModules;
145
+ const [wrapped, setWrapped] = useState(true);
146
+
147
+ const MockComponent = () => (
148
+ <Card title="业务组件" style={{ maxWidth: 400 }}>
149
+ <Typography.Paragraph>这是一个模拟的业务组件内容</Typography.Paragraph>
150
+ </Card>
151
+ );
152
+
153
+ const WrappedComponent = createEntry(MockComponent);
154
+
155
+ return (
156
+ <PureGlobal preset={{
157
+ ajax: async api => {
158
+ return { data: { code: 0, data: api.loader() } };
159
+ },
160
+ apis: {
161
+ example: {
162
+ staticUrl: getPublicPath('modules-dev') || window.PUBLIC_URL,
163
+ getUrl: {
164
+ loader: async ({ params }) => {
165
+ return 'mock-data';
166
+ }
167
+ }
168
+ }
169
+ }
170
+ }}>
171
+ <InfoPage>
172
+ <InfoPage.Part title="基础用法">
173
+ <Space direction="vertical" size="large" style={{ width: '100%' }}>
174
+ <Typography.Title level={3}>createEntry 高阶组件</Typography.Title>
175
+ <Typography.Paragraph>
176
+ createEntry 是一个高阶组件工厂函数,用于包装业务组件,
177
+ 集成开发环境的文档预览功能。当存在组件 README 数据时,
178
+ 会自动显示组件导航和示例页面。
179
+ </Typography.Paragraph>
180
+ <Card title="控制面板">
181
+ <Space>
182
+ <Typography.Text>启用包装:</Typography.Text>
183
+ <Typography.Switch checked={wrapped} onChange={setWrapped} />
184
+ </Space>
185
+ </Card>
186
+ <div style={{ padding: 20, background: '#f5f5f5', borderRadius: 8 }}>
187
+ {wrapped ? <WrappedComponent /> : <MockComponent />}
188
+ </div>
189
+ </Space>
190
+ </InfoPage.Part>
191
+ </InfoPage>
192
+ </PureGlobal>
193
+ );
194
+ });
195
+
196
+ render(<BaseExample />);
197
+
198
+ ```
199
+
200
+ - FontList 图标列表
201
+ - 使用 FontList 组件展示和预览图标字体
202
+ - _ModulesDev(@kne/modules-dev)[import * as _ModulesDev from "@kne/modules-dev"],remoteLoader(@kne/remote-loader),antd(antd)
203
+
204
+ ```jsx
205
+ const { FontList } = _ModulesDev;
206
+ const { createWithRemoteLoader, getPublicPath } = remoteLoader;
207
+ const { Space, Typography, Card } = antd;
208
+
209
+ const FontListExample = createWithRemoteLoader({
210
+ modules: ['components-core:Global@PureGlobal', 'components-core:InfoPage']
211
+ })(({ remoteModules }) => {
212
+ const [PureGlobal, InfoPage] = remoteModules;
213
+
214
+ return (
215
+ <PureGlobal preset={{
216
+ ajax: async api => {
217
+ return { data: { code: 0, data: api.loader() } };
218
+ },
219
+ apis: {
220
+ example: {
221
+ staticUrl: getPublicPath('modules-dev') || window.PUBLIC_URL,
222
+ getUrl: {
223
+ loader: async ({ params }) => {
224
+ return {
225
+ 'kne-font': {
226
+ glyphs: [
227
+ { font_class: 'icon-home' },
228
+ { font_class: 'icon-user' },
229
+ { font_class: 'icon-setting' },
230
+ { font_class: 'icon-delete' },
231
+ { font_class: 'icon-edit' },
232
+ { font_class: 'icon-add' }
233
+ ]
234
+ },
235
+ 'kne-font-colorful': {
236
+ glyphs: [
237
+ { font_class: 'icon-star' },
238
+ { font_class: 'icon-heart' },
239
+ { font_class: 'icon-check' }
240
+ ]
241
+ }
242
+ };
243
+ }
244
+ }
245
+ }
246
+ }
247
+ }}>
248
+ <InfoPage>
249
+ <InfoPage.Part title="FontList 图标列表">
250
+ <Space direction="vertical" size="large" style={{ width: '100%' }}>
251
+ <Typography.Title level={3}>图标字体预览组件</Typography.Title>
252
+ <Typography.Paragraph>
253
+ FontList 组件用于展示和预览图标字体,支持调整图标大小,
254
+ 点击图标名称可以复制使用代码。
255
+ </Typography.Paragraph>
256
+ <Card title="示例展示">
257
+ <FontList fonts={{
258
+ 'kne-font': {
259
+ glyphs: [
260
+ { font_class: 'icon-home' },
261
+ { font_class: 'icon-user' },
262
+ { font_class: 'icon-setting' },
263
+ { font_class: 'icon-delete' },
264
+ { font_class: 'icon-edit' },
265
+ { font_class: 'icon-add' }
266
+ ]
267
+ },
268
+ 'kne-font-colorful': {
269
+ glyphs: [
270
+ { font_class: 'icon-star' },
271
+ { font_class: 'icon-heart' },
272
+ { font_class: 'icon-check' }
273
+ ]
274
+ }
275
+ }} />
276
+ </Card>
277
+ </Space>
278
+ </InfoPage.Part>
279
+ </InfoPage>
280
+ </PureGlobal>
281
+ );
282
+ });
283
+
284
+ render(<FontListExample />);
285
+
286
+ ```
287
+
288
+ - Highlight 代码高亮
289
+ - 使用 Highlight 组件实现代码语法高亮显示
290
+ - _ModulesDev(@kne/modules-dev)[import * as _ModulesDev from "@kne/modules-dev"],antd(antd)
291
+
292
+ ```jsx
293
+ const { Highlight } = _ModulesDev;
294
+ const { Space, Typography, Card, Input } = antd;
295
+ const { useState } = React;
296
+
297
+ const codeTemplate = `import React from 'react';
298
+
299
+ function App() {
300
+ return (
301
+ <div className="app">
302
+ <h1>Hello World</h1>
303
+ <p>Welcome to React</p>
304
+ </div>
305
+ );
306
+ }
307
+
308
+ export default App;`;
309
+
310
+ const HighlightExample = () => {
311
+ const [code, setCode] = useState(codeTemplate);
312
+
313
+ return (
314
+ <Space direction="vertical" size="large" style={{ width: '100%' }}>
315
+ <Typography.Title level={3}>Highlight 代码高亮组件</Typography.Title>
316
+ <Typography.Paragraph>
317
+ Highlight 组件基于 highlight.js 实现,支持多种编程语言的语法高亮显示。
318
+ </Typography.Paragraph>
319
+ <Card title="代码输入">
320
+ <Input.TextArea
321
+ value={code}
322
+ onChange={(e) => setCode(e.target.value)}
323
+ rows={8}
324
+ placeholder="输入要高亮的代码"
325
+ />
326
+ </Card>
327
+ <Card title="高亮效果">
328
+ <Highlight html={`<pre><code class="language-javascript hljs">${escapeHtml(code)}</code></pre>`} />
329
+ </Card>
330
+ </Space>
331
+ );
332
+ };
333
+
334
+ function escapeHtml(text) {
335
+ return text
336
+ .replace(/&/g, '&amp;')
337
+ .replace(/</g, '&lt;')
338
+ .replace(/>/g, '&gt;')
339
+ .replace(/"/g, '&quot;')
340
+ .replace(/'/g, '&#039;');
341
+ }
342
+
343
+ render(<HighlightExample />);
344
+
345
+ ```
114
346
 
115
347
 
116
348
  ### API
117
349
 
118
350
  ```js
119
- const {CracoRemoteComponentsPlugin,CracoLibsExamplePlugin,env} = require('@kne/modules-dev');
351
+ const {CracoRemoteComponentsPlugin, CracoLibsExamplePlugin, env} = require('@kne/modules-dev');
352
+ ```
353
+
354
+ ### CracoRemoteComponentsPlugin
355
+
356
+ 用于远程组件项目的 Craco 插件,自动配置文档解析、模块联邦和 CSS Modules 支持。
357
+
358
+ | 属性名 | 说明 | 类型 | 默认值 |
359
+ |-----|----|----|-----|
360
+ | middleware | 中间件配置 | object | undefined |
361
+
362
+ ### CracoLibsExamplePlugin
363
+
364
+ 用于组件库示例项目的 Craco 插件,自动配置文档解析、版本管理和模块联邦支持。
365
+
366
+ | 属性名 | 说明 | 类型 | 默认值 |
367
+ |-----|----|----|-----|
368
+ | middleware | 中间件配置 | object | undefined |
369
+
370
+ ### env
371
+
372
+ 环境变量配置对象,提供项目构建所需的各种路径和配置信息。
373
+
374
+ | 属性名 | 说明 | 类型 |
375
+ |-----|----|----|
376
+ | appDir | 应用根目录 | string |
377
+ | manifestPath | manifest 文件路径 | string |
378
+ | staticBaseUrl | 静态资源基础 URL | string |
379
+ | moduleBasePath | 组件基础路径 | string |
380
+ | moduleBaseDir | 组件基础目录(绝对路径) | string |
381
+ | moduleAliasName | 组件别名名称 | string |
382
+ | templateDir | 模板目录 | string |
383
+ | templateLibsExampleDir | 组件库示例模板目录 | string |
384
+ | componentsName | 组件名称 | string |
385
+ | openComponentsVersion | 是否在 URL 中显示版本号 | boolean |
386
+ | componentsVersion | 组件版本号 | string |
387
+ | publicUrl | 公共 URL | string |
388
+
389
+ ```js
390
+ import createEntry from '@kne/modules-dev/dist/create-entry';
391
+
392
+ const Entry = createEntry(children);
120
393
  ```
121
394
 
122
- #### CracoRemoteComponentsPlugin
395
+ ### createEntry
396
+
397
+ 高阶组件工厂函数,用于包装业务组件,集成开发环境的文档预览功能。
398
+
399
+ | 属性名 | 说明 | 类型 | 默认值 |
400
+ |-----|----|----|-----|
401
+ | children | 要包装的业务组件 | ReactComponent | - |
402
+ | preset | 全局预设配置 | object | - |
403
+ | projectName | 项目名称 | string | - |
404
+ | themeToken | 主题配置 | object | - |
405
+ | pageProps | 页面属性 | object | - |
406
+ | baseUrl | 基础路径 | string | '' |
407
+
408
+ ### createEntry.ExampleRoutes
409
+
410
+ 示例路由组件,用于渲染组件示例页面。
411
+
412
+ | 属性名 | 说明 | 类型 | 默认值 |
413
+ |-----|----|----|-----|
414
+ | readme | 组件 README 数据 | object | - |
415
+ | paths | 路径配置列表 | array | - |
416
+ | preset | 全局预设配置 | object | - |
417
+ | themeToken | 主题配置 | object | - |
418
+ | baseUrl | 基础路径 | string | '' |
419
+ | projectName | 项目名称 | string | - |
420
+ | pageProps | 页面属性 | object | - |
421
+ | children | 子路由内容 | ReactNode | - |
422
+
423
+ ### createEntry.Example
424
+
425
+ 单个组件示例展示组件。
123
426
 
124
427
  | 属性名 | 说明 | 类型 | 默认值 |
125
428
  |-----|----|----|-----|
429
+ | baseUrl | 基础路径 | string | - |
430
+ | readme | 组件 README 数据 | object | - |
431
+ | pageProps | 页面属性 | object | - |
126
432
 
127
- #### CracoLibsExamplePlugin
433
+ ### createEntry.ExamplePage
434
+
435
+ 示例页面组件,用于展示组件文档和代码示例。
128
436
 
129
437
  | 属性名 | 说明 | 类型 | 默认值 |
130
438
  |-----|----|----|-----|
439
+ | data | 组件数据 | object | - |
440
+ | current | 当前组件 ID | string | - |
441
+ | items | 组件列表 | array | - |
442
+ | pageProps | 页面属性 | object | - |
443
+
444
+ ### createEntry.ExampleContent
131
445
 
132
- #### env
446
+ 示例内容组件,渲染组件的描述、概述、代码示例和 API 文档。
133
447
 
134
448
  | 属性名 | 说明 | 类型 | 默认值 |
135
449
  |-----|----|----|-----|
450
+ | data | 组件数据 | object | - |
136
451
 
137
452
  ```js
138
- import createEntry from '@kne/modules-dev/dist/create-entry';
453
+ import {FontList} from '@kne/modules-dev/dist/index';
454
+ ```
139
455
 
140
- const Entry = createEntry(children);
456
+ ### FontList
457
+
458
+ 图标字体列表展示组件,用于预览和复制图标代码。
459
+
460
+ | 属性名 | 说明 | 类型 | 默认值 |
461
+ |-----|----|----|-----|
462
+ | fonts | 图标字体数据 | object | - |
463
+
464
+ ```js
465
+ import {Example} from '@kne/modules-dev/dist/index';
141
466
  ```
142
467
 
468
+ ### Example
469
+
470
+ 组件示例展示组件(别名,同 createEntry.Example)。
471
+
472
+ | 属性名 | 说明 | 类型 | 默认值 |
473
+ |-----|----|----|-----|
474
+ | baseUrl | 基础路径 | string | - |
475
+ | readme | 组件 README 数据 | object | - |
476
+ | pageProps | 页面属性 | object | - |
477
+
478
+ ```js
479
+ import {ExamplePage} from '@kne/modules-dev/dist/index';
480
+ ```
481
+
482
+ ### ExamplePage
483
+
484
+ 示例页面组件(别名,同 createEntry.ExamplePage)。
485
+
143
486
  | 属性名 | 说明 | 类型 | 默认值 |
144
487
  |-----|----|----|-----|
488
+ | data | 组件数据 | object | - |
489
+ | current | 当前组件 ID | string | - |
490
+ | items | 组件列表 | array | - |
491
+ | pageProps | 页面属性 | object | - |
145
492
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/modules-dev",
3
- "version": "2.1.17",
3
+ "version": "2.1.18",
4
4
  "description": "用于辅助在项目内启动一个规范化组件开发的环境",
5
5
  "publishConfig": {
6
6
  "access": "public",