@jayfong/x-server 2.75.0 → 2.76.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.
@@ -137,6 +137,21 @@ class BuildUtil {
137
137
  loader: 'js'
138
138
  };
139
139
  });
140
+
141
+ // 支持构建时排除文件
142
+ if (options.excludeFiles?.length) {
143
+ const excludeRegExps = options.excludeFiles.map(item => new RegExp(item));
144
+ build.onLoad({
145
+ filter: /\.ts$/
146
+ }, async args => {
147
+ if (excludeRegExps.some(re => re.test(args.path))) {
148
+ return {
149
+ contents: 'export {}',
150
+ loader: 'js'
151
+ };
152
+ }
153
+ });
154
+ }
140
155
  }
141
156
  },
142
157
  // @ts-ignore
@@ -151,6 +151,11 @@ _yargs.default.command('dev', '开始开发', _ => _.positional('index', {
151
151
  describe: '构建完是否部署',
152
152
  type: 'boolean',
153
153
  default: false
154
+ }).positional('exclude', {
155
+ alias: 'x',
156
+ describe: '构建时排除的文件',
157
+ type: 'string',
158
+ array: true
154
159
  }), async argv => {
155
160
  await _env_util.EnvUtil.withChannel({
156
161
  cwd: process.cwd(),
@@ -179,7 +184,13 @@ _yargs.default.command('dev', '开始开发', _ => _.positional('index', {
179
184
  inlineEnvs: envMap,
180
185
  minify: argv.minify,
181
186
  noInstall: argv['no-install'],
182
- channel: channel
187
+ channel: channel,
188
+ excludeFiles: argv.exclude?.map(item => {
189
+ return (0, _vtils.escapeRegExp)(_vtils.StringTemplate.render(item, envMap, {
190
+ code: true,
191
+ onlyCode: true
192
+ }));
193
+ })
183
194
  });
184
195
  console.log('构建成功');
185
196
  if (argv.deploy) {
@@ -1,68 +1,68 @@
1
- import * as handlers from './handlers'
2
- import { Handler, XServer } from '@jayfong/x-server'
1
+ import { Handler, XServer } from '@jayfong/x-server';
2
+ import * as handlers from './handlers';
3
3
 
4
4
  const basePathWithHandlers: Array<[string, Record<string, Handler>]> = [
5
- // @index(['../../src/handlers/**/*.ts', '!**/_*', '!**/_*/**', '!**/*.test.*'], (f, _) => `['${(f.path+'/').replace('../../src/handlers/', '/').replace(/\/index\/$/, '/').split('/').map(v => _.snake(v)).join('/')}', handlers.__${_.pascal(f.path.replace('/src/handlers/', ''))}__ as any],`)
5
+ // @index(['../../src/handlers/**/*.ts', '!**/_*', '!**/_*/**', '!**/*.test.*'], (f, _) => `['${(f.path+'/').replace(/@dev/g, '').replace('../../src/handlers/', '/').replace(/\/index\/$/, '/').split('/').map(v => _.snake(v)).join('/')}', handlers.__${_.pascal(f.path.replace('/src/handlers/', ''))}__ as any],`)
6
6
  // @endindex
7
- ]
7
+ ];
8
8
 
9
9
  // https://stackoverflow.com/questions/50374908/transform-union-type-to-intersection-type
10
10
  type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
11
11
  k: infer I,
12
12
  ) => void
13
13
  ? I
14
- : never
14
+ : never;
15
15
  type RouteMap = {
16
16
  // @index(['../../src/handlers/**/*.ts', '!**/_*', '!**/_*/**', '!**/*.test.*'], (f, _) => `'${(f.path+'/').replace('../../src/handlers/', '/').replace(/\/index\/$/, '/').split('/').map(v => _.snake(v)).join('/')}': typeof handlers.__${_.pascal(f.path.replace('/src/handlers/', ''))}__,`)
17
17
  // @endindex
18
- }
18
+ };
19
19
  export type HandlerMap = UnionToIntersection<
20
20
  {
21
21
  [K in keyof RouteMap]: {
22
22
  [X in keyof RouteMap[K]]: {
23
- [Y in `${K}${X & string}`]: RouteMap[K][X]
24
- }
25
- }[keyof RouteMap[K]]
23
+ [Y in `${K}${X & string}`]: RouteMap[K][X];
24
+ };
25
+ }[keyof RouteMap[K]];
26
26
  }[keyof RouteMap]
27
- >
28
- export type HandlerPath = keyof HandlerMap
27
+ >;
28
+ export type HandlerPath = keyof HandlerMap;
29
29
  export type HandlerMetaMap = {
30
30
  [K in HandlerPath]: HandlerMap[K] extends Handler<infer X, infer Y, infer Z>
31
31
  ? {
32
- payload: X
33
- result: Y
34
- method: Z
32
+ payload: X;
33
+ result: Y;
34
+ method: Z;
35
35
  }
36
- : {}
37
- }
36
+ : {};
37
+ };
38
38
  export type HandlerPayloadMap = {
39
39
  // @ts-ignore
40
- [K in HandlerPath]: HandlerMetaMap[K]['payload']
41
- }
40
+ [K in HandlerPath]: HandlerMetaMap[K]['payload'];
41
+ };
42
42
  export type HandlerResultMap = {
43
43
  // @ts-ignore
44
- [K in HandlerPath]: HandlerMetaMap[K]['result']
45
- }
44
+ [K in HandlerPath]: HandlerMetaMap[K]['result'];
45
+ };
46
46
  export type HandlerMethodMap = {
47
47
  // @ts-ignore
48
- [K in HandlerPath]: HandlerMetaMap[K]['method']
49
- }
48
+ [K in HandlerPath]: HandlerMetaMap[K]['method'];
49
+ };
50
50
 
