vite-plugin-enter-dev 0.0.1 → 0.0.3

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
@@ -1,10 +1,13 @@
1
- import { PluginOption } from 'vite';
1
+ import { Plugin } from 'vite';
2
2
 
3
- interface Options {
3
+ interface DevOptions {
4
4
  /**
5
5
  * 是否注入消息监听器脚本(默认:true)
6
6
  */
7
7
  injectMessageListener?: boolean;
8
+ }
9
+ declare function enterDevPlugin(options?: DevOptions): Plugin[];
10
+ interface ProdOptions {
8
11
  /**
9
12
  * 是否注入 Google Fonts(默认:true)
10
13
  */
@@ -14,6 +17,6 @@ interface Options {
14
17
  */
15
18
  googleFontsUrl?: string;
16
19
  }
17
- declare function enterDevPlugin(options?: Options): PluginOption[];
20
+ declare function enterProdPlugin(options?: ProdOptions): Plugin[];
18
21
 
19
- export { enterDevPlugin as default };
22
+ export { enterDevPlugin, enterProdPlugin };
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,7 @@ function getParentElementType(parentNode) {
105
287
  // src/index.ts
106
288
  function enterDevPlugin(options = {}) {
107
289
  const {
108
- injectMessageListener = true,
109
- injectGoogleFonts = true,
110
- 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"
290
+ injectMessageListener = true
111
291
  } = options;
112
292
  const reactPlugin = react({
113
293
  // 确保在开发模式下启用JSX源码映射
@@ -132,12 +312,13 @@ function enterDevPlugin(options = {}) {
132
312
  }
133
313
  });
134
314
  const htmlInjectPlugin = {
135
- name: "enter-dev-custom-html-inject",
315
+ name: "enter-dev-message-listener",
136
316
  enforce: "pre",
137
317
  transformIndexHtml(html) {
138
- let transformedHtml = html;
139
- if (injectMessageListener) {
140
- const messageListenerScript = `
318
+ if (!injectMessageListener) {
319
+ return html;
320
+ }
321
+ const messageListenerScript = `
141
322
  <script>
142
323
  // \u6DFB\u52A0\u6D88\u606F\u76D1\u542C\u5668\uFF0C\u63A5\u6536\u6765\u81EA\u4E3B\u5DE5\u7A0B\u7684\u6CE8\u5165\u811A\u672C
143
324
  window.addEventListener('message', (event) => {
@@ -157,26 +338,41 @@ function enterDevPlugin(options = {}) {
157
338
  });
158
339
 
159
340
  </script>`;
160
- if (html.includes("</head>")) {
161
- transformedHtml = transformedHtml.replace("</head>", `${messageListenerScript}
341
+ if (html.includes("</head>")) {
342
+ return html.replace("</head>", `${messageListenerScript}
162
343
  </head>`);
163
- } else if (html.includes("</title>")) {
164
- transformedHtml = transformedHtml.replace("</title>", `</title>${messageListenerScript}`);
165
- }
344
+ } else if (html.includes("</title>")) {
345
+ return html.replace("</title>", `</title>${messageListenerScript}`);
346
+ }
347
+ return html;
348
+ }
349
+ };
350
+ return [...reactPlugin, htmlInjectPlugin];
351
+ }
352
+ function enterProdPlugin(options = {}) {
353
+ const {
354
+ injectGoogleFonts = true,
355
+ 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"
356
+ } = options;
357
+ const fontsInjectPlugin = {
358
+ name: "enter-prod-fonts-inject",
359
+ enforce: "pre",
360
+ transformIndexHtml(html) {
361
+ if (!injectGoogleFonts) {
362
+ return html;
363
+ }
364
+ const googleFontsLink = `<link href="${googleFontsUrl}" rel="stylesheet">`;
365
+ if (html.includes(googleFontsUrl)) {
366
+ return html;
166
367
  }
167
- if (injectGoogleFonts) {
168
- const googleFontsLink = `<link href="${googleFontsUrl}" rel="stylesheet">`;
169
- if (!transformedHtml.includes(googleFontsUrl)) {
170
- if (transformedHtml.includes("</head>")) {
171
- transformedHtml = transformedHtml.replace("</head>", ` ${googleFontsLink}
368
+ if (html.includes("</head>")) {
369
+ return html.replace("</head>", ` ${googleFontsLink}
172
370
  </head>`);
173
- }
174
- }
175
371
  }
176
- return transformedHtml;
372
+ return html;
177
373
  }
178
374
  };
179
- return [...reactPlugin, htmlInjectPlugin];
375
+ return [fontsInjectPlugin];
180
376
  }
181
377
 
182
- export { enterDevPlugin as default };
378
+ export { enterDevPlugin, enterProdPlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-enter-dev",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "description": "A Vite plugin for development enhancements",
@@ -16,11 +16,6 @@
16
16
  "files": [
17
17
  "dist"
18
18
  ],
19
- "scripts": {
20
- "build": "tsup",
21
- "dev": "tsup --watch",
22
- "lint": "eslint . --max-warnings 0"
23
- },
24
19
  "keywords": [
25
20
  "vite",
26
21
  "vite-plugin",
@@ -30,19 +25,23 @@
30
25
  ],
31
26
  "author": "",
32
27
  "license": "MIT",
33
- "packageManager": "pnpm@10.15.0",
34
28
  "devDependencies": {
35
29
  "@types/node": "^20.19.9",
36
- "@workspace/eslint-config": "workspace:*",
37
- "@workspace/typescript-config": "workspace:*",
38
30
  "tsup": "^8.5.1",
39
31
  "typescript": "^5.9.2",
40
- "vite": "^6.0.0"
32
+ "vite": "^6.0.0",
33
+ "@workspace/typescript-config": "0.0.0",
34
+ "@workspace/eslint-config": "0.0.0"
41
35
  },
42
36
  "peerDependencies": {
43
37
  "vite": "^5.0.0 || ^6.0.0"
44
38
  },
45
39
  "dependencies": {
46
40
  "@vitejs/plugin-react": "^5.1.1"
41
+ },
42
+ "scripts": {
43
+ "build": "tsup",
44
+ "dev": "tsup --watch",
45
+ "lint": "eslint . --max-warnings 0"
47
46
  }
48
- }
47
+ }