@vixt/core 0.10.1 → 0.11.1

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,6 +11,9 @@ 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
19
  //#region src/node/env.ts
@@ -50,6 +53,18 @@ function loadWorkspaceEnv(mode, prefixes) {
50
53
  }
51
54
  //#endregion
52
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
+ */
53
68
  function defineVixtConfig(input) {
54
69
  return input;
55
70
  }
@@ -64,7 +79,6 @@ async function loadVixtConfig(opts) {
64
79
  config.buildDir ??= resolve(config.rootDir, ".vixt");
65
80
  config.buildTypesDir ??= resolve(config.buildDir, "types");
66
81
  config.buildLayersDir ??= resolve(config.buildDir, "layers");
67
- config.buildImportsDir ??= resolve(config.buildDir, "imports");
68
82
  config.workspaceDir ??= findUpWorkspaceDir();
69
83
  config.srcDir ??= resolve(config.rootDir, "src");
70
84
  config.modulesDir ??= resolve(config.srcDir, "modules");
@@ -123,17 +137,65 @@ function resolveLayersDirs(layers = []) {
123
137
  }
124
138
  return dirs;
125
139
  }
126
- const VixtClientAutoImports = { "@vixt/core/client": [
127
- "defineAppConfig",
128
- "defineVixtPlugin",
129
- "useAppConfig",
130
- "useVixtApp"
131
- ] };
132
140
  //#endregion