51
51
  export const routes: XServer.Route[] = basePathWithHandlers.reduce<
52
52
  XServer.Route[]
53
53
  >((res, item) => {
54
54
  const validHandlerNames = Object.keys(item[1]).filter(
55
55
  name => !name.startsWith('_') && name !== 'default',
56
- )
56
+ );
57
57
  for (const handlerName of validHandlerNames) {
58
- const path = item[1][handlerName].options.requestPath || handlerName
59
- const paths = Array.isArray(path) ? path : [path]
58
+ const path = item[1][handlerName].options.requestPath || handlerName;
59
+ const paths = Array.isArray(path) ? path : [path];
60
60
  for (const path of paths) {
61
61
  res.push({
62
62
  path: `${item[0]}${path}`,
63
63
  handler: item[1][handlerName],
64
- })
64
+ });
65
65
  }
66
66
  }
67
- return res
68
- }, [])
67
+ return res;
68
+ }, []);
@@ -5,6 +5,7 @@ export interface BuildOptions {
5
5
  minify?: boolean;
6
6
  noInstall?: boolean;
7
7
  channel?: string;
8
+ excludeFiles?: string[];
8
9
  }
9
10
  export declare class BuildUtil {
10
11
  static build(options: BuildOptions): Promise<void>;
@@ -131,6 +131,21 @@ export class BuildUtil {
131
131
  loader: 'js'
132
132
  };
133
133
  });
134
+
135
+ // 支持构建时排除文件
136
+ if (options.excludeFiles?.length) {
137
+ const excludeRegExps = options.excludeFiles.map(item => new RegExp(item));
138
+ build.onLoad({
139
+ filter: /\.ts$/
140
+ }, async args => {
141
+ if (excludeRegExps.some(re => re.test(args.path))) {
142
+ return {
143
+ contents: 'export {}',
144
+ loader: 'js'
145
+ };
146
+ }
147
+ });
148
+ }
134
149
  }
135
150
  },
136
151
  // @ts-ignore
package/lib/cli/cli.js CHANGED
@@ -4,7 +4,8 @@ import chokidar from 'chokidar';
4
4
  import execa from 'execa';
5
5
  import fs from 'fs-extra';
6
6
  import { generateManyIndex } from 'vscode-generate-index-standalone';
7
- import { castArray, debounce } from 'vtils';
7
+ import { castArray, debounce, escapeRegExp } from 'vtils';
8
+ import { StringTemplate } from 'vtils';
8
9
  import yargs from 'yargs';
9
10
  import { ApiGenerator } from "./api_generator";
10
11
  import { BuildUtil } from "./build_util";
