@deot/dev-dever 2.6.2 → 2.8.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.cjs CHANGED
@@ -35,35 +35,37 @@ const path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
35
35
  const fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
36
36
 
37
37
  const dirname$1 = path__namespace.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))));
38
- let target = {
38
+ const target = {
39
39
  entries: [],
40
40
  html: ""
41
41
  };
42
42
  const excludes = ["tpl", "_", "dist", "node_modules", "__tests__"];
43
- const walk = (dir) => {
43
+ const walk = (dir, playDir) => {
44
44
  const { cwd, workspace } = devShared.Locals.impl();
45
- dir = dir || ".";
46
45
  const directory = path__namespace.join(cwd, workspace, dir);
47
46
  fs__namespace.readdirSync(directory).forEach((file) => {
48
47
  const fullpath = path__namespace.join(directory, file);
49
48
  const paths = fullpath.split("/") || [];
50
49
  const stat = fs__namespace.statSync(fullpath);
51
50
  const extname = path__namespace.extname(fullpath);
52
- if (stat.isFile() && /\.((t|j)sx?|vue)$/.test(extname) && paths.length >= 2 && paths[paths.length - 2] === "examples") {
51
+ if (stat.isFile() && /\.((t|j)sx?|vue|html)$/.test(extname) && paths.length >= 2 && playDir.includes(paths[paths.length - 2])) {
53
52
  const basename = path__namespace.basename(file, extname);
54
- let name = path__namespace.join(dir, basename).split("/");
55
- name.splice(name.length - 2, 1);
56
- target.entries.push(name.join("/"));
53
+ const name = path__namespace.join(dir, basename).split("/");
54
+ playDir.length === 1 && name.splice(name.length - 2, 1);
55
+ const name$ = name.join("/");
56
+ if (!target.entries.includes(name$)) {
57
+ target.entries.push(name$);
58
+ }
57
59
  } else if (stat.isDirectory() && !excludes.includes(file)) {
58
60
  const subdir = path__namespace.join(dir, file);
59
- walk(subdir);
61
+ walk(subdir, playDir);
60
62
  }
61
63
  });
62
64
  };
63
- const get = () => {
65
+ const get = (playDir) => {
64
66
  if (target.html)
65
67
  return target;
66
- walk();
68
+ walk(".", playDir.split(","));
67
69
  const tpl = fs__namespace.readFileSync(path__namespace.resolve(dirname$1, "../index.ejs"));
68
70
  target.html = ejs.render(tpl.toString(), {
69
71
  title: "demo",
@@ -98,7 +100,7 @@ const run = (options) => devShared.Utils.autoCatch(async () => {
98
100
  if (!options.workspace)
99
101
  delete options.workspace;
100
102
  delete options.packageName;
101
- let { entries, html } = get();
103
+ const { entries, html } = get(options.playDir);
102
104
  if (!entries.length)
103
105
  return devShared.Shell.spawn(`echo no entry file found!`);
104
106
  let options$ = {
@@ -136,10 +138,12 @@ const run = (options) => devShared.Utils.autoCatch(async () => {
136
138
  }
137
139
  process.env.DEV_OPTIONS = encodeURIComponent(JSON.stringify(devOptions));
138
140
  const server = await vite.createServer(options$);
139
- const $server = await server.listen();
140
- const { local = [], network = [] } = $server.resolvedUrls || {};
141
- const url = network[0] || local[0] || `http://localhost:${$server.config.server.port}`;
141
+ await server.listen();
142
+ const { local = [], network = [] } = server.resolvedUrls || {};
143
+ const url = network[0] || local[0] || `http://localhost:${server.config.server.port}`;
142
144
  entries.forEach((item) => devShared.Logger.log(` > ${item}: ${chalk.cyan(`${url}${item}.html`)}`));
145
+ server.printUrls();
146
+ server.bindCLIShortcuts({ print: true });
143
147
  });
144
148
 
145
149
  exports.run = run;
package/dist/index.js CHANGED
@@ -10,35 +10,37 @@ import * as fs from 'node:fs';
10
10
  import { render } from 'ejs';
11
11
 
12
12
  const dirname$1 = path.dirname(fileURLToPath(import.meta.url));
13
- let target = {
13
+ const target = {
14
14
  entries: [],
15
15
  html: ""
16
16
  };
17
17
  const excludes = ["tpl", "_", "dist", "node_modules", "__tests__"];
18
- const walk = (dir) => {
18
+ const walk = (dir, playDir) => {
19
19
  const { cwd, workspace } = Locals.impl();
20
- dir = dir || ".";
21
20
  const directory = path.join(cwd, workspace, dir);
22
21
  fs.readdirSync(directory).forEach((file) => {
23
22
  const fullpath = path.join(directory, file);
24
23
  const paths = fullpath.split("/") || [];
25
24
  const stat = fs.statSync(fullpath);
26
25
  const extname = path.extname(fullpath);
27
- if (stat.isFile() && /\.((t|j)sx?|vue)$/.test(extname) && paths.length >= 2 && paths[paths.length - 2] === "examples") {
26
+ if (stat.isFile() && /\.((t|j)sx?|vue|html)$/.test(extname) && paths.length >= 2 && playDir.includes(paths[paths.length - 2])) {
28
27
  const basename = path.basename(file, extname);
29
- let name = path.join(dir, basename).split("/");
30
- name.splice(name.length - 2, 1);
31
- target.entries.push(name.join("/"));
28
+ const name = path.join(dir, basename).split("/");
29
+ playDir.length === 1 && name.splice(name.length - 2, 1);
30
+ const name$ = name.join("/");
31
+ if (!target.entries.includes(name$)) {
32
+ target.entries.push(name$);
33
+ }
32
34
  } else if (stat.isDirectory() && !excludes.includes(file)) {
33
35
  const subdir = path.join(dir, file);
34
- walk(subdir);
36
+ walk(subdir, playDir);
35
37
  }
36
38
  });
37
39
  };
38
- const get = () => {
40
+ const get = (playDir) => {
39
41
  if (target.html)
40
42
  return target;
41
- walk();
43
+ walk(".", playDir.split(","));
42
44
  const tpl = fs.readFileSync(path.resolve(dirname$1, "../index.ejs"));
43
45
  target.html = render(tpl.toString(), {
44
46
  title: "demo",
@@ -73,7 +75,7 @@ const run = (options) => Utils.autoCatch(async () => {
73
75
  if (!options.workspace)
74
76
  delete options.workspace;
75
77
  delete options.packageName;
76
- let { entries, html } = get();
78
+ const { entries, html } = get(options.playDir);
77
79
  if (!entries.length)
78
80
  return Shell.spawn(`echo no entry file found!`);
79
81
  let options$ = {
@@ -111,10 +113,12 @@ const run = (options) => Utils.autoCatch(async () => {
111
113
  }
112
114
  process.env.DEV_OPTIONS = encodeURIComponent(JSON.stringify(devOptions));
113
115
  const server = await createServer(options$);
114
- const $server = await server.listen();
115
- const { local = [], network = [] } = $server.resolvedUrls || {};
116
- const url = network[0] || local[0] || `http://localhost:${$server.config.server.port}`;
116
+ await server.listen();
117
+ const { local = [], network = [] } = server.resolvedUrls || {};
118
+ const url = network[0] || local[0] || `http://localhost:${server.config.server.port}`;
117
119
  entries.forEach((item) => Logger.log(` > ${item}: ${chalk.cyan(`${url}${item}.html`)}`));
120
+ server.printUrls();
121
+ server.bindCLIShortcuts({ print: true });
118
122
  });
119
123
 
120
124
  export { run };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deot/dev-dever",
3
- "version": "2.6.2",
3
+ "version": "2.8.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -21,14 +21,14 @@
21
21
  "access": "public"
22
22
  },
23
23
  "dependencies": {
24
- "@deot/dev-react": "^2.6.2",
25
- "@deot/dev-shared": "^2.6.2",
26
- "@deot/dev-vue": "^2.6.2",
24
+ "@deot/dev-react": "^2.8.0",
25
+ "@deot/dev-shared": "^2.8.0",
26
+ "@deot/dev-vue": "^2.8.0",
27
27
  "ejs": "^3.1.9",
28
- "vite": "^4.5.0"
28
+ "vite": "^5.0.8"
29
29
  },
30
30
  "devDependencies": {
31
- "@deot/dev-test": "^2.6.2",
31
+ "@deot/dev-test": "^2.8.0",
32
32
  "cross-env": "^7.0.3"
33
33
  }
34
34
  }
package/shared.config.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as path from 'node:path';
2
2
  import * as fs from 'node:fs';
3
- import { createRequire } from "node:module";
3
+ import { createRequire } from 'node:module';
4
4
  import { defineConfig } from 'vitest/config';
5
5
  import type { UserConfig, ViteDevServer } from 'vite';
6
6
 
@@ -8,15 +8,15 @@ import type { UserConfig, ViteDevServer } from 'vite';
8
8
  * https://github.com/vuejs/core/issues/8303
9
9
  * to fix error: ReferenceError: __name is not defined
10
10
  */
11
- let __defProp = Object.defineProperty;
12
- let __name = (target: any, value: any) => __defProp(target, 'name', { value, configurable: true });
11
+ const __defProp = Object.defineProperty;
12
+ const __name = (target: any, value: any) => __defProp(target, 'name', { value, configurable: true });
13
13
  globalThis.__name = globalThis.__name || __name;
14
14
 
15
15
  const cwd = process.cwd();
16
16
 
17
17
  // devOptions
18
18
  const devOptions = JSON.parse(decodeURIComponent(process.env.DEV_OPTIONS || '{}'));
19
- const { workspace, html, subpackagesMap } = devOptions;
19
+ const { playDir, workspace, html, subpackagesMap } = devOptions;
20
20
 
21
21
  const generateIndexHtml = (url: string, inject: string) => {
22
22
  let contents = '';
@@ -45,42 +45,56 @@ const getVirtualHtml = async (url: string) => {
45
45
  const prefix = info.slice(0, -1);
46
46
  const entry = info.slice(-1)[0];
47
47
 
48
- if (prefix[prefix.length - 1] !== 'examples') {
49
- prefix.push('examples');
50
- }
51
-
52
48
  if (workspace && prefix[0] !== workspace) {
53
49
  prefix.unshift(workspace);
54
50
  }
55
51
 
56
- const dir = path.join(cwd, prefix.join('/'));
57
52
  const isExist = (ext: string) => {
58
- const fullpath = path.join(dir, `${entry.replace(/(.*)(\..*)$/, '$1') + ext}`);
59
- return fs.existsSync(fullpath) ? fullpath : false;
53
+ const getFullpath = (dir: string) => path.join(dir, `${entry.replace(/(.*)(\..*)$/, '$1') + ext}`);
54
+ const last = prefix[prefix.length - 1];
55
+ const dirs = playDir.split(',');
56
+
57
+ if (dirs.some((i: string) => i === last)) {
58
+ const fullpath = getFullpath(path.join(cwd, prefix.join('/')));
59
+ return fs.existsSync(fullpath) ? fullpath : false;
60
+ }
61
+
62
+ for (let i = 0; i <= dirs.length; i++) {
63
+ let suffix = dirs[i];
64
+ if (last === suffix) {
65
+ suffix = '';
66
+ }
67
+
68
+ const fullpath = getFullpath(path.join(cwd, prefix.concat([suffix]).join('/')));
69
+ if (fs.existsSync(fullpath)) {
70
+ return fullpath;
71
+ }
72
+ }
73
+
74
+ return false;
60
75
  };
61
76
 
62
77
  const getPreload = (fullpath: string) => {
63
- let dir$ = path.dirname(fullpath);
78
+ let dir = path.dirname(fullpath);
64
79
  let preload = '';
65
- while (dir$.includes(cwd) && !preload) {
66
-
67
- let preloadFullPath = [
68
- path.resolve(dir$, './z.dev.preload.ts'),
69
- path.resolve(dir$, './dev.preload.ts'),
70
- path.resolve(dir$, './z.preload.ts'),
71
- path.resolve(dir$, './preload.ts')
80
+ while (dir.includes(cwd) && !preload) {
81
+ const preloadFullPath = [
82
+ path.resolve(dir, './z.dev.preload.ts'),
83
+ path.resolve(dir, './dev.preload.ts'),
84
+ path.resolve(dir, './z.preload.ts'),
85
+ path.resolve(dir, './preload.ts')
72
86
  ].find(i => fs.existsSync(i));
73
87
 
74
88
  if (preloadFullPath) {
75
89
  preload = ` import "/${path.relative(cwd, preloadFullPath)}";\n`;
76
90
  } else {
77
- dir$ = path.resolve(dir$, '..');
91
+ dir = path.resolve(dir, '..');
78
92
  }
79
93
  }
80
94
 
81
95
  return preload;
82
96
  };
83
-
97
+
84
98
  const htmlFullpath = isExist('.html');
85
99
  if (htmlFullpath) {
86
100
  return fs.readFileSync(htmlFullpath).toString();
@@ -118,7 +132,6 @@ const getVirtualHtml = async (url: string) => {
118
132
  }
119
133
  };
120
134
 
121
-
122
135
  // alias
123
136
  const require$ = createRequire(cwd);
124
137
  const getPackageName = (name: string) => (require$(path.resolve(cwd, workspace ? `${workspace}/${name}` : '', 'package.json'))).name;
@@ -128,27 +141,27 @@ const name = getPackageName('index');
128
141
  export default defineConfig({
129
142
  resolve: workspace
130
143
  ? {
131
- alias: [
132
- {
133
- find: new RegExp(`^${name}$`),
134
- replacement: replacement('index')
135
- },
136
- ...Object.keys(subpackagesMap).reduce((pre, cur: string) => {
137
- if (subpackagesMap[cur].length) {
138
- pre.push({
139
- find: new RegExp(`^${getPackageName(cur)}$`),
140
- replacement: replacement(cur, true)
141
- });
144
+ alias: [
145
+ {
146
+ find: new RegExp(`^${name}$`),
147
+ replacement: replacement('index')
148
+ },
149
+ ...Object.keys(subpackagesMap).reduce((pre, cur: string) => {
150
+ if (subpackagesMap[cur].length) {
151
+ pre.push({
152
+ find: new RegExp(`^${getPackageName(cur)}$`),
153
+ replacement: replacement(cur, true)
154
+ });
155
+ }
156
+ return pre;
157
+ }, [] as any),
158
+ {
159
+
160
+ find: new RegExp(`^${name}-(.*?)$`),
161
+ replacement: replacement('$1')
142
162
  }
143
- return pre;
144
- }, [] as any),
145
- {
146
-
147
- find: new RegExp(`^${name}-(.*?)$`),
148
- replacement: replacement('$1')
149
- }
150
- ]
151
- }
163
+ ]
164
+ }
152
165
  : {},
153
166
  plugins: [
154
167
  {
@@ -156,13 +169,13 @@ export default defineConfig({
156
169
  configureServer(server: ViteDevServer) {
157
170
  server.middlewares.use(async (req, res, next) => {
158
171
  if (res.writableEnded) {
159
- return next();
172
+ return next();
160
173
  }
161
174
  if (req.url!.includes('html-proxy&')) {
162
175
  return next();
163
176
  }
164
177
 
165
- let url = req.url?.replace(/[?#].*$/s, '') || '';
178
+ const url = req.url?.replace(/[?#].*$/s, '') || '';
166
179
  if (url === '/') return res.end(html);
167
180
 
168
181
  // 文件已存在,这样xxx.png可以被获取,真实路径的.ts,.html都可以被获取
@@ -172,7 +185,7 @@ export default defineConfig({
172
185
 
173
186
  let vHtml = await getVirtualHtml(url);
174
187
  if (
175
- (url?.endsWith('.html') || vHtml)
188
+ (url?.endsWith('.html') || vHtml)
176
189
  && req.headers['sec-fetch-dest'] !== 'script'
177
190
  ) {
178
191
  if (!vHtml) {
@@ -191,4 +204,4 @@ export default defineConfig({
191
204
 
192
205
  // 因为virtualHtml不需要入口,这样可以不弹出Skipping dependency pre-bundling.
193
206
  optimizeDeps: { entries: [] }
194
- }) as UserConfig;
207
+ }) as UserConfig;