@dev-to/react-plugin 1.0.2 → 1.1.2

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/README.md CHANGED
@@ -33,7 +33,7 @@ export default defineConfig({
33
33
 
34
34
  ### components 参数形态
35
35
 
36
- - `devToReactPlugin()`:通配符模式(默认 `'*': '/'`),便于开发;但 `vite build --mode lib` 需要显式 componentName。
36
+ - `devToReactPlugin()`:通配符模式(默认 `'*': '/'`),便于开发;但 `dev-to build`(等价于 `vite build --mode lib`)需要显式 componentName。
37
37
  - `devToReactPlugin('MyCard')`:字符串快捷模式,等价于 `{ MyCard: '/' }`。
38
38
  - `devToReactPlugin({ MyCard: 'src/MyCard.tsx' })`:显式映射(推荐,且 lib 构建必须)。
39
39
 
@@ -61,7 +61,8 @@ export default defineConfig({
61
61
 
62
62
  ## 生产构建(Library Mode)
63
63
 
64
- 执行 `vite build --mode lib` 时,插件会按 `components` 参数为每个组件产出一个 UMD bundle,默认输出到 `dist/<component>/`。
64
+ 执行 `dev-to build`(等价于 `vite build --mode lib`)时,插件会按 `components` 参数为每个组件产出一个 UMD bundle,默认输出到 `dist/<component>/`。
65
+ 支持透传 Vite build 参数,例如:`dev-to build --sourcemap --outDir dist-lib`。
65
66
 
66
67
  > 提示:`'*': '/'` 的通配符模式仅适合开发;lib 构建请显式列出组件名。
67
68
 
package/bin/dev-to.js ADDED
@@ -0,0 +1,104 @@
1
+ #!/usr/bin/env node
2
+ import { spawnSync } from 'node:child_process'
3
+ import { createRequire } from 'node:module'
4
+ import path from 'node:path'
5
+ import process from 'node:process'
6
+
7
+ const { console } = globalThis
8
+
9
+ const args = process.argv.slice(2)
10
+
11
+ if (args.length === 0 || args[0] === '-h' || args[0] === '--help') {
12
+ printHelp()
13
+ process.exit(0)
14
+ }
15
+
16
+ const command = args[0]
17
+ if (command !== 'build') {
18
+ console.error(`Unknown command: ${command}`)
19
+ printHelp()
20
+ process.exit(1)
21
+ }
22
+
23
+ const { args: forwardedArgs, removed } = stripModeArgs(args.slice(1))
24
+ if (removed) {
25
+ console.error('Note: --mode is managed by dev-to (lib). The provided mode is ignored.')
26
+ }
27
+
28
+ const result = spawnSync(
29
+ process.execPath,
30
+ [resolveViteBin(), 'build', '--mode', 'lib', ...forwardedArgs],
31
+ { stdio: 'inherit' },
32
+ )
33
+
34
+ if (result.error) {
35
+ console.error(`Failed to run Vite: ${result.error.message}`)
36
+ process.exit(1)
37
+ }
38
+
39
+ process.exit(result.status ?? 1)
40
+
41
+ function stripModeArgs(userArgs) {
42
+ const cleaned = []
43
+ let removed = false
44
+
45
+ for (let i = 0; i < userArgs.length; i += 1) {
46
+ const arg = userArgs[i]
47
+ if (arg === '--') {
48
+ continue
49
+ }
50
+ if (arg === '--mode' || arg === '-m') {
51
+ removed = true
52
+ i += 1
53
+ continue
54
+ }
55
+ if (arg.startsWith('--mode=')) {
56
+ removed = true
57
+ continue
58
+ }
59
+ if (arg.startsWith('-m') && arg.length > 2) {
60
+ removed = true
61
+ continue
62
+ }
63
+ cleaned.push(arg)
64
+ }
65
+
66
+ return { args: cleaned, removed }
67
+ }
68
+
69
+ function resolveViteBin() {
70
+ const require = createRequire(import.meta.url)
71
+ const searchPaths = [process.cwd(), path.join(process.cwd(), 'node_modules')]
72
+
73
+ for (const base of searchPaths) {
74
+ try {
75
+ return require.resolve('vite/bin/vite.js', { paths: [base] })
76
+ }
77
+ catch {
78
+ // Try next path.
79
+ }
80
+ }
81
+
82
+ try {
83
+ return require.resolve('vite/bin/vite.js')
84
+ }
85
+ catch {
86
+ console.error('Vite is not installed. Please add it to devDependencies.')
87
+ process.exit(1)
88
+ }
89
+ }
90
+
91
+ function printHelp() {
92
+ console.log(
93
+ [
94
+ 'dev-to <command>',
95
+ '',
96
+ 'Commands:',
97
+ ' build Run "vite build --mode lib" and forward extra args.',
98
+ '',
99
+ 'Examples:',
100
+ ' dev-to build',
101
+ ' dev-to build --sourcemap --outDir dist-lib',
102
+ ].join('\n'),
103
+ )
104
+ }
package/dist/876.js ADDED
@@ -0,0 +1,3 @@
1
+ export { default as node_path } from "node:path";
2
+ export { exec, spawnSync } from "node:child_process";
3
+ export { createRequire } from "node:module";
package/dist/cli.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
package/dist/cli.js ADDED
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env node
2
+ import node_process from "node:process";
3
+ import { createRequire, node_path, spawnSync } from "./876.js";
4
+ const args = node_process.argv.slice(2);
5
+ if (0 === args.length || '-h' === args[0] || '--help' === args[0]) {
6
+ printHelp();
7
+ node_process.exit(0);
8
+ }
9
+ const command = args[0];
10
+ if ('build' !== command) {
11
+ console.error(`Unknown command: ${command}`);
12
+ printHelp();
13
+ node_process.exit(1);
14
+ }
15
+ const { args: forwardedArgs, removed: cli_removed } = stripModeArgs(args.slice(1));
16
+ if (cli_removed) console.error('Note: --mode is managed by dev-to (lib). The provided mode is ignored.');
17
+ const viteBin = resolveViteBin();
18
+ const result = spawnSync(node_process.execPath, [
19
+ viteBin,
20
+ 'build',
21
+ '--mode',
22
+ 'lib',
23
+ ...forwardedArgs
24
+ ], {
25
+ stdio: 'inherit'
26
+ });
27
+ if (result.error) {
28
+ console.error(`Failed to run Vite: ${result.error.message}`);
29
+ node_process.exit(1);
30
+ }
31
+ node_process.exit(result.status ?? 1);
32
+ function stripModeArgs(userArgs) {
33
+ const cleaned = [];
34
+ let removed = false;
35
+ for(let i = 0; i < userArgs.length; i += 1){
36
+ const arg = userArgs[i];
37
+ if ('--' !== arg) {
38
+ if ('--mode' === arg || '-m' === arg) {
39
+ removed = true;
40
+ i += 1;
41
+ continue;
42
+ }
43
+ if (arg.startsWith('--mode=')) {
44
+ removed = true;
45
+ continue;
46
+ }
47
+ if (arg.startsWith('-m') && arg.length > 2) {
48
+ removed = true;
49
+ continue;
50
+ }
51
+ cleaned.push(arg);
52
+ }
53
+ }
54
+ return {
55
+ args: cleaned,
56
+ removed
57
+ };
58
+ }
59
+ function resolveViteBin() {
60
+ const require = createRequire(import.meta.url);
61
+ const searchPaths = [
62
+ node_process.cwd(),
63
+ node_path.join(node_process.cwd(), 'node_modules')
64
+ ];
65
+ for (const base of searchPaths)try {
66
+ return require.resolve('vite/bin/vite.js', {
67
+ paths: [
68
+ base
69
+ ]
70
+ });
71
+ } catch {}
72
+ try {
73
+ return require.resolve('vite/bin/vite.js');
74
+ } catch {
75
+ console.error('Vite is not installed. Please add it to devDependencies.');
76
+ node_process.exit(1);
77
+ }
78
+ }
79
+ function printHelp() {
80
+ console.log('dev-to <command>\n\nCommands:\n build Run "vite build --mode lib" and forward extra args.\n\nExamples:\n dev-to build\n dev-to build --sourcemap --outDir dist-lib');
81
+ }
package/dist/index.js CHANGED
@@ -1,13 +1,11 @@
1
1
  import { DEV_TO_BASE_PATH, DEV_TO_DEBUG_HTML_PATH, DEV_TO_DEBUG_JSON_PATH, DEV_TO_DISCOVERY_PATH, DEV_TO_NAMESPACE, DEV_TO_REACT_BASE_PATH, DEV_TO_REACT_CONTRACT_KEY, DEV_TO_REACT_CONTRACT_PATH, DEV_TO_REACT_DEBUG_STATE_KEY, DEV_TO_REACT_DID_OPEN_BROWSER_KEY, DEV_TO_REACT_EVENT_FULL_RELOAD, DEV_TO_REACT_EVENT_HMR_UPDATE, DEV_TO_REACT_INIT_PATH, DEV_TO_REACT_LOADER_BASE_PATH, DEV_TO_REACT_LOADER_UMD_PATH, DEV_TO_REACT_NAMESPACE, DEV_TO_REACT_ORIGIN_KEY, DEV_TO_REACT_RESOLVE_ASSET_KEY, DEV_TO_REACT_RUNTIME_PATH } from "@dev-to/react-shared";
2
2
  import node_fs from "node:fs";
3
- import node_path from "node:path";
4
3
  import picocolors from "picocolors";
5
4
  import { mergeConfig } from "vite";
6
- import { exec } from "node:child_process";
7
- import { createRequire } from "node:module";
8
5
  import { fileURLToPath } from "node:url";
9
6
  import node_os from "node:os";
10
7
  import typescript from "typescript";
8
+ import { exec, node_path, createRequire } from "./876.js";
11
9
  const PLUGIN_NAME = `${DEV_TO_NAMESPACE}_${DEV_TO_REACT_NAMESPACE}`;
12
10
  const constants_PLUGIN_LOG_PREFIX = `[${DEV_TO_NAMESPACE}:${DEV_TO_REACT_NAMESPACE}]`;
13
11
  const STABLE_BASE_PATH = DEV_TO_BASE_PATH;
@@ -383,7 +381,7 @@ root.render(React.createElement(window.ComponentName, { prop1: <span class="str"
383
381
 
384
382
  <div class="card">
385
383
  <h3>📦 构建与部署</h3>
386
- <p class="muted">执行 <code>vite build --mode lib</code> 将组件打包为 UMD 格式以供发布。</p>
384
+ <p class="muted">执行 <code>dev-to build</code>(等价于 <code>vite build --mode lib</code>)将组件打包为 UMD 格式以供发布。</p>
387
385
 
388
386
  <div class="build-grid">
389
387
  <div class="build-card">
@@ -1326,7 +1324,7 @@ function installDebugTools(server, ctx, state) {
1326
1324
  suggested: requestOrigin,
1327
1325
  snippet: `localStorage.setItem('VITE_DEV_SERVER_ORIGIN', '${requestOrigin}'); location.reload();`,
1328
1326
  libBuild: {
1329
- command: 'vite build --mode lib',
1327
+ command: 'dev-to build',
1330
1328
  env: {
1331
1329
  DEV_TO_REACT_LIB_SECTION: libComponentExample
1332
1330
  },
@@ -1353,7 +1351,7 @@ function installDebugTools(server, ctx, state) {
1353
1351
  '宿主侧需设置 localStorage.VITE_DEV_SERVER_ORIGIN(可从 originCandidates 里选择一个可访问的 origin)。',
1354
1352
  'components 参数的 key 必须与后端返回的 componentName 完全一致(严格匹配)。',
1355
1353
  'Fast Refresh 关键:必须先 import init.js(安装 react-refresh preamble),再 import react-dom/client。',
1356
- '如需产出可分发 UMD 包:使用 `vite build --mode lib`(仅构建 components 指定的组件,输出到 dist/<component>/)。'
1354
+ '如需产出可分发 UMD 包:使用 `dev-to build`(等价于 `vite build --mode lib`,仅构建 components 指定的组件,输出到 dist/<component>/)。'
1357
1355
  ]
1358
1356
  }, null, 2));
1359
1357
  return;
@@ -1891,7 +1889,7 @@ function devToReactPlugin(components, options) {
1891
1889
  if (1 === Object.keys(resolvedConfig.componentMap).length && '/' === resolvedConfig.componentMap['*']) {
1892
1890
  const warn = server.config.logger?.warn?.bind(server.config.logger) ?? console.warn;
1893
1891
  warn('');
1894
- warn(`⚠️ ${constants_PLUGIN_LOG_PREFIX} No componentName configured. This works in dev mode but will fail in library build (--mode lib).`);
1892
+ warn(`⚠️ ${constants_PLUGIN_LOG_PREFIX} No componentName configured. This works in dev mode but will fail in library build (dev-to build / --mode lib).`);
1895
1893
  warn(' Please use devToReactPlugin({ ComponentName: "src/ComponentName.tsx" }) or devToReactPlugin({ ComponentName: "/" }) to specify components.');
1896
1894
  warn(' Or use wildcard: devToReactPlugin({ "*": "/" }) or devToReactPlugin("*")');
1897
1895
  warn('');
@@ -1932,7 +1930,7 @@ function devToReactPlugin(components, options) {
1932
1930
  }).css;
1933
1931
  if (isLibBuild(env)) {
1934
1932
  const actualNames = Object.keys(contract.dev.componentMap).filter((n)=>'*' !== n);
1935
- if (0 === actualNames.length && !process.env.DEV_TO_REACT_LIB_SECTION) throw new Error(` ${constants_PLUGIN_LOG_PREFIX} Library build (--mode lib) requires at least one explicit componentName for identification and distribution.\nCurrent configuration is in "global fallback mode", which cannot determine build targets.\n\nPlease use one of the following to specify components:\n - devToReactPlugin('ComponentName')\n - devToReactPlugin({ ComponentName: '/' })\n - devToReactPlugin({ ComponentName: 'src/ComponentName.tsx' })\n\n💡 Tip: Wildcards are convenient for development, but explicit naming is required for production builds.`);
1933
+ if (0 === actualNames.length && !process.env.DEV_TO_REACT_LIB_SECTION) throw new Error(` ${constants_PLUGIN_LOG_PREFIX} Library build (dev-to build / --mode lib) requires at least one explicit componentName for identification and distribution.\nCurrent configuration is in "global fallback mode", which cannot determine build targets.\n\nPlease use one of the following to specify components:\n - devToReactPlugin('ComponentName')\n - devToReactPlugin({ ComponentName: '/' })\n - devToReactPlugin({ ComponentName: 'src/ComponentName.tsx' })\n\n💡 Tip: Wildcards are convenient for development, but explicit naming is required for production builds.`);
1936
1934
  const isChild = '1' === process.env.DEV_TO_REACT_LIB_CHILD;
1937
1935
  const buildTargets = resolveBuildTargets({
1938
1936
  componentMap: contract.dev.componentMap,
package/dist/types.d.ts CHANGED
@@ -31,7 +31,7 @@ export interface DevToReactPluginOptions {
31
31
  */
32
32
  css?: CSSOptions | false;
33
33
  /**
34
- * Build configuration passed to Vite (only applies when running `vite build --mode lib`).
34
+ * Build configuration passed to Vite (only applies when running `dev-to build` / `vite build --mode lib`).
35
35
  *
36
36
  * Typical use cases:
37
37
  * - Adjust asset inlining: `assetsInlineLimit`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dev-to/react-plugin",
3
- "version": "1.0.2",
3
+ "version": "1.1.2",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -11,8 +11,12 @@
11
11
  "import": "./dist/index.js"
12
12
  }
13
13
  },
14
+ "bin": {
15
+ "dev-to": "./bin/dev-to.js"
16
+ },
14
17
  "files": [
15
- "dist"
18
+ "dist",
19
+ "bin"
16
20
  ],
17
21
  "peerDependencies": {
18
22
  "react": ">=18.0.0",
@@ -31,7 +35,7 @@
31
35
  "dependencies": {
32
36
  "picocolors": "^1.1.0",
33
37
  "typescript": "^5.4.5",
34
- "@dev-to/react-shared": "1.0.0"
38
+ "@dev-to/react-shared": "1.0.1"
35
39
  },
36
40
  "scripts": {
37
41
  "build": "rslib build",