unplugin-dingtalk 0.1.8 → 0.2.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/README.md CHANGED
@@ -7,7 +7,9 @@
7
7
  ## 功能
8
8
  - [x] 在钉钉内打开调试链接
9
9
  - [x] 可选注入 [vConsole](https://github.com/Tencent/vConsole) 用于调试
10
+ - [x] 可选注入 [chii](https://github.com/liriliri/chii) 用于调试
10
11
  - [x] 可选打开 [vue-devtools](https://github.com/vuejs/devtools) 用于调试
12
+ - [x] 支持调试 `HttpOnly` cookies
11
13
 
12
14
  ![images](https://github.com/zcf0508/unplugin-dingtalk/raw/main/images/Snipaste_2024-05-22_11-25-35.png)
13
15
 
@@ -25,6 +27,10 @@ interface Options {
25
27
  debug?: boolean
26
28
  /** @link https://github.com/vadxq/vite-plugin-vconsole#vitevconsole-options */
27
29
  vconsole?: viteVConsoleOptions
30
+ /** 需要调试的 cookies, 避免无法调试 HttpOnly 的 cookie */
31
+ debugCookies?: string[]
32
+ /** 默认值 `true` */
33
+ chii?: boolean
28
34
  vueDevtools?: {
29
35
  enable?: boolean
30
36
  host?: string
package/dist/astro.cjs CHANGED
@@ -40,27 +40,52 @@ var import_node_process = __toESM(require("process"), 1);
40
40
  var import_unplugin = require("unplugin");
41
41
  var import_picocolors = __toESM(require("picocolors"), 1);
42
42
  var import_vite_plugin_vconsole = require("vite-plugin-vconsole");
43
+ var import_cookie = __toESM(require("cookie"), 1);
44
+ var import_chii = require("chii");
45
+ var import_get_port_please = require("get-port-please");
43
46
  var config;
44
47
  var devtoolsInstance;
45
48
  var colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
49
+ var availablePort;
46
50
  var unpluginFactory = (options) => {
47
51
  var _a;
52
+ const {
53
+ chii: enableChii = true
54
+ } = options || {};
55
+ function debug(...args) {
56
+ if (options == null ? void 0 : options.debug) {
57
+ console.log(` ${import_picocolors.default.yellow("DEBUG")} `, ...args);
58
+ }
59
+ }
48
60
  const unpluginDing = {
49
61
  name: "unplugin-dingtalk",
50
62
  enforce: "pre",
51
63
  transformInclude(id) {
52
64
  return id.endsWith("main.ts") || id.endsWith("main.js");
53
65
  },
54
- transform(_source) {
66
+ async transform(_source) {
55
67
  var _a2, _b, _c;
68
+ if ((options == null ? void 0 : options.enable) && enableChii) {
69
+ availablePort = await (0, import_get_port_please.getRandomPort)();
70
+ debug(`chii server port: ${JSON.stringify({ availablePort })}`);
71
+ (0, import_chii.start)({
72
+ port: availablePort
73
+ });
74
+ }
56
75
  if ((options == null ? void 0 : options.enable) && ((_a2 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a2.enable)) {
57
- const code = `/* eslint-disable */;
58
- import { devtools } from '@vue/devtools'
59
- devtools.connect(${(_b = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _b.host}, ${(_c = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _c.port})
60
- /* eslint-enable */${_source};
61
- `;
76
+ const codes = [
77
+ `/* eslint-disable */;
78
+ import { devtools } from '@vue/devtools'
79
+ devtools.connect(${(_b = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _b.host}, ${(_c = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _c.port});`,
80
+ (options == null ? void 0 : options.enable) && enableChii ? `(() => {
81
+ const script = document.createElement('script');
82
+ script.src="http://localhost:${availablePort}/target.js";
83
+ document.body.appendChild(script);
84
+ })()` : "",
85
+ `/* eslint-enable */${_source};`
86
+ ];
62
87
  return {
63
- code,
88
+ code: codes.join("\n"),
64
89
  map: null
65
90
  // support source map
66
91
  };
@@ -75,16 +100,11 @@ var unpluginFactory = (options) => {
75
100
  configResolved(_config) {
76
101
  config = _config;
77
102
  },
78
- configureServer(server) {
103
+ async configureServer(server) {
79
104
  var _a2, _b;
80
105
  if (!(options == null ? void 0 : options.enable)) {
81
106
  return;
82
107
  }
83
- function debug(...args) {
84
- if (options == null ? void 0 : options.debug) {
85
- console.log(` ${import_picocolors.default.yellow("DEBUG")} `, ...args);
86
- }
87
- }
88
108
  const _printUrls = server.printUrls.bind(server);
89
109
  let source = `localhost:${config.server.port || 5173}`;
90
110
  const url = (_a2 = server.resolvedUrls) == null ? void 0 : _a2.local[0];
@@ -100,12 +120,48 @@ var unpluginFactory = (options) => {
100
120
  console.log(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold(
101
121
  `Open in dingtalk${((_a3 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a3.enable) ? " (with vue-devtools)" : ""}`
102
122
  )}: ${colorUrl(`http://${source}${base}open-dingtalk`)}`);
123
+ if (enableChii) {
124
+ console.log(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold(
125
+ "Click to open chrome devtools"
126
+ )}: ${colorUrl(`http://${source}${base}__chrome_devtools`)}`);
127
+ }
103
128
  };
104
129
  const targetURL = new URL(_targetUrl);
105
130
  targetURL.searchParams.append("ddtab", "true");
106
131
  if (options == null ? void 0 : options.corpId) {
107
132
  targetURL.searchParams.append("corpId", options.corpId);
108
133
  }
134
+ if (options.debugCookies && options.debugCookies.length > 0) {
135
+ server.middlewares.use((req, res, next) => {
136
+ const cookies = import_cookie.default.parse(req.headers.cookie || "");
137
+ for (const [name, value] of Object.entries(cookies)) {
138
+ if (options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
139
+ const serializedCookie = import_cookie.default.serialize(name, value, {
140
+ httpOnly: false
141
+ });
142
+ res.setHeader("Set-Cookie", serializedCookie);
143
+ }
144
+ }
145
+ next();
146
+ });
147
+ }
148
+ if (enableChii) {
149
+ server.middlewares.use("/__chrome_devtools", async (_req, res) => {
150
+ try {
151
+ const raw = await fetch(`http://localhost:${availablePort}/targets`);
152
+ const data = await raw.json();
153
+ if ((data == null ? void 0 : data.targets.length) > 0) {
154
+ const devToolsUrl = `http://localhost:${availablePort}/front_end/chii_app.html?ws=localhost:${availablePort}/client/${Math.random().toString(20).substring(2, 8)}?target=${data.targets[0].id}&rtc=false`;
155
+ res.writeHead(302, { Location: devToolsUrl });
156
+ res.end();
157
+ }
158
+ } catch (error) {
159
+ debug(`${error}`);
160
+ res.writeHead(502);
161
+ res.end();
162
+ }
163
+ });
164
+ }
109
165
  server.middlewares.use("/open-dingtalk", (req, res) => {
110
166
  var _a3;
111
167
  debug(targetURL.toString());
@@ -129,11 +185,12 @@ var unpluginFactory = (options) => {
129
185
  }
130
186
  }
131
187
  };
188
+ const plugins = [];
132
189
  if ((options == null ? void 0 : options.enable) && ((_a = options == null ? void 0 : options.vconsole) == null ? void 0 : _a.enabled)) {
133
- return [(0, import_vite_plugin_vconsole.viteVConsole)(options == null ? void 0 : options.vconsole), unpluginDing];
134
- } else {
135
- return unpluginDing;
190
+ plugins.push((0, import_vite_plugin_vconsole.viteVConsole)(options == null ? void 0 : options.vconsole));
136
191
  }
192
+ plugins.push(unpluginDing);
193
+ return plugins;
137
194
  };
138
195
  var unplugin = /* @__PURE__ */ (0, import_unplugin.createUnplugin)(unpluginFactory);
139
196
  var src_default = unplugin;
package/dist/astro.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  src_default
3
- } from "./chunk-YU6RRDIU.js";
3
+ } from "./chunk-GRAT7SP7.js";
4
4
 
5
5
  // src/astro.ts
6
6
  var astro_default = (options) => ({
@@ -4,27 +4,52 @@ import process from "process";
4
4
  import { createUnplugin } from "unplugin";
5
5
  import c from "picocolors";
6
6
  import { viteVConsole } from "vite-plugin-vconsole";
7
+ import cookie from "cookie";
8
+ import { start } from "chii";
9
+ import { getRandomPort } from "get-port-please";
7
10
  var config;
8
11
  var devtoolsInstance;
9
12
  var colorUrl = (url) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`));
13
+ var availablePort;
10
14
  var unpluginFactory = (options) => {
11
15
  var _a;
16
+ const {
17
+ chii: enableChii = true
18
+ } = options || {};
19
+ function debug(...args) {
20
+ if (options == null ? void 0 : options.debug) {
21
+ console.log(` ${c.yellow("DEBUG")} `, ...args);
22
+ }
23
+ }
12
24
  const unpluginDing = {
13
25
  name: "unplugin-dingtalk",
14
26
  enforce: "pre",
15
27
  transformInclude(id) {
16
28
  return id.endsWith("main.ts") || id.endsWith("main.js");
17
29
  },
18
- transform(_source) {
30
+ async transform(_source) {
19
31
  var _a2, _b, _c;
32
+ if ((options == null ? void 0 : options.enable) && enableChii) {
33
+ availablePort = await getRandomPort();
34
+ debug(`chii server port: ${JSON.stringify({ availablePort })}`);
35
+ start({
36
+ port: availablePort
37
+ });
38
+ }
20
39
  if ((options == null ? void 0 : options.enable) && ((_a2 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a2.enable)) {
21
- const code = `/* eslint-disable */;
22
- import { devtools } from '@vue/devtools'
23
- devtools.connect(${(_b = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _b.host}, ${(_c = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _c.port})
24
- /* eslint-enable */${_source};
25
- `;
40
+ const codes = [
41
+ `/* eslint-disable */;
42
+ import { devtools } from '@vue/devtools'
43
+ devtools.connect(${(_b = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _b.host}, ${(_c = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _c.port});`,
44
+ (options == null ? void 0 : options.enable) && enableChii ? `(() => {
45
+ const script = document.createElement('script');
46
+ script.src="http://localhost:${availablePort}/target.js";
47
+ document.body.appendChild(script);
48
+ })()` : "",
49
+ `/* eslint-enable */${_source};`
50
+ ];
26
51
  return {
27
- code,
52
+ code: codes.join("\n"),
28
53
  map: null
29
54
  // support source map
30
55
  };
@@ -39,16 +64,11 @@ var unpluginFactory = (options) => {
39
64
  configResolved(_config) {
40
65
  config = _config;
41
66
  },
42
- configureServer(server) {
67
+ async configureServer(server) {
43
68
  var _a2, _b;
44
69
  if (!(options == null ? void 0 : options.enable)) {
45
70
  return;
46
71
  }
47
- function debug(...args) {
48
- if (options == null ? void 0 : options.debug) {
49
- console.log(` ${c.yellow("DEBUG")} `, ...args);
50
- }
51
- }
52
72
  const _printUrls = server.printUrls.bind(server);
53
73
  let source = `localhost:${config.server.port || 5173}`;
54
74
  const url = (_a2 = server.resolvedUrls) == null ? void 0 : _a2.local[0];
@@ -64,12 +84,48 @@ var unpluginFactory = (options) => {
64
84
  console.log(` ${c.green("\u279C")} ${c.bold(
65
85
  `Open in dingtalk${((_a3 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a3.enable) ? " (with vue-devtools)" : ""}`
66
86
  )}: ${colorUrl(`http://${source}${base}open-dingtalk`)}`);
87
+ if (enableChii) {
88
+ console.log(` ${c.green("\u279C")} ${c.bold(
89
+ "Click to open chrome devtools"
90
+ )}: ${colorUrl(`http://${source}${base}__chrome_devtools`)}`);
91
+ }
67
92
  };
68
93
  const targetURL = new URL(_targetUrl);
69
94
  targetURL.searchParams.append("ddtab", "true");
70
95
  if (options == null ? void 0 : options.corpId) {
71
96
  targetURL.searchParams.append("corpId", options.corpId);
72
97
  }
98
+ if (options.debugCookies && options.debugCookies.length > 0) {
99
+ server.middlewares.use((req, res, next) => {
100
+ const cookies = cookie.parse(req.headers.cookie || "");
101
+ for (const [name, value] of Object.entries(cookies)) {
102
+ if (options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
103
+ const serializedCookie = cookie.serialize(name, value, {
104
+ httpOnly: false
105
+ });
106
+ res.setHeader("Set-Cookie", serializedCookie);
107
+ }
108
+ }
109
+ next();
110
+ });
111
+ }
112
+ if (enableChii) {
113
+ server.middlewares.use("/__chrome_devtools", async (_req, res) => {
114
+ try {
115
+ const raw = await fetch(`http://localhost:${availablePort}/targets`);
116
+ const data = await raw.json();
117
+ if ((data == null ? void 0 : data.targets.length) > 0) {
118
+ const devToolsUrl = `http://localhost:${availablePort}/front_end/chii_app.html?ws=localhost:${availablePort}/client/${Math.random().toString(20).substring(2, 8)}?target=${data.targets[0].id}&rtc=false`;
119
+ res.writeHead(302, { Location: devToolsUrl });
120
+ res.end();
121
+ }
122
+ } catch (error) {
123
+ debug(`${error}`);
124
+ res.writeHead(502);
125
+ res.end();
126
+ }
127
+ });
128
+ }
73
129
  server.middlewares.use("/open-dingtalk", (req, res) => {
74
130
  var _a3;
75
131
  debug(targetURL.toString());
@@ -93,11 +149,12 @@ var unpluginFactory = (options) => {
93
149
  }
94
150
  }
95
151
  };
152
+ const plugins = [];
96
153
  if ((options == null ? void 0 : options.enable) && ((_a = options == null ? void 0 : options.vconsole) == null ? void 0 : _a.enabled)) {
97
- return [viteVConsole(options == null ? void 0 : options.vconsole), unpluginDing];
98
- } else {
99
- return unpluginDing;
154
+ plugins.push(viteVConsole(options == null ? void 0 : options.vconsole));
100
155
  }
156
+ plugins.push(unpluginDing);
157
+ return plugins;
101
158
  };
102
159
  var unplugin = /* @__PURE__ */ createUnplugin(unpluginFactory);
103
160
  var src_default = unplugin;
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-YU6RRDIU.js";
3
+ } from "./chunk-GRAT7SP7.js";
4
4
 
5
5
  // src/webpack.ts
6
6
  import { createWebpackPlugin } from "unplugin";
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-YU6RRDIU.js";
3
+ } from "./chunk-GRAT7SP7.js";
4
4
 
5
5
  // src/vite.ts
6
6
  import { createVitePlugin } from "unplugin";
package/dist/esbuild.cjs CHANGED
@@ -41,27 +41,52 @@ var import_node_process = __toESM(require("process"), 1);
41
41
  var import_unplugin = require("unplugin");
42
42
  var import_picocolors = __toESM(require("picocolors"), 1);
43
43
  var import_vite_plugin_vconsole = require("vite-plugin-vconsole");
44
+ var import_cookie = __toESM(require("cookie"), 1);
45
+ var import_chii = require("chii");
46
+ var import_get_port_please = require("get-port-please");
44
47
  var config;
45
48
  var devtoolsInstance;
46
49
  var colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
50
+ var availablePort;
47
51
  var unpluginFactory = (options) => {
48
52
  var _a;
53
+ const {
54
+ chii: enableChii = true
55
+ } = options || {};
56
+ function debug(...args) {
57
+ if (options == null ? void 0 : options.debug) {
58
+ console.log(` ${import_picocolors.default.yellow("DEBUG")} `, ...args);
59
+ }
60
+ }
49
61
  const unpluginDing = {
50
62
  name: "unplugin-dingtalk",
51
63
  enforce: "pre",
52
64
  transformInclude(id) {
53
65
  return id.endsWith("main.ts") || id.endsWith("main.js");
54
66
  },
55
- transform(_source) {
67
+ async transform(_source) {
56
68
  var _a2, _b, _c;
69
+ if ((options == null ? void 0 : options.enable) && enableChii) {
70
+ availablePort = await (0, import_get_port_please.getRandomPort)();
71
+ debug(`chii server port: ${JSON.stringify({ availablePort })}`);
72
+ (0, import_chii.start)({
73
+ port: availablePort
74
+ });
75
+ }
57
76
  if ((options == null ? void 0 : options.enable) && ((_a2 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a2.enable)) {
58
- const code = `/* eslint-disable */;
59
- import { devtools } from '@vue/devtools'
60
- devtools.connect(${(_b = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _b.host}, ${(_c = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _c.port})
61
- /* eslint-enable */${_source};
62
- `;
77
+ const codes = [
78
+ `/* eslint-disable */;
79
+ import { devtools } from '@vue/devtools'
80
+ devtools.connect(${(_b = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _b.host}, ${(_c = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _c.port});`,
81
+ (options == null ? void 0 : options.enable) && enableChii ? `(() => {
82
+ const script = document.createElement('script');
83
+ script.src="http://localhost:${availablePort}/target.js";
84
+ document.body.appendChild(script);
85
+ })()` : "",
86
+ `/* eslint-enable */${_source};`
87
+ ];
63
88
  return {
64
- code,
89
+ code: codes.join("\n"),
65
90
  map: null
66
91
  // support source map
67
92
  };
@@ -76,16 +101,11 @@ var unpluginFactory = (options) => {
76
101
  configResolved(_config) {
77
102
  config = _config;
78
103
  },
79
- configureServer(server) {
104
+ async configureServer(server) {
80
105
  var _a2, _b;
81
106
  if (!(options == null ? void 0 : options.enable)) {
82
107
  return;
83
108
  }
84
- function debug(...args) {
85
- if (options == null ? void 0 : options.debug) {
86
- console.log(` ${import_picocolors.default.yellow("DEBUG")} `, ...args);
87
- }
88
- }
89
109
  const _printUrls = server.printUrls.bind(server);
90
110
  let source = `localhost:${config.server.port || 5173}`;
91
111
  const url = (_a2 = server.resolvedUrls) == null ? void 0 : _a2.local[0];
@@ -101,12 +121,48 @@ var unpluginFactory = (options) => {
101
121
  console.log(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold(
102
122
  `Open in dingtalk${((_a3 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a3.enable) ? " (with vue-devtools)" : ""}`
103
123
  )}: ${colorUrl(`http://${source}${base}open-dingtalk`)}`);
124
+ if (enableChii) {
125
+ console.log(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold(
126
+ "Click to open chrome devtools"
127
+ )}: ${colorUrl(`http://${source}${base}__chrome_devtools`)}`);
128
+ }
104
129
  };
105
130
  const targetURL = new URL(_targetUrl);
106
131
  targetURL.searchParams.append("ddtab", "true");
107
132
  if (options == null ? void 0 : options.corpId) {
108
133
  targetURL.searchParams.append("corpId", options.corpId);
109
134
  }
135
+ if (options.debugCookies && options.debugCookies.length > 0) {
136
+ server.middlewares.use((req, res, next) => {
137
+ const cookies = import_cookie.default.parse(req.headers.cookie || "");
138
+ for (const [name, value] of Object.entries(cookies)) {
139
+ if (options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
140
+ const serializedCookie = import_cookie.default.serialize(name, value, {
141
+ httpOnly: false
142
+ });
143
+ res.setHeader("Set-Cookie", serializedCookie);
144
+ }
145
+ }
146
+ next();
147
+ });
148
+ }
149
+ if (enableChii) {
150
+ server.middlewares.use("/__chrome_devtools", async (_req, res) => {
151
+ try {
152
+ const raw = await fetch(`http://localhost:${availablePort}/targets`);
153
+ const data = await raw.json();
154
+ if ((data == null ? void 0 : data.targets.length) > 0) {
155
+ const devToolsUrl = `http://localhost:${availablePort}/front_end/chii_app.html?ws=localhost:${availablePort}/client/${Math.random().toString(20).substring(2, 8)}?target=${data.targets[0].id}&rtc=false`;
156
+ res.writeHead(302, { Location: devToolsUrl });
157
+ res.end();
158
+ }
159
+ } catch (error) {
160
+ debug(`${error}`);
161
+ res.writeHead(502);
162
+ res.end();
163
+ }
164
+ });
165
+ }
110
166
  server.middlewares.use("/open-dingtalk", (req, res) => {
111
167
  var _a3;
112
168
  debug(targetURL.toString());
@@ -130,11 +186,12 @@ var unpluginFactory = (options) => {
130
186
  }
131
187
  }
132
188
  };
189
+ const plugins = [];
133
190
  if ((options == null ? void 0 : options.enable) && ((_a = options == null ? void 0 : options.vconsole) == null ? void 0 : _a.enabled)) {
134
- return [(0, import_vite_plugin_vconsole.viteVConsole)(options == null ? void 0 : options.vconsole), unpluginDing];
135
- } else {
136
- return unpluginDing;
191
+ plugins.push((0, import_vite_plugin_vconsole.viteVConsole)(options == null ? void 0 : options.vconsole));
137
192
  }
193
+ plugins.push(unpluginDing);
194
+ return plugins;
138
195
  };
139
196
 
140
197
  // src/esbuild.ts
package/dist/esbuild.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-YU6RRDIU.js";
3
+ } from "./chunk-GRAT7SP7.js";
4
4
 
5
5
  // src/esbuild.ts
6
6
  import { createEsbuildPlugin } from "unplugin";
package/dist/index.cjs CHANGED
@@ -40,27 +40,52 @@ var import_node_process = __toESM(require("process"), 1);
40
40
  var import_unplugin = require("unplugin");
41
41
  var import_picocolors = __toESM(require("picocolors"), 1);
42
42
  var import_vite_plugin_vconsole = require("vite-plugin-vconsole");
43
+ var import_cookie = __toESM(require("cookie"), 1);
44
+ var import_chii = require("chii");
45
+ var import_get_port_please = require("get-port-please");
43
46
  var config;
44
47
  var devtoolsInstance;
45
48
  var colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
49
+ var availablePort;
46
50
  var unpluginFactory = (options) => {
47
51
  var _a;
52
+ const {
53
+ chii: enableChii = true
54
+ } = options || {};
55
+ function debug(...args) {
56
+ if (options == null ? void 0 : options.debug) {
57
+ console.log(` ${import_picocolors.default.yellow("DEBUG")} `, ...args);
58
+ }
59
+ }
48
60
  const unpluginDing = {
49
61
  name: "unplugin-dingtalk",
50
62
  enforce: "pre",
51
63
  transformInclude(id) {
52
64
  return id.endsWith("main.ts") || id.endsWith("main.js");
53
65
  },
54
- transform(_source) {
66
+ async transform(_source) {
55
67
  var _a2, _b, _c;
68
+ if ((options == null ? void 0 : options.enable) && enableChii) {
69
+ availablePort = await (0, import_get_port_please.getRandomPort)();
70
+ debug(`chii server port: ${JSON.stringify({ availablePort })}`);
71
+ (0, import_chii.start)({
72
+ port: availablePort
73
+ });
74
+ }
56
75
  if ((options == null ? void 0 : options.enable) && ((_a2 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a2.enable)) {
57
- const code = `/* eslint-disable */;
58
- import { devtools } from '@vue/devtools'
59
- devtools.connect(${(_b = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _b.host}, ${(_c = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _c.port})
60
- /* eslint-enable */${_source};
61
- `;
76
+ const codes = [
77
+ `/* eslint-disable */;
78
+ import { devtools } from '@vue/devtools'
79
+ devtools.connect(${(_b = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _b.host}, ${(_c = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _c.port});`,
80
+ (options == null ? void 0 : options.enable) && enableChii ? `(() => {
81
+ const script = document.createElement('script');
82
+ script.src="http://localhost:${availablePort}/target.js";
83
+ document.body.appendChild(script);
84
+ })()` : "",
85
+ `/* eslint-enable */${_source};`
86
+ ];
62
87
  return {
63
- code,
88
+ code: codes.join("\n"),
64
89
  map: null
65
90
  // support source map
66
91
  };
@@ -75,16 +100,11 @@ var unpluginFactory = (options) => {
75
100
  configResolved(_config) {
76
101
  config = _config;
77
102
  },
78
- configureServer(server) {
103
+ async configureServer(server) {
79
104
  var _a2, _b;
80
105
  if (!(options == null ? void 0 : options.enable)) {
81
106
  return;
82
107
  }
83
- function debug(...args) {
84
- if (options == null ? void 0 : options.debug) {
85
- console.log(` ${import_picocolors.default.yellow("DEBUG")} `, ...args);
86
- }
87
- }
88
108
  const _printUrls = server.printUrls.bind(server);
89
109
  let source = `localhost:${config.server.port || 5173}`;
90
110
  const url = (_a2 = server.resolvedUrls) == null ? void 0 : _a2.local[0];
@@ -100,12 +120,48 @@ var unpluginFactory = (options) => {
100
120
  console.log(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold(
101
121
  `Open in dingtalk${((_a3 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a3.enable) ? " (with vue-devtools)" : ""}`
102
122
  )}: ${colorUrl(`http://${source}${base}open-dingtalk`)}`);
123
+ if (enableChii) {
124
+ console.log(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold(
125
+ "Click to open chrome devtools"
126
+ )}: ${colorUrl(`http://${source}${base}__chrome_devtools`)}`);
127
+ }
103
128
  };
104
129
  const targetURL = new URL(_targetUrl);
105
130
  targetURL.searchParams.append("ddtab", "true");
106
131
  if (options == null ? void 0 : options.corpId) {
107
132
  targetURL.searchParams.append("corpId", options.corpId);
108
133
  }
134
+ if (options.debugCookies && options.debugCookies.length > 0) {
135
+ server.middlewares.use((req, res, next) => {
136
+ const cookies = import_cookie.default.parse(req.headers.cookie || "");
137
+ for (const [name, value] of Object.entries(cookies)) {
138
+ if (options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
139
+ const serializedCookie = import_cookie.default.serialize(name, value, {
140
+ httpOnly: false
141
+ });
142
+ res.setHeader("Set-Cookie", serializedCookie);
143
+ }
144
+ }
145
+ next();
146
+ });
147
+ }
148
+ if (enableChii) {
149
+ server.middlewares.use("/__chrome_devtools", async (_req, res) => {
150
+ try {
151
+ const raw = await fetch(`http://localhost:${availablePort}/targets`);
152
+ const data = await raw.json();
153
+ if ((data == null ? void 0 : data.targets.length) > 0) {
154
+ const devToolsUrl = `http://localhost:${availablePort}/front_end/chii_app.html?ws=localhost:${availablePort}/client/${Math.random().toString(20).substring(2, 8)}?target=${data.targets[0].id}&rtc=false`;
155
+ res.writeHead(302, { Location: devToolsUrl });
156
+ res.end();
157
+ }
158
+ } catch (error) {
159
+ debug(`${error}`);
160
+ res.writeHead(502);
161
+ res.end();
162
+ }
163
+ });
164
+ }
109
165
  server.middlewares.use("/open-dingtalk", (req, res) => {
110
166
  var _a3;
111
167
  debug(targetURL.toString());
@@ -129,11 +185,12 @@ var unpluginFactory = (options) => {
129
185
  }
130
186
  }
131
187
  };
188
+ const plugins = [];
132
189
  if ((options == null ? void 0 : options.enable) && ((_a = options == null ? void 0 : options.vconsole) == null ? void 0 : _a.enabled)) {
133
- return [(0, import_vite_plugin_vconsole.viteVConsole)(options == null ? void 0 : options.vconsole), unpluginDing];
134
- } else {
135
- return unpluginDing;
190
+ plugins.push((0, import_vite_plugin_vconsole.viteVConsole)(options == null ? void 0 : options.vconsole));
136
191
  }
192
+ plugins.push(unpluginDing);
193
+ return plugins;
137
194
  };
138
195
  var unplugin = /* @__PURE__ */ (0, import_unplugin.createUnplugin)(unpluginFactory);
139
196
  var src_default = unplugin;
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  src_default,
3
3
  unplugin,
4
4
  unpluginFactory
5
- } from "./chunk-YU6RRDIU.js";
5
+ } from "./chunk-GRAT7SP7.js";
6
6
  export {
7
7
  src_default as default,
8
8
  unplugin,