@@ -148,6 +149,11 @@ yargs.command('dev', '开始开发', _ => _.positional('index', {
148
149
  describe: '构建完是否部署',
149
150
  type: 'boolean',
150
151
  default: false
152
+ }).positional('exclude', {
153
+ alias: 'x',
154
+ describe: '构建时排除的文件',
155
+ type: 'string',
156
+ array: true
151
157
  }), async argv => {
152
158
  await EnvUtil.withChannel({
153
159
  cwd: process.cwd(),
@@ -176,7 +182,13 @@ yargs.command('dev', '开始开发', _ => _.positional('index', {
176
182
  inlineEnvs: envMap,
177
183
  minify: argv.minify,
178
184
  noInstall: argv['no-install'],
179
- channel: channel
185
+ channel: channel,
186
+ excludeFiles: argv.exclude?.map(item => {
187
+ return escapeRegExp(StringTemplate.render(item, envMap, {
188
+ code: true,
189
+ onlyCode: true
190
+ }));
191
+ })
180
192
  });
181
193
  console.log('构建成功');
182
194
  if (argv.deploy) {
@@ -1,68 +1,68 @@
1
- import * as handlers from './handlers'
2
- import { Handler, XServer } from '@jayfong/x-server'
1
+ import { Handler, XServer } from '@jayfong/x-server';
2
+ import * as handlers from './handlers';
3
3
 
4
4
  const basePathWithHandlers: Array<[string, Record<string, Handler>]> = [
5
- // @index(['../../src/handlers/**/*.ts', '!**/_*', '!**/_*/**', '!**/*.test.*'], (f, _) => `['${(f.path+'/').replace('../../src/handlers/', '/').replace(/\/index\/$/, '/').split('/').map(v => _.snake(v)).join('/')}', handlers.__${_.pascal(f.path.replace('/src/handlers/', ''))}__ as any],`)
5
+ // @index(['../../src/handlers/**/*.ts', '!**/_*', '!**/_*/**', '!**/*.test.*'], (f, _) => `['${(f.path+'/').replace(/@dev/g, '').replace('../../src/handlers/', '/').replace(/\/index\/$/, '/').split('/').map(v => _.snake(v)).join('/')}', handlers.__${_.pascal(f.path.replace('/src/handlers/', ''))}__ as any],`)
6
6
  // @endindex
7
- ]
7
+ ];
8
8
 
9
9
  // https://stackoverflow.com/questions/50374908/transform-union-type-to-intersection-type
10
10
  type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
11
11
  k: infer I,
12
12
  ) => void
13
13
  ? I
14
- : never
14
+ : never;
15
15
  type RouteMap = {
16
16
  // @index(['../../src/handlers/**/*.ts', '!**/_*', '!**/_*/**', '!**/*.test.*'], (f, _) => `'${(f.path+'/').replace('../../src/handlers/', '/').replace(/\/index\/$/, '/').split('/').map(v => _.snake(v)).join('/')}': typeof handlers.__${_.pascal(f.path.replace('/src/handlers/', ''))}__,`)
17
17
  // @endindex
18
- }
18
+ };
19
19
  export type HandlerMap = UnionToIntersection<
20
20
  {
21
21
  [K in keyof RouteMap]: {
22
22
  [X in keyof RouteMap[K]]: {
23
- [Y in `${K}${X & string}`]: RouteMap[K][X]
24
- }
25
- }[keyof RouteMap[K]]
23
+ [Y in `${K}${X & string}`]: RouteMap[K][X];
24
+ };
25
+ }[keyof RouteMap[K]];
26
26
  }[keyof RouteMap]
27
- >
28
- export type HandlerPath = keyof HandlerMap
27
+ >;
28
+ export type HandlerPath = keyof HandlerMap;
29
29
  export type HandlerMetaMap = {
30
30
  [K in HandlerPath]: HandlerMap[K] extends Handler<infer X, infer Y, infer Z>
31
31
  ? {
32
- payload: X
33
- result: Y
34
- method: Z
32
+ payload: X;
33
+ result: Y;
34
+ method: Z;
35
35
  }
36
- : {}
37
- }
36
+ : {};
37
+ };
38
38
  export type HandlerPayloadMap = {
39
39
  // @ts-ignore
40
- [K in HandlerPath]: HandlerMetaMap[K]['payload']
41
- }
40
+ [K in HandlerPath]: HandlerMetaMap[K]['payload'];
41
+ };
42
42
  export type HandlerResultMap = {
43
43
  // @ts-ignore
44
- [K in HandlerPath]: HandlerMetaMap[K]['result']
45
- }
44
+ [K in HandlerPath]: HandlerMetaMap[K]['result'];
45
+ };
46
46
  export type HandlerMethodMap = {
47
47
  // @ts-ignore
48
- [K in HandlerPath]: HandlerMetaMap[K]['method']
49
- }
48
+ [K in HandlerPath]: HandlerMetaMap[K]['method'];
49
+ };
50
50
 
51
51
  export const routes: XServer.Route[] = basePathWithHandlers.reduce<
52
52
  XServer.Route[]
53
53
  >((res, item) => {
54
54
  const validHandlerNames = Object.keys(item[1]).filter(
55
55
  name => !name.startsWith('_') && name !== 'default',
56
- )
56
+ );
57
57
  for (const handlerName of validHandlerNames) {
58
- const path = item[1][handlerName].options.requestPath || handlerName
59
- const paths = Array.isArray(path) ? path : [path]
58
+ const path = item[1][handlerName].options.requestPath || handlerName;
59
+ const paths = Array.isArray(path) ? path : [path];
60
60
  for (const path of paths) {
61
61
  res.push({
62
62
  path: `${item[0]}${path}`,
63
63
  handler: item[1][handlerName],
64
- })
64
+ });
65
65
  }
66
66
  }
67
- return res
68
- }, [])
67
+ return res;
68
+ }, []);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.75.0",
3
+ "version": "2.76.0",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",