@vixt/core 0.9.0 → 0.11.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.
@@ -11,9 +11,11 @@ import { pathToFileURL } from "mlly";
11
11
  import Legacy from "@vitejs/plugin-legacy";
12
12
  import Analyzer from "vite-bundle-analyzer";
13
13
  import Ssl from "@vitejs/plugin-basic-ssl";
14
+ import AutoImport from "unplugin-auto-import/vite";
15
+ import { writeTypes } from "nitro/builder";
16
+ import { nitro } from "nitro/vite";
14
17
  import Checker from "vite-plugin-checker";
15
18
  import { builders, parseExpression, parseModule } from "magicast";
16
-
17
19
  //#region src/node/env.ts
18
20
  function loadCLIOptions() {
19
21
  return cac().parse().options ?? {};
@@ -49,9 +51,20 @@ function loadWorkspaceEnv(mode, prefixes) {
49
51
  const workspaceDir = findUpWorkspaceDir();
50
52
  return workspaceDir ? loadEnv$1(mode, workspaceDir, prefixes) : {};
51
53
  }
52
-
53
54
  //#endregion
54
55
  //#region src/node/config.ts
56
+ /**
57
+ * Define the Vixt config.
58
+ * @example
59
+ * ```ts
60
+ * // vixt.config.ts
61
+ * import { defineVixtConfig } from 'vixt'
62
+ *
63
+ * export default defineVixtConfig({
64
+ * // Vixt配置
65
+ * })
66
+ * ```
67
+ */
55
68
  function defineVixtConfig(input) {
56
69
  return input;
57
70
  }
@@ -66,7 +79,6 @@ async function loadVixtConfig(opts) {
66
79
  config.buildDir ??= resolve(config.rootDir, ".vixt");
67
80
  config.buildTypesDir ??= resolve(config.buildDir, "types");
68
81
  config.buildLayersDir ??= resolve(config.buildDir, "layers");
69
- config.buildImportsDir ??= resolve(config.buildDir, "imports");
70
82
  config.workspaceDir ??= findUpWorkspaceDir();
71
83
  config.srcDir ??= resolve(config.rootDir, "src");
72
84
  config.modulesDir ??= resolve(config.srcDir, "modules");
@@ -125,18 +137,65 @@ function resolveLayersDirs(layers = []) {
125
137
  }
126
138
  return dirs;
127
139
  }
128
- const VixtClientAutoImports = { "@vixt/core/client": [
129
- "defineAppConfig",
130
- "defineVixtPlugin",
131
- "useAppConfig",
132
- "useVixtApp"
133
- ] };
134
-
135
140
  //#endregion
136
141
  //#region src/node/module.ts
142
+ /**
143
+ * A helper function for creating Vite plugins.
144
+ * @example
145
+ * ```ts
146
+ * import { defineVitePlugin } from 'vixt'
147
+ *
148
+ * interface PluginOptions {
149
+ * enabled?: boolean
150
+ * }
151
+ *
152
+ * export const myVitePlugin = defineVitePlugin<PluginOptions>((options) => {
153
+ * console.log(options)
154
+ * return {
155
+ * name: 'my-vite-plugin',
156
+ * configResolved(config) {
157
+ * console.log(config)
158
+ * }
159
+ * }
160
+ * })
161
+ * ```
162
+ */
137
163
  function defineVitePlugin(pluginFn) {
138
164
  return pluginFn;
139
165
  }
166
+ /**
167
+ * Define a Vixt module.
168
+ * @example
169
+ * ```ts
170
+ * // src/modules/my-module.ts
171
+ * import { defineVixtModule } from 'vixt'
172
+ *
173
+ * interface ModuleOptions {
174
+ * enabled?: boolean
175
+ * }
176
+ *
177
+ * declare module 'vixt' {
178
+ * interface VixtOptions {
179
+ * myModuleOptions?: ModuleOptions
180
+ * }
181
+ * }
182
+ *
183
+ * const name = 'my-module'
184
+ * export default defineVixtModule<ModuleOptions>({
185
+ * meta: { name },
186
+ * defaults: { enabled: true },
187
+ * setup(options, vixt) {
188
+ * console.log(options) // { enabled: true }
189
+ * return {
190
+ * name,
191
+ * configResolved(config) {
192
+ * console.log(config)
193
+ * }
194
+ * } // return one or more vite plugins
195
+ * }
196
+ * })
197
+ * ```
198
+ */
140
199
  function defineVixtModule(definition) {
141
200
  if (typeof definition == "function") return defineVixtModule({ setup: definition });
142
201
  const module = definition;
@@ -171,13 +230,12 @@ async function applyLayerModules({ config, layers = [] }) {
171
230
  }
172
231
  return modules;
173
232
  }
174
-
175
233
  //#endregion
176
234
  //#region src/node/modules/alias.ts
177
- const name$8 = "vixt:alias";
235
+ const name$9 = "vixt:alias";
178
236
  var alias_default = defineVixtModule({
179
237
  meta: {
180
- name: name$8,
238
+ name: name$9,
181
239
  configKey: "alias"
182
240
  },
183
241
  defaults(vixt) {
@@ -194,7 +252,7 @@ var alias_default = defineVixtModule({
194
252
  },
195
253
  setup(options) {
196
254
  return {
197
- name: name$8,
255
+ name: name$9,
198
256
  enforce: "pre",
199
257
  config() {
200
258
  return { resolve: { alias: options } };
@@ -202,7 +260,6 @@ var alias_default = defineVixtModule({
202
260
  };
203
261
  }
204
262
  });
205
-
206
263
  //#endregion
207
264
  //#region src/node/modules/app.ts
208
265
  function resolveHead(tag, attrs) {
@@ -231,10 +288,10 @@ function isEmptyCode(code) {
231
288
  return false;
232
289
  }
233
290
  }
234
- const name$7 = "vixt:app";
291
+ const name$8 = "vixt:app";
235
292
  var app_default = defineVixtModule({
236
293
  meta: {
237
- name: name$7,
294
+ name: name$8,
238
295
  configKey: "app"
239
296
  },
240
297
  defaults: {
@@ -253,57 +310,53 @@ var app_default = defineVixtModule({
253
310
  const relativeEntryPath = `/${path.basename(srcDir)}/${entryFile}`;
254
311
  const absoluteEntryPath = path.resolve(srcDir, entryFile);
255
312
  if (transformEntryFile && !fs.existsSync(absoluteEntryPath)) fs.outputFileSync(absoluteEntryPath, "");
313
+ function transformEntryFileHandler(code) {
314
+ if (isEmptyCode(code)) return options.entryCode;
315
+ }
316
+ function transformIndexHtmlHandler(html = "") {
317
+ const { rootTag, rootId, head } = options;
318
+ const tags = Object.entries(head ?? {}).map(([tag, attrs]) => attrs.map((attr) => resolveHead(tag, attr))).flat();
319
+ const loadingTemplate = resolveLoadingTemplate(options, vixt);
320
+ return {
321
+ html: `<!DOCTYPE html>\n${html}`,
322
+ tags: [
323
+ {
324
+ tag: rootTag,
325
+ attrs: { id: rootId },
326
+ children: loadingTemplate,
327
+ injectTo: "body"
328
+ },
329
+ {
330
+ tag: "script",
331
+ attrs: {
332
+ type: "module",
333
+ src: relativeEntryPath
334
+ },
335
+ injectTo: "body"
336
+ },
337
+ ...tags
338
+ ]
339
+ };
340
+ }
256
341
  const order = "pre";
257
342
  return {
258
- name: name$7,
343
+ name: name$8,
259
344
  enforce: order,
260
- transform: {
345
+ transform: transformEntryFile ? {
261
346
  order,
262
347
  filter: { id: absoluteEntryPath },
263
- handler(code) {
264
- if (!transformEntryFile) return;
265
- if (isEmptyCode(code)) return options.entryCode;
266
- }
267
- },
268
- transformIndexHtml: {
348
+ handler: transformEntryFileHandler
349
+ } : void 0,
350
+ transformIndexHtml: transformIndexHtml ? {
269
351
  order,
270
- handler(html = "") {
271
- if (!transformIndexHtml) return;
272
- const { rootTag, rootId, head } = options;
273
- const tags = Object.entries(head ?? {}).map(([tag, attrs]) => attrs.map((attr) => resolveHead(tag, attr))).flat();
274
- const loadingTemplate = resolveLoadingTemplate(options, vixt);
275
- return {
276
- html: `<!DOCTYPE html>\n${html}`,
277
- tags: [
278
- {
279
- tag: rootTag,
280
- attrs: { id: rootId },
281
- children: loadingTemplate,
282
- injectTo: "body"
283
- },
284
- {
285
- tag: "script",
286
- attrs: {
287
- type: "module",
288
- src: relativeEntryPath
289
- },
290
- injectTo: "body"
291
- },
292
- ...tags
293
- ]
294
- };
295
- }
296
- }
352
+ handler: transformIndexHtmlHandler
353
+ } : void 0
297
354
  };
298
355
  }
299
356
  });
300
-
301
- //#endregion
302
- //#region src/node/modules/build.ts
303
- const name$6 = "vixt:build";
304
357
  var build_default = defineVixtModule({
305
358
  meta: {
306
- name: name$6,
359
+ name: "vixt:build",
307
360
  configKey: "build"
308
361
  },
309
362
  setup(options) {
@@ -318,13 +371,12 @@ var build_default = defineVixtModule({
318
371
  return [analyzeOptions.enabled && Analyzer(analyzeOptions), legacyOptions.enabled && Legacy(legacyOptions)];
319
372
  }
320
373
  });
321
-
322
374
  //#endregion
323
375
  //#region src/node/modules/dev-server.ts
324
- const name$5 = "vixt:dev-server";
376
+ const name$6 = "vixt:dev-server";
325
377
  var dev_server_default = defineVixtModule({
326
378
  meta: {
327
- name: name$5,
379
+ name: name$6,
328
380
  configKey: "devServer"
329
381
  },
330
382
  defaults(vixt) {
@@ -357,7 +409,7 @@ var dev_server_default = defineVixtModule({
357
409
  timer = setTimeout(fn, 500);
358
410
  }
359
411
  return [{
360
- name: name$5,
412
+ name: name$6,
361
413
  enforce: "pre",
362
414
  config() {
363
415
  const { port, host, cors } = options;
@@ -376,7 +428,92 @@ var dev_server_default = defineVixtModule({
376
428
  }, sslOptions.enabled && Ssl(sslOptions)];
377
429
  }
378
430
  });
379
-
431
+ var imports_default = defineVixtModule({
432
+ meta: {
433
+ name: "vixt:imports",
434
+ configKey: "imports"
435
+ },
436
+ defaults(vixt) {
437
+ const { buildTypesDir } = vixt.options;
438
+ const { composables = [], constants = [], hooks = [], stores = [], utils = [] } = resolveLayersDirs([...vixt._layers].reverse());
439
+ return {
440
+ imports: [{ "@vixt/core/client": [
441
+ "defineAppConfig",
442
+ "defineVixtPlugin",
443
+ "useAppConfig",
444
+ "useVixtApp",
445
+ "$fetch"
446
+ ] }],
447
+ dts: `${buildTypesDir}/auto-imports.d.ts`,
448
+ dirs: [
449
+ composables,
450
+ constants,
451
+ hooks,
452
+ stores,
453
+ utils
454
+ ].flat()
455
+ };
456
+ },
457
+ setup(options) {
458
+ return AutoImport(options);
459
+ }
460
+ });
461
+ //#endregion
462
+ //#region src/node/modules/nitro.ts
463
+ let nitroCtx;
464
+ function setNitroCtx(ctx) {
465
+ nitroCtx = ctx;
466
+ }
467
+ function writeTypes$1() {
468
+ return nitroCtx && writeTypes(nitroCtx);
469
+ }
470
+ var nitro_default = defineVixtModule({
471
+ meta: {
472
+ name: "vixt:nitro",
473
+ configKey: "nitro"
474
+ },
475
+ defaults(vixt) {
476
+ return {
477
+ enabled: false,
478
+ scanDirs: vixt._layers.map((layer) => {
479
+ const { serverDir = "./server" } = layer.config?.nitro ?? {};
480
+ return path.join(layer.cwd, serverDir);
481
+ }),
482
+ serverDir: "./server",
483
+ typescript: { generatedTypesDir: vixt.options.buildTypesDir },
484
+ modules: [(nitro) => {
485
+ nitro.hooks.hook("build:before", setNitroCtx);
486
+ }]
487
+ };
488
+ },
489
+ setup(options, vixt) {
490
+ if (!options.enabled) return;
491
+ return [
492
+ nitro(options),
493
+ {
494
+ name: "vixt:nitro-types",
495
+ configureServer(server) {
496
+ writeTypes$1();
497
+ const routesDirs = vixt._layers.map((e) => `${e.cwd}/server/routes`);
498
+ const watcher = server.watcher;
499
+ watcher.add(routesDirs);
500
+ watcher.on("all", (_, p) => {
501
+ if (routesDirs.some((r) => path.normalize(p).includes(path.normalize(r)))) writeTypes$1();
502
+ });
503
+ }
504
+ },
505
+ {
506
+ name: "vixt:config-outdir-for-unocss",
507
+ config() {
508
+ let publicDir = ".output/public";
509
+ if (options.output?.publicDir) publicDir = options.output.publicDir;
510
+ else if (options.output?.dir) publicDir = path.join(options.output.dir, "public");
511
+ return { build: { outDir: publicDir } };
512
+ }
513
+ }
514
+ ];
515
+ }
516
+ });
380
517
  //#endregion
381
518
  //#region src/node/modules/typescript.ts
382
519
  function generateTsConfig(options, vixt) {
@@ -430,7 +567,7 @@ var typescript_default = defineVixtModule({
430
567
  configKey: "typescript"
431
568
  },
432
569
  defaults(vixt) {
433
- const { rootDir, alias } = vixt.options;
570
+ const { alias } = vixt.options;
434
571
  const paths = {};
435
572
  for (const [ak, av] of Object.entries(alias ?? {})) {
436
573
  const stats = fs.existsSync(av) ? fs.statSync(av) : null;
@@ -442,7 +579,6 @@ var typescript_default = defineVixtModule({
442
579
  tsConfig: {
443
580
  extends: "@vue/tsconfig/tsconfig.dom.json",
444
581
  compilerOptions: {
445
- baseUrl: rootDir,
446
582
  paths,
447
583
  types: ["vite/client"]
448
584
  },
@@ -466,7 +602,6 @@ var typescript_default = defineVixtModule({
466
602
  }, Checker(options.typeCheck ?? {})];
467
603
  }
468
604
  });
469
-
470
605
  //#endregion
471
606
  //#region src/node/modules/virtual-app-config.ts
472
607
  const name$3 = "virtual:vixt:app-config";
@@ -498,6 +633,11 @@ var virtual_app_config_default = defineVixtModule({
498
633
  defuCall.$args.push(parseExpression(appConfigName));
499
634
  }
500
635
  }
636
+ try {
637
+ defuCall.$args.push(JSON.parse(JSON.stringify(vixt.options.appConfig ?? {})));
638
+ } catch (err) {
639
+ console.error(err);
640
+ }
501
641
  defuCall.$args.push({
502
642
  baseURL,
503
643
  rootId
@@ -508,7 +648,6 @@ var virtual_app_config_default = defineVixtModule({
508
648
  };
509
649
  }
510
650
  });
511
-
512
651
  //#endregion
513
652
  //#region src/node/modules/virtual-css.ts
514
653
  const name$2 = "virtual:vixt:css";
@@ -528,7 +667,6 @@ var virtual_css_default = defineVixtModule({
528
667
  };
529
668
  }
530
669
  });
531
-
532
670
  //#endregion
533
671
  //#region src/node/modules/virtual-plugins.ts
534
672
  const name$1 = "virtual:vixt:plugins";
@@ -568,7 +706,6 @@ var virtual_plugins_default = defineVixtModule({
568
706
  };
569
707
  }
570
708
  });
571
-
572
709
  //#endregion
573
710
  //#region src/node/modules/vite.ts
574
711
  const name = "vixt:vite";
@@ -602,12 +739,11 @@ var vite_default = defineVixtModule({
602
739
  };
603
740
  }
604
741
  });
605
-
606
742
  //#endregion
607
743
  //#region src/node/modules/index.ts
608
744
  const virtualModuleIds = {
609
- css: virtual_css_default.getMeta().name,
610
745
  appConfig: virtual_app_config_default.getMeta().name,
746
+ css: virtual_css_default.getMeta().name,
611
747
  plugins: virtual_plugins_default.getMeta().name
612
748
  };
613
749
  const builtinModules = [
@@ -616,12 +752,13 @@ const builtinModules = [
616
752
  app_default,
617
753
  build_default,
618
754
  dev_server_default,
755
+ imports_default,
756
+ nitro_default,
619
757
  typescript_default,
620
758
  virtual_app_config_default,
621
759
  virtual_css_default,
622
760
  virtual_plugins_default
623
761
  ];
624
-
625
762
  //#endregion
626
763
  //#region src/node/vixt.ts
627
764
  async function loadVixt(opts) {
@@ -630,6 +767,7 @@ async function loadVixt(opts) {
630
767
  cliOptions.force && fs.removeSync(result.config.buildDir);
631
768
  result.config.debug = !!cliOptions.debug;
632
769
  result.config.dev = env.NODE_ENV !== "production";
770
+ result.config.test = env.NODE_ENV === "test";
633
771
  result.layers = applyLayers(result);
634
772
  const layerModules = await applyLayerModules(result);
635
773
  return {
@@ -648,6 +786,5 @@ function createVixtPlugin(loadOptions) {
648
786
  return vixt._modules.map((module) => installModule(module, {}, vixt));
649
787
  });
650
788
  }
651
-
652
789
  //#endregion
653
- export { VixtClientAutoImports, applyLayerModules, applyLayers, builtinModules, createVixtPlugin, defineVitePlugin, defineVixtConfig, defineVixtModule, findUpWorkspaceDir, installModule, isSamePath, loadCLIOptions, loadEnv, loadMode, loadVixt, loadVixtConfig, loadWorkspaceEnv, resolveLayersDirs, virtualModuleIds };
790
+ export { applyLayerModules, applyLayers, builtinModules, createVixtPlugin, defineVitePlugin, defineVixtConfig, defineVixtModule, findUpWorkspaceDir, installModule, isSamePath, loadCLIOptions, loadEnv, loadMode, loadVixt, loadVixtConfig, loadWorkspaceEnv, resolveLayersDirs, virtualModuleIds };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vixt/core",
3
3
  "type": "module",
4
- "version": "0.9.0",
4
+ "version": "0.11.0",
5
5
  "author": "SoulLyoko<https://github.com/SoulLyoko>",
6
6
  "license": "MIT",
7
7
  "homepage": "https://soullyoko.github.io/vixt/",
@@ -29,22 +29,24 @@
29
29
  ],
30
30
  "dependencies": {
31
31
  "@types/fs-extra": "^11.0.4",
32
- "@vitejs/plugin-basic-ssl": "^2.1.4",
33
- "@vitejs/plugin-legacy": "^8.0.0-beta.3",
34
- "@vue/tsconfig": "^0.8.1",
35
- "c12": "^3.3.3",
36
- "cac": "^6.7.14",
37
- "citty": "^0.2.1",
38
- "defu": "^6.1.4",
32
+ "@vitejs/plugin-basic-ssl": "^2.3.0",
33
+ "@vitejs/plugin-legacy": "^8.0.2",
34
+ "@vue/tsconfig": "^0.9.1",
35
+ "c12": "^3.3.4",
36
+ "cac": "^7.0.0",
37
+ "citty": "^0.2.2",
38
+ "defu": "^6.1.7",
39
39
  "find-up": "^8.0.0",
40
- "fs-extra": "^11.3.3",
41
- "jiti": "^2.6.1",
42
- "magicast": "^0.5.2",
43
- "mlly": "^1.8.0",
40
+ "fs-extra": "^11.3.5",
41
+ "jiti": "^2.7.0",
42
+ "magicast": "^0.5.3",
43
+ "mlly": "^1.8.2",
44
+ "nitro": "^3.0.260429-beta",
44
45
  "pathe": "^2.0.3",
45
- "pkg-types": "^2.3.0",
46
- "vite": "^7.3.1",
47
- "vite-bundle-analyzer": "^1.3.6",
48
- "vite-plugin-checker": "^0.12.0"
46
+ "pkg-types": "^2.3.1",
47
+ "unplugin-auto-import": "^21.0.0",
48
+ "vite": "^8.0.14",
49
+ "vite-bundle-analyzer": "^1.3.8",
50
+ "vite-plugin-checker": "^0.13.0"
49
51
  }
50
52
  }