create-jwn-js 1.2.2 → 1.2.3

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.
@@ -52,8 +52,7 @@ const createUrl = (url) => {
52
52
  return new URL(url);
53
53
  };
54
54
  const fileType = (file) => {
55
- var _a;
56
- const ext = ((_a = file.split(".").pop()) == null ? void 0 : _a.toLowerCase()) || "";
55
+ const ext = file.split(".").pop()?.toLowerCase() || "";
57
56
  if (ext === "js") {
58
57
  return "script";
59
58
  } else if (ext === "css") {
@@ -65,11 +64,11 @@ const fileType = (file) => {
65
64
  }
66
65
  return "";
67
66
  };
68
- const findDependencies = (modules, manifest, shouldPreload, shouldPrefetch) => {
67
+ const findDependencies = (modules, ssrManifest, shouldPreload, shouldPrefetch) => {
69
68
  const preload = /* @__PURE__ */ new Set();
70
69
  const prefetch = /* @__PURE__ */ new Set();
71
70
  for (const id of modules || []) {
72
- for (const file of manifest[id] || []) {
71
+ for (const file of ssrManifest[id] || []) {
73
72
  const asType = fileType(file);
74
73
  if (!shouldPreload && asType !== "script" && asType !== "style") {
75
74
  continue;
@@ -80,8 +79,8 @@ const findDependencies = (modules, manifest, shouldPreload, shouldPrefetch) => {
80
79
  preload.add(file);
81
80
  }
82
81
  }
83
- for (const id of Object.keys(manifest)) {
84
- for (const file of manifest[id]) {
82
+ for (const id of Object.keys(ssrManifest)) {
83
+ for (const file of ssrManifest[id]) {
85
84
  if (!preload.has(file)) {
86
85
  const asType = fileType(file);
87
86
  if (!shouldPrefetch) {
@@ -97,22 +96,21 @@ const findDependencies = (modules, manifest, shouldPreload, shouldPrefetch) => {
97
96
  return { preload: [...preload], prefetch: [...prefetch] };
98
97
  };
99
98
  const renderPreloadLinks = (files) => {
100
- var _a;
101
- const link = [];
99
+ const links = [];
102
100
  for (const file of files || []) {
103
101
  const asType = fileType(file);
104
- const ext = ((_a = file.split(".").pop()) == null ? void 0 : _a.toLowerCase()) || "";
102
+ const ext = file.split(".").pop()?.toLowerCase() || "";
105
103
  if (asType === "script") {
106
- link.push(`<link rel="modulepreload" crossorigin href="${file}">`);
104
+ links.push(`<link rel="modulepreload" crossorigin href="${file}">`);
107
105
  } else if (asType === "style") {
108
- link.push(`<link rel="stylesheet" href="${file}">`);
106
+ links.push(`<link rel="stylesheet" href="${file}">`);
109
107
  } else if (asType === "font") {
110
- link.push(`<link rel="stylesheet" href="${file}" type="font/${ext}" crossorigin>`);
108
+ links.push(`<link rel="stylesheet" href="${file}" type="font/${ext}" crossorigin>`);
111
109
  } else {
112
- link.push(`<link rel="stylesheet" href="${file}">`);
110
+ links.push(`<link rel="stylesheet" href="${file}">`);
113
111
  }
114
112
  }
115
- return link;
113
+ return links;
116
114
  };
117
115
  const renderPrefetchLinks = (files) => {
118
116
  const link = [];
@@ -121,6 +119,30 @@ const renderPrefetchLinks = (files) => {
121
119
  }
122
120
  return link;
123
121
  };
122
+ const findIndexHtmlDependencies = (manifest) => {
123
+ const output = [];
124
+ const indexHtmlDependencies = manifest["index.html"] || {};
125
+ indexHtmlDependencies.file && output.push(indexHtmlDependencies.file);
126
+ indexHtmlDependencies.css && indexHtmlDependencies.css.length && output.push(...indexHtmlDependencies.css);
127
+ output.forEach((file, index) => {
128
+ if (!/^\//.test(file)) {
129
+ output[index] = `/${file}`;
130
+ }
131
+ });
132
+ return output;
133
+ };
134
+ const renderPreloadLinksIndexHtml = (files) => {
135
+ const links = [];
136
+ for (const file of files || []) {
137
+ const asType = fileType(file);
138
+ if (asType === "script") {
139
+ links.push(`<link rel="modulepreload" crossorigin href="${file}">`);
140
+ } else if (asType === "style") {
141
+ links.push(`<link rel="preload" href="${file}" as="style">`);
142
+ }
143
+ }
144
+ return links;
145
+ };
124
146
  const teleportsInject = (body, teleports = {}) => {
125
147
  const teleportsKeys = Object.keys(teleports);
126
148
  if (teleportsKeys.length) {
@@ -140,7 +162,7 @@ const teleportsInject = (body, teleports = {}) => {
140
162
  return body;
141
163
  };
142
164
  const createViteSsrVue = (App2, options2 = {}) => {
143
- return async (url, { manifest, ...extra } = {}) => {
165
+ return async (url, { manifest, ssrManifest, ...extra } = {}) => {
144
166
  const app = createSSRApp(App2, options2.rootProps);
145
167
  const serializer = options2.serializer || serialize;
146
168
  const ssrContext = {
@@ -149,6 +171,7 @@ const createViteSsrVue = (App2, options2 = {}) => {
149
171
  initialState: {},
150
172
  ...extra
151
173
  };
174
+ ssrManifest = ssrManifest || manifest;
152
175
  const { head, router, store, inserts, context, pinia } = options2.created && await options2.created({
153
176
  app,
154
177
  ...ssrContext
@@ -170,15 +193,20 @@ const createViteSsrVue = (App2, options2 = {}) => {
170
193
  if (pinia) {
171
194
  ssrContext.initialState.pinia = pinia.state.value;
172
195
  }
173
- const body = (inserts == null ? void 0 : inserts.body) || await renderToString(app, Object.assign(ssrContext, context || {}));
174
- let headTags = (inserts == null ? void 0 : inserts.headTags) || "", htmlAttrs = (inserts == null ? void 0 : inserts.htmlAttrs) || "", bodyAttrs = (inserts == null ? void 0 : inserts.bodyAttrs) || "", dependencies = (inserts == null ? void 0 : inserts.dependencies) || [];
196
+ const body = inserts?.body || await renderToString(app, Object.assign(ssrContext, context || {}));
197
+ let headTags = inserts?.headTags || "", htmlAttrs = inserts?.htmlAttrs || "", bodyAttrs = inserts?.bodyAttrs || "", dependencies = inserts?.dependencies || [];
175
198
  if (head) {
176
199
  ({ headTags, htmlAttrs, bodyAttrs } = await renderHeadToString(head));
177
200
  }
178
- if (manifest) {
201
+ if (options2.preloadIndexHtml && manifest) {
202
+ const preloadIndexHtmlFiles = findIndexHtmlDependencies(manifest);
203
+ const links = renderPreloadLinksIndexHtml(preloadIndexHtmlFiles);
204
+ headTags += links.length ? "\n" + links.join("\n") : "";
205
+ }
206
+ if (ssrManifest) {
179
207
  const { preload, prefetch } = findDependencies(
180
208
  ssrContext.modules,
181
- manifest,
209
+ ssrManifest,
182
210
  options2.shouldPreload,
183
211
  options2.shouldPrefetch
184
212
  );
@@ -200,7 +228,7 @@ const createViteSsrVue = (App2, options2 = {}) => {
200
228
  ...ssrContext
201
229
  });
202
230
  const initialState = await serializer(ssrContext.initialState || {});
203
- const teleports = (ssrContext == null ? void 0 : ssrContext.teleports) || {};
231
+ const teleports = ssrContext?.teleports || {};
204
232
  return {
205
233
  html: teleportsInject(`<!DOCTYPE html>
206
234
  <html${htmlAttrs}>
@@ -215,7 +243,7 @@ const createViteSsrVue = (App2, options2 = {}) => {
215
243
  }
216
244
 
217
245
  /*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4Lmh0bWwlM0ZodG1sLXByb3h5JmlubGluZS1jc3MmaW5kZXg9MC5jc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtZQUNZO2dCQUNJLFdBQVc7WUFDZiIsImZpbGUiOiJpbmRleC5odG1sP2h0bWwtcHJveHkmaW5saW5lLWNzcyZpbmRleD0wLmNzcyIsInNvdXJjZXNDb250ZW50IjpbIlxuICAgICAgICAgICAgLmljb25pbmcge1xuICAgICAgICAgICAgICAgIHdpZHRoOiAyMHB4O1xuICAgICAgICAgICAgfVxuICAgICAgICAiXX0= */</style>
218
- <script type="module" crossorigin src="/assets/index-CrrygVvU.js"><\/script>
246
+ <script type="module" crossorigin src="/assets/index-CIJvTamH.js"><\/script>
219
247
  <link rel="stylesheet" crossorigin href="/assets/index-B40M1W6I.css">
220
248
  ${headTags}
221
249
  </head>
@@ -277,9 +305,8 @@ const useBaseStore = defineStore("baseStore", {
277
305
  return langLink(path2, this.lang);
278
306
  },
279
307
  async fetch(input, init = {}, opts = {}) {
280
- var _a, _b;
281
308
  const url = new URL(`${this.getBaseUrl}${input}`);
282
- const token = (init == null ? void 0 : init.token) || "";
309
+ const token = init?.token || "";
283
310
  const search = url.searchParams;
284
311
  search.set("lang", search.get("lang") || this.lang);
285
312
  const ssrHeaders = this.context.headers || {};
@@ -306,15 +333,15 @@ const useBaseStore = defineStore("baseStore", {
306
333
  init.headers = Object.assign(
307
334
  {},
308
335
  ssrHeaders,
309
- (init == null ? void 0 : init.body) instanceof FormData ? {} : { "content-type": "application/json" },
336
+ init?.body instanceof FormData ? {} : { "content-type": "application/json" },
310
337
  token ? { "authorization": "Bearer " + token } : {},
311
338
  init.headers
312
339
  );
313
340
  init.method = init.method || "GET";
314
341
  if (["get", "head"].includes(init.method.toLowerCase())) {
315
- url.search = new URLSearchParams((init == null ? void 0 : init.data) || url.search || {}).toString();
342
+ url.search = new URLSearchParams(init?.data || url.search || {}).toString();
316
343
  } else {
317
- init.body = (init == null ? void 0 : init.body) || JSON.stringify((init == null ? void 0 : init.data) || {});
344
+ init.body = init?.body || JSON.stringify(init?.data || {});
318
345
  }
319
346
  try {
320
347
  this.loading = true;
@@ -348,7 +375,7 @@ const useBaseStore = defineStore("baseStore", {
348
375
  }
349
376
  if (e instanceof ApiError) {
350
377
  const statusCode = e.getStatusCode();
351
- const error = ((_b = (_a = e.getData()) == null ? void 0 : _a.json) == null ? void 0 : _b.error) || e.getData().text || e.getMessage();
378
+ const error = e.getData()?.json?.error || e.getData().text || e.getMessage();
352
379
  if (init.method.toLowerCase() === "get") {
353
380
  if (statusCode === 410) {
354
381
  const location = e.getHeaders().location;
@@ -436,7 +463,6 @@ _sfc_main$2.setup = (props, ctx) => {
436
463
  const Error$1 = /* @__PURE__ */ _export_sfc(_sfc_main$2, [["ssrRender", _sfc_ssrRender$2], ["__file", "/Volumes/MacMiniAPFS/projects/packages/vite-node-template/template-vite-vue3-pinia-mts/src/frontend/views/Error/Error.vue"]]);
437
464
  const customerPrefetchPinia = (options2, fn) => {
438
465
  return async ({ store, isFetch }, to, from) => {
439
- var _a, _b;
440
466
  if (isFetch) {
441
467
  const baseStore = useBaseStore(store);
442
468
  const res = await baseStore.fetch("/web", {
@@ -448,10 +474,10 @@ const customerPrefetchPinia = (options2, fn) => {
448
474
  }
449
475
  });
450
476
  const body = await res.json();
451
- const page = (_a = body.data) == null ? void 0 : _a.page;
477
+ const page = body.data?.page;
452
478
  baseStore.page = page ? omit(page, ["way"]) : {};
453
- baseStore.way = (page == null ? void 0 : page.way) || [];
454
- baseStore.microdata = ((_b = body.data) == null ? void 0 : _b.microdata) || "";
479
+ baseStore.way = page?.way || [];
480
+ baseStore.microdata = body.data?.microdata || "";
455
481
  baseStore.mergeContext({ memcache: options2.memcache || null });
456
482
  }
457
483
  };
@@ -498,8 +524,7 @@ const createRouter = (opt) => createRouter$1({
498
524
  }
499
525
  ],
500
526
  async scrollBehavior(to, from, savedPosition) {
501
- var _a;
502
- if ((_a = to.query) == null ? void 0 : _a.el) {
527
+ if (to.query?.el) {
503
528
  const el = to.query.el;
504
529
  return { el };
505
530
  }
@@ -630,8 +655,7 @@ const webRoutes = [
630
655
  }
631
656
  ];
632
657
  const createMariadb = (config2) => {
633
- var _a;
634
- if ((_a = config2.db) == null ? void 0 : _a.home) {
658
+ if (config2.db?.home) {
635
659
  const home2 = new Query({ debug: process.env.NODE_ENV === "production" ? 0 : 1 });
636
660
  const homePool = home2.createPool(config2.db.home);
637
661
  return { homePool, home: home2 };
@@ -1,4 +1,4 @@
1
- import {Context, CreatorOptions} from "vite-ssr-vue";
1
+ import type {Context, CreatorOptions} from "vite-ssr-vue";
2
2
  import createRouter from "./routes/index.mjs";
3
3
  import {createHead} from "@vueuse/head";
4
4
  import {createPrefetch} from "@vuemod/prefetch";
@@ -6,7 +6,7 @@ import {amitt} from "amitt";
6
6
 
7
7
  // Multilang
8
8
  import Multilang from "@jwn-js/plugins/Multilang";
9
- import {parseLang} from "@/helpers/parseLang";
9
+ import {parseLang} from "@/helpers/parseLang.mjs";
10
10
 
11
11
  // Primevue
12
12
  import PrimeVue from 'primevue/config';
@@ -23,7 +23,7 @@ const defaultLang = "ru";
23
23
 
24
24
  export default {
25
25
  created({app, context, req, res, isClient}) {
26
- const emitter = amitt();
26
+ const emitter = amitt<"ga-pageview"|"ga-buttons"|"web-notification">();
27
27
  const head = createHead();
28
28
  const router = createRouter({isClient});
29
29
  const pinia = createPinia();
@@ -1,4 +1,4 @@
1
- export const parseLang = (url: string, defaultLang = "ru") => {
2
- const result = url.match(/^\/(ru|ua|en)\/?/i);
1
+ export const parseLang = (url: string, defaultLang = "ua") => {
2
+ const result = url.match(/^\/(ru|ua|en)(\/|$)/i);
3
3
  return result && result.length > 0 ? result[1] : defaultLang;
4
4
  }
@@ -9,7 +9,7 @@ declare global {
9
9
 
10
10
  const start = async(resolve: CallableFunction) => {
11
11
  try {
12
- await fetch(`${testConfig.apiUrl}'`);
12
+ await fetch(`${testConfig.apiUrl}`);
13
13
  resolve();
14
14
  } catch(e: any) {
15
15
  await new Promise(resolve => setTimeout(resolve, 100));
@@ -1,6 +1,6 @@
1
1
  import * as path from "path";
2
2
  import chalk from "chalk";
3
- import {defineConfig, ResolvedConfig} from "vite";
3
+ import {defineConfig, type ResolvedConfig} from "vite";
4
4
  import vue from "@vitejs/plugin-vue";
5
5
  import ssr from "vite-ssr-vue/plugin";
6
6
  import svgLoader from "vite-svg-loader";