vite-plugin-enter-dev 0.0.1 → 0.0.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/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
@@ -105,9 +105,7 @@ function getParentElementType(parentNode) {
105
105
  // src/index.ts
106
106
  function enterDevPlugin(options = {}) {
107
107
  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"
108
+ injectMessageListener = true
111
109
  } = options;
112
110
  const reactPlugin = react({
113
111
  // 确保在开发模式下启用JSX源码映射
@@ -132,12 +130,13 @@ function enterDevPlugin(options = {}) {
132
130
  }
133
131
  });
134
132
  const htmlInjectPlugin = {
135
- name: "enter-dev-custom-html-inject",
133
+ name: "enter-dev-message-listener",
136
134
  enforce: "pre",
137
135
  transformIndexHtml(html) {
138
- let transformedHtml = html;
139
- if (injectMessageListener) {
140
- const messageListenerScript = `
136
+ if (!injectMessageListener) {
137
+ return html;
138
+ }
139
+ const messageListenerScript = `
141
140
  <script>
142
141
  // \u6DFB\u52A0\u6D88\u606F\u76D1\u542C\u5668\uFF0C\u63A5\u6536\u6765\u81EA\u4E3B\u5DE5\u7A0B\u7684\u6CE8\u5165\u811A\u672C
143
142
  window.addEventListener('message', (event) => {
@@ -157,26 +156,41 @@ function enterDevPlugin(options = {}) {
157
156
  });
158
157
 
159
158
  </script>`;
160
- if (html.includes("</head>")) {
161
- transformedHtml = transformedHtml.replace("</head>", `${messageListenerScript}
159
+ if (html.includes("</head>")) {
160
+ return html.replace("</head>", `${messageListenerScript}
162
161
  </head>`);
163
- } else if (html.includes("</title>")) {
164
- transformedHtml = transformedHtml.replace("</title>", `</title>${messageListenerScript}`);
165
- }
162
+ } else if (html.includes("</title>")) {
163
+ return html.replace("</title>", `</title>${messageListenerScript}`);
166
164
  }
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}
165
+ return html;
166
+ }
167
+ };
168
+ return [...reactPlugin, htmlInjectPlugin];
169
+ }
170
+ function enterProdPlugin(options = {}) {
171
+ const {
172
+ injectGoogleFonts = true,
173
+ 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"
174
+ } = options;
175
+ const fontsInjectPlugin = {
176
+ name: "enter-prod-fonts-inject",
177
+ enforce: "pre",
178
+ transformIndexHtml(html) {
179
+ if (!injectGoogleFonts) {
180
+ return html;
181
+ }
182
+ const googleFontsLink = `<link href="${googleFontsUrl}" rel="stylesheet">`;
183
+ if (html.includes(googleFontsUrl)) {
184
+ return html;
185
+ }
186
+ if (html.includes("</head>")) {
187
+ return html.replace("</head>", ` ${googleFontsLink}
172
188
  </head>`);
173
- }
174
- }
175
189
  }
176
- return transformedHtml;
190
+ return html;
177
191
  }
178
192
  };
179
- return [...reactPlugin, htmlInjectPlugin];
193
+ return [fontsInjectPlugin];
180
194
  }
181
195
 
182
- export { enterDevPlugin as default };
196
+ 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.2",
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/eslint-config": "0.0.0",
34
+ "@workspace/typescript-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
+ }