133
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
+ */
134
163
  function defineVitePlugin(pluginFn) {
135
164
  return pluginFn;
136
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
+ */
137
199
  function defineVixtModule(definition) {
138
200
  if (typeof definition == "function") return defineVixtModule({ setup: definition });
139
201
  const module = definition;
@@ -170,10 +232,10 @@ async function applyLayerModules({ config, layers = [] }) {
170
232
  }
171
233
  //#endregion
172
234
  //#region src/node/modules/alias.ts
173
- const name$8 = "vixt:alias";
235
+ const name$9 = "vixt:alias";
174
236
  var alias_default = defineVixtModule({
175
237
  meta: {
176
- name: name$8,
238
+ name: name$9,
177
239
  configKey: "alias"
178
240
  },
179
241
  defaults(vixt) {
@@ -190,7 +252,7 @@ var alias_default = defineVixtModule({
190
252
  },
191
253
  setup(options) {
192
254
  return {
193
- name: name$8,
255
+ name: name$9,
194
256
  enforce: "pre",
195
257
  config() {
196
258
  return { resolve: { alias: options } };
@@ -226,10 +288,10 @@ function isEmptyCode(code) {
226
288
  return false;
227
289
  }
228
290
  }
229
- const name$7 = "vixt:app";
291
+ const name$8 = "vixt:app";
230
292
  var app_default = defineVixtModule({
231
293
  meta: {
232
- name: name$7,
294
+ name: name$8,
233
295
  configKey: "app"
234
296
  },
235
297
  defaults: {
@@ -248,47 +310,47 @@ var app_default = defineVixtModule({
248
310
  const relativeEntryPath = `/${path.basename(srcDir)}/${entryFile}`;
249
311
  const absoluteEntryPath = path.resolve(srcDir, entryFile);
250
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
+ }
251
341
  const order = "pre";
252
342
  return {
253
- name: name$7,
343
+ name: name$8,
254
344
  enforce: order,
255
- transform: {
345
+ transform: transformEntryFile ? {
256
346
  order,
257
347
  filter: { id: absoluteEntryPath },
258
- handler(code) {
259
- if (!transformEntryFile) return;
260
- if (isEmptyCode(code)) return options.entryCode;
261
- }
262
- },
263
- transformIndexHtml: {
348
+ handler: transformEntryFileHandler
349
+ } : void 0,
350
+ transformIndexHtml: transformIndexHtml ? {
264
351
  order,
265
- handler(html = "") {
266
- if (!transformIndexHtml) return;
267
- const { rootTag, rootId, head } = options;
268
- const tags = Object.entries(head ?? {}).map(([tag, attrs]) => attrs.map((attr) => resolveHead(tag, attr))).flat();
269
- const loadingTemplate = resolveLoadingTemplate(options, vixt);
270
- return {
271
- html: `<!DOCTYPE html>\n${html}`,
272
- tags: [
273
- {
274
- tag: rootTag,
275
- attrs: { id: rootId },
276
- children: loadingTemplate,
277
- injectTo: "body"
278
- },
279
- {
280
- tag: "script",
281
- attrs: {
282
- type: "module",
283
- src: relativeEntryPath
284
- },
285
- injectTo: "body"
286
- },
287
- ...tags
288
- ]
289
- };
290
- }
291
- }
352
+ handler: transformIndexHtmlHandler
353
+ } : void 0
292
354
  };
293
355
  }
294
356
  });
@@ -311,10 +373,10 @@ var build_default = defineVixtModule({
311
373
  });
312
374
  //#endregion
313
375
  //#region src/node/modules/dev-server.ts
314
- const name$5 = "vixt:dev-server";
376
+ const name$6 = "vixt:dev-server";
315
377
  var dev_server_default = defineVixtModule({
316
378
  meta: {
317
- name: name$5,
379
+ name: name$6,
318
380
  configKey: "devServer"
319
381
  },
320
382
  defaults(vixt) {
@@ -347,7 +409,7 @@ var dev_server_default = defineVixtModule({
347
409
  timer = setTimeout(fn, 500);
348
410
  }
349
411
  return [{
350
- name: name$5,
412
+ name: name$6,
351
413
  enforce: "pre",
352
414
  config() {
353
415
  const { port, host, cors } = options;
@@ -366,6 +428,92 @@ var dev_server_default = defineVixtModule({
366
428
  }, sslOptions.enabled && Ssl(sslOptions)];
367
429
  }
368
430
  });
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
+ });
369
517
  //#endregion
370
518
  //#region src/node/modules/typescript.ts
371
519
  function generateTsConfig(options, vixt) {
@@ -485,6 +633,11 @@ var virtual_app_config_default = defineVixtModule({
485
633
  defuCall.$args.push(parseExpression(appConfigName));
486
634
  }
487
635
  }
636
+ try {
637
+ defuCall.$args.push(JSON.parse(JSON.stringify(vixt.options.appConfig ?? {})));
638
+ } catch (err) {
639
+ console.error(err);
640
+ }
488
641
  defuCall.$args.push({
489
642
  baseURL,
490
643
  rootId
@@ -589,8 +742,8 @@ var vite_default = defineVixtModule({
589
742
  //#endregion
590
743
  //#region src/node/modules/index.ts
591
744
  const virtualModuleIds = {
592
- css: virtual_css_default.getMeta().name,
593
745
  appConfig: virtual_app_config_default.getMeta().name,
746
+ css: virtual_css_default.getMeta().name,
594
747
  plugins: virtual_plugins_default.getMeta().name
595
748
  };
596
749
  const builtinModules = [
@@ -599,6 +752,8 @@ const builtinModules = [
599
752
  app_default,
600
753
  build_default,
601
754
  dev_server_default,
755
+ imports_default,
756
+ nitro_default,
602
757
  typescript_default,
603
758
  virtual_app_config_default,
604
759
  virtual_css_default,
@@ -612,6 +767,7 @@ async function loadVixt(opts) {
612
767
  cliOptions.force && fs.removeSync(result.config.buildDir);
613
768
  result.config.debug = !!cliOptions.debug;
614
769
  result.config.dev = env.NODE_ENV !== "production";
770
+ result.config.test = env.NODE_ENV === "test";
615
771
  result.layers = applyLayers(result);
616
772
  const layerModules = await applyLayerModules(result);
617
773
  return {
@@ -631,4 +787,4 @@ function createVixtPlugin(loadOptions) {
631
787
  });
632
788
  }
633
789
  //#endregion
634
- 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.10.1",
4
+ "version": "0.11.1",
5
5
  "author": "SoulLyoko<https://github.com/SoulLyoko>",
6
6
  "license": "MIT",
7
7
  "homepage": "https://soullyoko.github.io/vixt/",
@@ -30,21 +30,23 @@
30
30
  "dependencies": {
31
31
  "@types/fs-extra": "^11.0.4",
32
32
  "@vitejs/plugin-basic-ssl": "^2.3.0",
33
- "@vitejs/plugin-legacy": "^8.0.1",
33
+ "@vitejs/plugin-legacy": "^8.0.2",
34
34
  "@vue/tsconfig": "^0.9.1",
35
35
  "c12": "^3.3.4",
36
36
  "cac": "^7.0.0",
37
37
  "citty": "^0.2.2",
38
38
  "defu": "^6.1.7",
39
39
  "find-up": "^8.0.0",
40
- "fs-extra": "^11.3.4",
41
- "jiti": "^2.6.1",
42
- "magicast": "^0.5.2",
40
+ "fs-extra": "^11.3.5",
41
+ "jiti": "^2.7.0",
42
+ "magicast": "^0.5.3",
43
43
  "mlly": "^1.8.2",
44
+ "nitro": "^3.0.260522-beta",
44
45
  "pathe": "^2.0.3",
45
- "pkg-types": "^2.3.0",
46
- "vite": "^8.0.7",
47
- "vite-bundle-analyzer": "^1.3.7",
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
  }