doc-render-sdk 0.0.4 → 0.0.6

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.
@@ -3,7 +3,6 @@
3
3
  /**
4
4
  * Doc SDK CLI 工具
5
5
  */
6
-
7
6
  const { program } = require('commander');
8
7
  const path = require('path');
9
8
  const fs = require('fs');
@@ -49,7 +48,7 @@ program
49
48
  .description('启动开发服务器')
50
49
  .option('-p, --port <port>', '端口号', '8080')
51
50
  .option('-h, --host <host>', '主机地址', 'localhost')
52
- .action((options) => {
51
+ .action(async (options) => {
53
52
  console.log('🚀 启动开发服务器...');
54
53
 
55
54
  const configPath = findConfig();
@@ -58,7 +57,7 @@ program
58
57
  process.exit(1);
59
58
  }
60
59
 
61
- startDevServer(configPath, options);
60
+ await startDevServer(configPath, options);
62
61
  });
63
62
 
64
63
  // 构建项目
@@ -66,7 +65,7 @@ program
66
65
  .command('build')
67
66
  .description('构建文档站点')
68
67
  .option('-o, --output <dir>', '输出目录', 'dist')
69
- .action((options) => {
68
+ .action(async (options) => {
70
69
  console.log('📦 构建文档站点...');
71
70
 
72
71
  const configPath = findConfig();
@@ -75,7 +74,7 @@ program
75
74
  process.exit(1);
76
75
  }
77
76
 
78
- buildProject(configPath, options);
77
+ await buildProject(configPath, options);
79
78
  });
80
79
 
81
80
 
@@ -105,19 +104,18 @@ async function createProject(projectDir, template) {
105
104
  version: '1.0.0',
106
105
  description: 'Documentation site built with Doc SDK',
107
106
  main: 'index.js',
108
- scripts: {
109
- dev: 'doc-render-sdk dev',
110
- build: 'doc-render-sdk build',
111
- preview: 'doc-render-sdk preview'
112
- },
113
- dependencies: {
114
- 'doc-render-sdk': sdkVersion
115
- },
116
- devDependencies: {
117
- webpack: '^5.88.0',
118
- 'webpack-cli': '^5.1.0',
119
- 'webpack-dev-server': '^4.15.0'
120
- }
107
+ scripts: {
108
+ dev: 'doc-render-sdk dev',
109
+ build: 'doc-render-sdk build',
110
+ preview: 'doc-render-sdk preview'
111
+ },
112
+ dependencies: {
113
+ 'doc-render-sdk': sdkVersion
114
+ },
115
+ devDependencies: {
116
+ vite: '^5.0.0',
117
+ '@vitejs/plugin-react': '^3.1.0'
118
+ }
121
119
  };
122
120
 
123
121
  fs.writeFileSync(
@@ -223,6 +221,7 @@ docSdk.render('#app');
223
221
  </head>
224
222
  <body>
225
223
  <div id="app"></div>
224
+ <script src="index.js"></script>
226
225
  </body>
227
226
  </html>`;
228
227
 
@@ -278,71 +277,118 @@ function findConfig() {
278
277
  * 启动开发服务器
279
278
  */
280
279
  function startDevServer(configPath, options) {
281
- const webpackConfig = generateWebpackConfig(configPath, 'development', options);
282
- const configFile = path.join(__dirname, '../webpack.temp.js');
283
-
284
- fs.writeFileSync(configFile, `module.exports = ${JSON.stringify(webpackConfig, null, 2)};`);
285
-
286
- const child = spawn('npx', ['webpack', 'serve', '--config', configFile], {
287
- stdio: 'inherit',
288
- shell: true
289
- });
290
-
291
- child.on('close', (code) => {
292
- fs.unlinkSync(configFile);
293
- process.exit(code);
294
- });
280
+ // Use Vite Node API to create a dev server via createViteServer
281
+ (async () => {
282
+ try {
283
+ const server = await createViteServer({
284
+ root: process.cwd(),
285
+ server: {
286
+ port: Number(options.port) || 8080,
287
+ host: options.host || 'localhost'
288
+ },
289
+ plugins: createVitePlugins()
290
+ });
291
+
292
+ await server.listen();
293
+ server.printUrls();
294
+ } catch (err) {
295
+ console.error('❌ 启动 Vite 开发服务器失败:', err);
296
+ process.exit(1);
297
+ }
298
+ })();
295
299
  }
296
300
 
297
301
  /**
298
302
  * 构建项目
299
303
  */
300
304
  function buildProject(configPath, options) {
301
- const webpackConfig = generateWebpackConfig(configPath, 'production', options);
302
- const configFile = path.join(__dirname, '../webpack.temp.js');
303
-
304
- fs.writeFileSync(configFile, `module.exports = ${JSON.stringify(webpackConfig, null, 2)};`);
305
-
306
- const child = spawn('npx', ['webpack', '--config', configFile], {
307
- stdio: 'inherit',
308
- shell: true
309
- });
305
+ // Use Vite build API with plugin-react
306
+ (async () => {
307
+ try {
308
+ const { build } = require('vite');
309
+ const outDir = options.output || 'dist';
310
+
311
+ console.log('📦 running vite build...');
312
+
313
+ await build({
314
+ root: process.cwd(),
315
+ build: {
316
+ outDir
317
+ },
318
+ plugins: createVitePlugins()
319
+ });
310
320
 
311
- child.on('close', (code) => {
312
- fs.unlinkSync(configFile);
313
- if (code === 0) {
314
321
  console.log('✅ 构建完成!');
322
+ } catch (err) {
323
+ console.error('❌ Vite 构建失败:', err);
324
+ process.exit(1);
315
325
  }
316
- process.exit(code);
317
- });
326
+ })();
318
327
  }
319
328
 
320
329
  /**
321
- * 预览构建结果
330
+ * Create Vite server with shared plugins
322
331
  */
323
- function previewBuild(options) {
324
- const express = require('express');
325
- const app = express();
326
-
327
- app.use(express.static(options.dir));
328
-
329
- app.listen(options.port, () => {
330
- console.log(`📖 预览地址: http://localhost:${options.port}`);
331
- });
332
+ function createViteServer(options = {}) {
333
+ const { createServer } = require('vite');
334
+ const plugins = createVitePlugins();
335
+ return createServer({ ...options, plugins });
336
+ }
337
+
338
+ function createVitePlugins() {
339
+ try {
340
+ const reactPlugin = require('@vitejs/plugin-react');
341
+ return [reactPlugin()];
342
+ } catch (err) {
343
+ // If plugin not installed, return empty array and let Vite warn later
344
+ return [];
345
+ }
332
346
  }
333
347
 
334
348
  /**
335
- * 生成webpack配置
349
+ * 预览构建结果
336
350
  */
337
- function generateWebpackConfig(configPath, mode, options) {
338
- // 这里应该生成完整的webpack配置
339
- // 简化版本,实际使用时需要完善
340
- return {
341
- mode,
342
- entry: './index.js',
343
- output: {
344
- path: path.resolve(options.output || 'dist'),
345
- filename: 'bundle.js'
351
+ function previewBuild(options) {
352
+ // Use Vite programmatic preview API if available, else fallback to npx vite preview
353
+ const distDir = options.dir || 'dist';
354
+ const port = Number(options.port) || 3000;
355
+
356
+ try {
357
+ const vite = require('vite');
358
+ if (typeof vite.preview === 'function') {
359
+ // Vite exposes preview function in some versions
360
+ (async () => {
361
+ try {
362
+ const server = await vite.preview({ root: process.cwd(), preview: { port } });
363
+ console.log(`📖 预览地址: http://localhost:${port}`);
364
+ } catch (err) {
365
+ console.error('❌ 启动 Vite preview 失败:', err);
366
+ process.exit(1);
367
+ }
368
+ })();
369
+ return;
346
370
  }
347
- };
348
- }
371
+
372
+ // Fallback: try to create a server configured for preview
373
+ if (typeof vite.createServer === 'function') {
374
+ (async () => {
375
+ try {
376
+ const server = await vite.createServer({ root: process.cwd(), preview: { port } });
377
+ await server.listen();
378
+ console.log(`📖 预览地址: http://localhost:${port}`);
379
+ } catch (err) {
380
+ // continue to fallback
381
+ console.error('❌ 使用 createServer 作为 preview 启动失败,回退到 CLI:', err.message || err);
382
+ }
383
+ })();
384
+ return;
385
+ }
386
+ } catch (err) {
387
+ // vite not installed locally, will fallback to npx
388
+ }
389
+
390
+ // Final fallback: spawn npx vite preview
391
+ const args = ['vite', 'preview', '--port', String(port)];
392
+ const child = spawn('npx', args, { stdio: 'inherit', shell: true, cwd: process.cwd() });
393
+ child.on('close', (code) => process.exit(code));
394
+ }
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ComponentType, ReactNode } from "react";
2
- import * as react_jsx_runtime0 from "react/jsx-runtime";
2
+ import * as react_jsx_runtime1 from "react/jsx-runtime";
3
3
 
4
4
  //#region src/core/PluginManager.d.ts
5
5
  /**
@@ -572,7 +572,7 @@ declare const Layout: ({
572
572
  routerManager: any;
573
573
  componentRegistry: any;
574
574
  onRouteChange: any;
575
- }) => react_jsx_runtime0.JSX.Element;
575
+ }) => react_jsx_runtime1.JSX.Element;
576
576
  //#endregion
577
577
  //#region src/components/Navigation.d.ts
578
578
  /**
@@ -592,7 +592,7 @@ declare const Navigation: ({
592
592
  componentRegistry: any;
593
593
  onNavigate: any;
594
594
  compact?: boolean | undefined;
595
- }) => react_jsx_runtime0.JSX.Element;
595
+ }) => react_jsx_runtime1.JSX.Element;
596
596
  //#endregion
597
597
  //#region src/components/Demo.d.ts
598
598
  /**
@@ -610,7 +610,7 @@ declare const Demo: ({
610
610
  config: any;
611
611
  theme: any;
612
612
  renderer: any;
613
- }) => react_jsx_runtime0.JSX.Element;
613
+ }) => react_jsx_runtime1.JSX.Element;
614
614
  //#endregion
615
615
  //#region src/components/ApiDoc.d.ts
616
616
  /**
@@ -626,7 +626,7 @@ declare const ApiDoc: ({
626
626
  componentId: any;
627
627
  config: any;
628
628
  theme: any;
629
- }) => react_jsx_runtime0.JSX.Element;
629
+ }) => react_jsx_runtime1.JSX.Element;
630
630
  //#endregion
631
631
  //#region src/components/CodeBlock.d.ts
632
632
  /**
@@ -646,7 +646,7 @@ declare const CodeBlock: ({
646
646
  showLineNumbers?: boolean | undefined;
647
647
  highlightLines?: never[] | undefined;
648
648
  className?: string | undefined;
649
- }) => react_jsx_runtime0.JSX.Element;
649
+ }) => react_jsx_runtime1.JSX.Element;
650
650
  //#endregion
651
651
  //#region src/index.d.ts
652
652
  declare class DocSDK {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doc-render-sdk",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "A powerful documentation rendering SDK for component libraries",
5
5
  "main": "dist/index.mjs",
6
6
  "module": "dist/index.mjs",