@dev-to/react-plugin 1.1.2 → 1.1.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.
package/README.md CHANGED
@@ -53,7 +53,7 @@ export default defineConfig({
53
53
  - **Loader UMD**: `/__dev_to__/react/loader.js` - ReactLoader UMD 构建
54
54
  - **Component Loaders**: `/__dev_to__/react/loader/{ComponentName}.js` - 单组件加载器
55
55
 
56
- 这些路径与事件名常量集中在 `@dev-to/react-shared`,用于保证 Vite 侧与宿主侧协议一致。
56
+ 这些路径与事件名常量集中在 `@dev-to/shared`,用于保证 Vite 侧与宿主侧协议一致。
57
57
 
58
58
  ### HMR 事件
59
59
  - Full Reload: `dev_to:react:full-reload`
package/bin/dev-to.js CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { spawnSync } from 'node:child_process'
3
+ import fs from 'node:fs'
3
4
  import { createRequire } from 'node:module'
4
5
  import path from 'node:path'
5
6
  import process from 'node:process'
@@ -70,9 +71,30 @@ function resolveViteBin() {
70
71
  const require = createRequire(import.meta.url)
71
72
  const searchPaths = [process.cwd(), path.join(process.cwd(), 'node_modules')]
72
73
 
74
+ const resolved = resolvePath(require, 'vite/bin/vite.js', searchPaths)
75
+ if (resolved) {
76
+ return resolved
77
+ }
78
+
79
+ const viteEntry = resolvePath(require, 'vite', searchPaths)
80
+ if (viteEntry) {
81
+ const viteRoot = findPackageRoot(viteEntry)
82
+ if (viteRoot) {
83
+ const binPath = resolveBinFromPackage(viteRoot)
84
+ if (binPath) {
85
+ return binPath
86
+ }
87
+ }
88
+ }
89
+
90
+ console.error('Vite is not installed or could not be resolved. Please add it to devDependencies.')
91
+ process.exit(1)
92
+ }
93
+
94
+ function resolvePath(require, specifier, searchPaths) {
73
95
  for (const base of searchPaths) {
74
96
  try {
75
- return require.resolve('vite/bin/vite.js', { paths: [base] })
97
+ return require.resolve(specifier, { paths: [base] })
76
98
  }
77
99
  catch {
78
100
  // Try next path.
@@ -80,14 +102,67 @@ function resolveViteBin() {
80
102
  }
81
103
 
82
104
  try {
83
- return require.resolve('vite/bin/vite.js')
105
+ return require.resolve(specifier)
84
106
  }
85
107
  catch {
86
- console.error('Vite is not installed. Please add it to devDependencies.')
87
- process.exit(1)
108
+ return null
88
109
  }
89
110
  }
90
111
 
112
+ function findPackageRoot(startPath) {
113
+ let current = path.dirname(startPath)
114
+ while (true) {
115
+ const pkgPath = path.join(current, 'package.json')
116
+ if (fs.existsSync(pkgPath)) {
117
+ try {
118
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
119
+ if (pkg && pkg.name === 'vite') {
120
+ return current
121
+ }
122
+ }
123
+ catch {
124
+ // Keep walking up.
125
+ }
126
+ }
127
+
128
+ const parent = path.dirname(current)
129
+ if (parent === current) {
130
+ break
131
+ }
132
+ current = parent
133
+ }
134
+
135
+ return null
136
+ }
137
+
138
+ function resolveBinFromPackage(pkgRoot) {
139
+ const pkgPath = path.join(pkgRoot, 'package.json')
140
+ if (!fs.existsSync(pkgPath)) {
141
+ return null
142
+ }
143
+
144
+ try {
145
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
146
+ if (pkg && pkg.bin) {
147
+ const binEntry = typeof pkg.bin === 'string'
148
+ ? pkg.bin
149
+ : pkg.bin.vite || pkg.bin['vite']
150
+ if (binEntry) {
151
+ const binPath = path.resolve(pkgRoot, binEntry)
152
+ if (fs.existsSync(binPath)) {
153
+ return binPath
154
+ }
155
+ }
156
+ }
157
+ }
158
+ catch {
159
+ // Fall back to default path.
160
+ }
161
+
162
+ const fallback = path.resolve(pkgRoot, 'bin', 'vite.js')
163
+ return fs.existsSync(fallback) ? fallback : null
164
+ }
165
+
91
166
  function printHelp() {
92
167
  console.log(
93
168
  [
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
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";
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/shared";
2
2
  import node_fs from "node:fs";
3
3
  import picocolors from "picocolors";
4
4
  import { mergeConfig } from "vite";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dev-to/react-plugin",
3
- "version": "1.1.2",
3
+ "version": "1.1.6",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -35,7 +35,7 @@
35
35
  "dependencies": {
36
36
  "picocolors": "^1.1.0",
37
37
  "typescript": "^5.4.5",
38
- "@dev-to/react-shared": "1.0.1"
38
+ "@dev-to/shared": "1.0.4"
39
39
  },
40
40
  "scripts": {
41
41
  "build": "rslib build",