vite-plugin-enter-dev 0.0.7 → 0.0.10-alpha.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/dist/index.d.ts CHANGED
@@ -5,6 +5,17 @@ interface DevOptions {
5
5
  * 是否注入消息监听器脚本(默认:true)
6
6
  */
7
7
  injectMessageListener?: boolean;
8
+ /**
9
+ * 是否注册 @vitejs/plugin-react(默认:true)。
10
+ *
11
+ * 宿主框架自己已经注册了 React 转换时必须传 false:vinext 会在
12
+ * configResolved 里检测重复注册并直接抛错
13
+ * ("[vinext] Duplicate @vitejs/plugin-react detected"),
14
+ * 整个 dev server 和 build 都起不来。关掉后本插件只保留
15
+ * allowedHosts / 端口 / base / 消息监听,代价是失去可视化编辑
16
+ * 需要的源码位置注入。
17
+ */
18
+ react?: boolean;
8
19
  }
9
20
  declare function enterDevPlugin(options?: DevOptions): Plugin[];
10
21
  interface ProdOptions {
package/dist/index.js CHANGED
@@ -3,10 +3,189 @@ import react from '@vitejs/plugin-react';
3
3
  // src/index.ts
4
4
 
5
5
  // src/sourceInfoInjectPlugin.ts
6
+ var threeFiberElems = /* @__PURE__ */ new Set([
7
+ "object3D",
8
+ "audioListener",
9
+ "positionalAudio",
10
+ "mesh",
11
+ "batchedMesh",
12
+ "instancedMesh",
13
+ "scene",
14
+ "sprite",
15
+ "lOD",
16
+ "skinnedMesh",
17
+ "skeleton",
18
+ "bone",
19
+ "lineSegments",
20
+ "lineLoop",
21
+ "points",
22
+ "group",
23
+ "camera",
24
+ "perspectiveCamera",
25
+ "orthographicCamera",
26
+ "cubeCamera",
27
+ "arrayCamera",
28
+ "instancedBufferGeometry",
29
+ "bufferGeometry",
30
+ "boxBufferGeometry",
31
+ "circleBufferGeometry",
32
+ "coneBufferGeometry",
33
+ "cylinderBufferGeometry",
34
+ "dodecahedronBufferGeometry",
35
+ "extrudeBufferGeometry",
36
+ "icosahedronBufferGeometry",
37
+ "latheBufferGeometry",
38
+ "octahedronBufferGeometry",
39
+ "planeBufferGeometry",
40
+ "polyhedronBufferGeometry",
41
+ "ringBufferGeometry",
42
+ "shapeBufferGeometry",
43
+ "sphereBufferGeometry",
44
+ "tetrahedronBufferGeometry",
45
+ "torusBufferGeometry",
46
+ "torusKnotBufferGeometry",
47
+ "tubeBufferGeometry",
48
+ "wireframeGeometry",
49
+ "tetrahedronGeometry",
50
+ "octahedronGeometry",
51
+ "icosahedronGeometry",
52
+ "dodecahedronGeometry",
53
+ "polyhedronGeometry",
54
+ "tubeGeometry",
55
+ "torusKnotGeometry",
56
+ "torusGeometry",
57
+ "sphereGeometry",
58
+ "ringGeometry",
59
+ "planeGeometry",
60
+ "latheGeometry",
61
+ "shapeGeometry",
62
+ "extrudeGeometry",
63
+ "edgesGeometry",
64
+ "coneGeometry",
65
+ "cylinderGeometry",
66
+ "circleGeometry",
67
+ "boxGeometry",
68
+ "capsuleGeometry",
69
+ "material",
70
+ "shadowMaterial",
71
+ "spriteMaterial",
72
+ "rawShaderMaterial",
73
+ "shaderMaterial",
74
+ "pointsMaterial",
75
+ "meshPhysicalMaterial",
76
+ "meshStandardMaterial",
77
+ "meshPhongMaterial",
78
+ "meshToonMaterial",
79
+ "meshNormalMaterial",
80
+ "meshLambertMaterial",
81
+ "meshDepthMaterial",
82
+ "meshDistanceMaterial",
83
+ "meshBasicMaterial",
84
+ "meshMatcapMaterial",
85
+ "lineDashedMaterial",
86
+ "lineBasicMaterial",
87
+ "primitive",
88
+ "light",
89
+ "spotLightShadow",
90
+ "spotLight",
91
+ "pointLight",
92
+ "rectAreaLight",
93
+ "hemisphereLight",
94
+ "directionalLightShadow",
95
+ "directionalLight",
96
+ "ambientLight",
97
+ "lightShadow",
98
+ "ambientLightProbe",
99
+ "hemisphereLightProbe",
100
+ "lightProbe",
101
+ "spotLightHelper",
102
+ "skeletonHelper",
103
+ "pointLightHelper",
104
+ "hemisphereLightHelper",
105
+ "gridHelper",
106
+ "polarGridHelper",
107
+ "directionalLightHelper",
108
+ "cameraHelper",
109
+ "boxHelper",
110
+ "box3Helper",
111
+ "planeHelper",
112
+ "arrowHelper",
113
+ "axesHelper",
114
+ "texture",
115
+ "videoTexture",
116
+ "dataTexture",
117
+ "dataTexture3D",
118
+ "compressedTexture",
119
+ "cubeTexture",
120
+ "canvasTexture",
121
+ "depthTexture",
122
+ "raycaster",
123
+ "vector2",
124
+ "vector3",
125
+ "vector4",
126
+ "euler",
127
+ "matrix3",
128
+ "matrix4",
129
+ "quaternion",
130
+ "bufferAttribute",
131
+ "float16BufferAttribute",
132
+ "float32BufferAttribute",
133
+ "float64BufferAttribute",
134
+ "int8BufferAttribute",
135
+ "int16BufferAttribute",
136
+ "int32BufferAttribute",
137
+ "uint8BufferAttribute",
138
+ "uint16BufferAttribute",
139
+ "uint32BufferAttribute",
140
+ "instancedBufferAttribute",
141
+ "color",
142
+ "fog",
143
+ "fogExp2",
144
+ "shape",
145
+ "colorShiftMaterial"
146
+ ]);
147
+ function shouldTagElement(elementName, threeDreiImportedElements, threeDreiNamespaces) {
148
+ if (threeFiberElems.has(elementName)) {
149
+ return false;
150
+ }
151
+ if (threeDreiImportedElements.has(elementName)) {
152
+ return false;
153
+ }
154
+ if (elementName.includes(".")) {
155
+ const namespace = elementName.split(".")[0] || "";
156
+ if (threeDreiNamespaces.has(namespace)) {
157
+ return false;
158
+ }
159
+ }
160
+ return true;
161
+ }
6
162
  function injectSourcePlugin({ types: t }) {
7
163
  return {
8
164
  name: "inject-source-location-enhanced",
9
165
  visitor: {
166
+ Program: {
167
+ enter(path, state) {
168
+ state.threeImportedElements = /* @__PURE__ */ new Set();
169
+ state.threeNamespaces = /* @__PURE__ */ new Set();
170
+ }
171
+ },
172
+ ImportDeclaration(path, state) {
173
+ const threeLibraries = [
174
+ "@react-three/fiber",
175
+ "@react-three/drei",
176
+ "@react-three/postprocessing",
177
+ "three"
178
+ ];
179
+ if (threeLibraries.includes(path.node.source.value)) {
180
+ path.node.specifiers.forEach((spec) => {
181
+ if (spec.type === "ImportSpecifier") {
182
+ state.threeImportedElements.add(spec.local.name);
183
+ } else if (spec.type === "ImportNamespaceSpecifier") {
184
+ state.threeNamespaces.add(spec.local.name);
185
+ }
186
+ });
187
+ }
188
+ },
10
189
  JSXElement(path, state) {
11
190
  const { node } = path;
12
191
  const filename = state.filename || state.file.opts.filename || "unknown";
@@ -16,6 +195,9 @@ function injectSourcePlugin({ types: t }) {
16
195
  const relativePath = filename.replace(/^.*\/src\//, "src/").replace(/\\/g, "/");
17
196
  const sourceLocation = `${relativePath}:${start.line}:${start.column + 1}`;
18
197
  const elementType = getElementType(node.openingElement.name);
198
+ if (!shouldTagElement(elementType, state.threeImportedElements || /* @__PURE__ */ new Set(), state.threeNamespaces || /* @__PURE__ */ new Set())) {
199
+ return;
200
+ }
19
201
  const elementInfo = {
20
202
  fileName: relativePath,
21
203
  lineNumber: start.line,
@@ -105,9 +287,10 @@ function getParentElementType(parentNode) {
105
287
  // src/index.ts
106
288
  function enterDevPlugin(options = {}) {
107
289
  const {
108
- injectMessageListener = true
290
+ injectMessageListener = true,
291
+ react: enableReact = true
109
292
  } = options;
110
- const reactPlugin = react({
293
+ const reactPlugin = enableReact ? react({
111
294
  // 确保在开发模式下启用JSX源码映射
112
295
  jsxImportSource: void 0,
113
296
  jsxRuntime: "automatic",
@@ -128,7 +311,7 @@ function enterDevPlugin(options = {}) {
128
311
  ]
129
312
  ]
130
313
  }
131
- });
314
+ }) : [];
132
315
  const htmlInjectPlugin = {
133
316
  name: "enter-dev-message-listener",
134
317
  enforce: "pre",
@@ -143,6 +326,21 @@ function enterDevPlugin(options = {}) {
143
326
  window.__earlyConsoleLogs__ = [];
144
327
 
145
328
  const earlyErrorHandler = function(event) {
329
+ const target = event.target;
330
+ if (target && (target.tagName === 'SCRIPT' || target.tagName === 'LINK')) {
331
+ const isStylesheet =
332
+ target.tagName === 'LINK' &&
333
+ String(target.rel || '').toLowerCase().split(/s+/).includes('stylesheet');
334
+ if (target.tagName === 'SCRIPT' || isStylesheet) {
335
+ window.__earlyErrors__.push({
336
+ type: 'resource',
337
+ resourceType: target.tagName === 'SCRIPT' ? 'script' : 'stylesheet',
338
+ url: target.src || target.href || '',
339
+ timestamp: Date.now()
340
+ });
341
+ return;
342
+ }
343
+ }
146
344
  window.__earlyErrors__.push({
147
345
  type: 'error',
148
346
  message: event.message,
@@ -251,12 +449,65 @@ function enterDevPlugin(options = {}) {
251
449
  return html;
252
450
  }
253
451
  };
254
- return [...reactPlugin, htmlInjectPlugin];
452
+ const sandboxServerPlugin = {
453
+ name: "enter-dev-sandbox-server",
454
+ config(_, { command }) {
455
+ if (command !== "serve") return;
456
+ const isSandbox = process.cwd().startsWith("/workspace");
457
+ if (!isSandbox) return;
458
+ const port = process.env.ENTER_DEV_PORT ? parseInt(process.env.ENTER_DEV_PORT, 10) : void 0;
459
+ const basePath = process.env.ENTER_BASE_PATH || void 0;
460
+ return {
461
+ ...basePath && { base: basePath },
462
+ server: {
463
+ ...port && { port },
464
+ allowedHosts: true,
465
+ watch: { usePolling: true, interval: 500 }
466
+ }
467
+ };
468
+ }
469
+ };
470
+ const routerBasePlugin = {
471
+ name: "enter-dev-router-base",
472
+ enforce: "pre",
473
+ transform(code, id) {
474
+ if (!process.env.ENTER_BASE_PATH) return;
475
+ if (!id.match(/\.[jt]sx?$/)) return;
476
+ if (!code.includes("createBrowserRouter")) return;
477
+ if (code.includes("basename")) return;
478
+ return code.replace(
479
+ /createBrowserRouter\(([^,)]+)\)/g,
480
+ "createBrowserRouter($1, { basename: import.meta.env.BASE_URL })"
481
+ );
482
+ }
483
+ };
484
+ const publicAssetReloadPlugin = {
485
+ name: "enter-dev-public-asset-reload",
486
+ apply: "serve",
487
+ configureServer(server) {
488
+ if (!process.cwd().startsWith("/workspace")) return;
489
+ const publicDir = server.config.publicDir;
490
+ const inPublicDir = (file) => file === publicDir || file.startsWith(`${publicDir}/`);
491
+ let timer;
492
+ const scheduleReload = () => {
493
+ clearTimeout(timer);
494
+ timer = setTimeout(() => {
495
+ server.ws.send({ type: "full-reload" });
496
+ }, 300);
497
+ };
498
+ for (const event of ["change", "add", "unlink"]) {
499
+ server.watcher.on(event, (file) => {
500
+ if (inPublicDir(file)) scheduleReload();
501
+ });
502
+ }
503
+ }
504
+ };
505
+ return [...reactPlugin, htmlInjectPlugin, sandboxServerPlugin, routerBasePlugin, publicAssetReloadPlugin];
255
506
  }
256
507
  function enterProdPlugin(options = {}) {
257
508
  const {
258
509
  injectGoogleFonts = true,
259
- googleFontsUrl = "https://fonts.googleapis.com/css2?family=Inter:wght@100;200;300;400;500;600;700;800;900;950&family=Poppins:wght@100;200;300;400;500;600;700;800;900;950&family=Roboto:wght@100;200;300;400;500;600;700;800;900;950&display=swap"
510
+ googleFontsUrl = "https://fonts.googleapis.com/css2?family=Bebas+Neue&family=Cormorant+Garamond:ital,wght@0,300..700;1,300..700&family=DM+Sans:ital,opsz,wght@0,9..40,100..1000;1,9..40,100..1000&family=Funnel+Display:wght@300..800&family=Instrument+Sans:ital,wght@0,400..700;1,400..700&family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&family=Lato:ital,wght@0,100;0,300;0,400;0,700;0,900;1,100;1,300;1,400;1,700;1,900&family=Lora:ital,wght@0,400..700;1,400..700&family=Manrope:wght@200..800&family=Merriweather:ital,opsz,wght@0,18..144,300..900;1,18..144,300..900&family=Montserrat:ital,wght@0,100..900;1,100..900&family=Nunito:ital,wght@0,200..1000;1,200..1000&family=Open+Sans:ital,wght@0,300..800;1,300..800&family=Oswald:wght@200..700&family=Playfair+Display:ital,wght@0,400..900;1,400..900&family=Poppins:ital,wght@0,100..900;1,100..900&family=Raleway:ital,wght@0,100..900;1,100..900&family=Roboto:ital,wght@0,100..900;1,100..900&family=Space+Grotesk:wght@300..700&family=Work+Sans:ital,wght@0,100..900;1,100..900&display=swap"
260
511
  } = options;
261
512
  const fontsInjectPlugin = {
262
513
  name: "enter-prod-fonts-inject",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-enter-dev",
3
- "version": "0.0.7",
3
+ "version": "0.0.10-alpha.0",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "description": "A Vite plugin for development enhancements",
@@ -16,6 +16,15 @@
16
16
  "files": [
17
17
  "dist"
18
18
  ],
19
+ "scripts": {
20
+ "build": "tsup",
21
+ "dev": "tsup --watch",
22
+ "lint": "eslint . --max-warnings 0",
23
+ "typecheck": "tsc --noEmit"
24
+ },
25
+ "publishConfig": {
26
+ "registry": "https://registry.npmjs.org/"
27
+ },
19
28
  "keywords": [
20
29
  "vite",
21
30
  "vite-plugin",
@@ -25,23 +34,19 @@
25
34
  ],
26
35
  "author": "",
27
36
  "license": "MIT",
37
+ "packageManager": "pnpm@10.15.0",
28
38
  "devDependencies": {
29
39
  "@types/node": "^20.19.9",
40
+ "@workspace/eslint-config": "workspace:*",
41
+ "@workspace/typescript-config": "workspace:*",
30
42
  "tsup": "^8.5.1",
31
43
  "typescript": "^5.9.2",
32
- "vite": "^6.0.0",
33
- "@workspace/typescript-config": "0.0.0",
34
- "@workspace/eslint-config": "0.0.0"
44
+ "vite": "^6.0.0"
35
45
  },
36
46
  "peerDependencies": {
37
- "vite": "^5.0.0 || ^6.0.0"
47
+ "vite": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
38
48
  },
39
49
  "dependencies": {
40
50
  "@vitejs/plugin-react": "^5.1.1"
41
- },
42
- "scripts": {
43
- "build": "tsup",
44
- "dev": "tsup --watch",
45
- "lint": "eslint . --max-warnings 0"
46
51
  }
47
- }
52
+ }