@adonisjs/vite 3.0.0-3 → 3.0.0-5

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.
@@ -17,7 +17,6 @@ var Vite = class {
17
17
  */
18
18
  #manifestCache;
19
19
  #options;
20
- #runtime;
21
20
  #devServer;
22
21
  /**
23
22
  * Reads the file contents as JSON
@@ -91,6 +90,12 @@ var Vite = class {
91
90
  children: []
92
91
  });
93
92
  }
93
+ /**
94
+ * Generate an asset URL for a given asset path
95
+ */
96
+ #generateAssetUrl(path) {
97
+ return `${this.#options.assetsUrl}/${path}`;
98
+ }
94
99
  /**
95
100
  * Generate a HTML tag for the given asset
96
101
  */
@@ -99,7 +104,7 @@ var Vite = class {
99
104
  if (this.isViteRunning) {
100
105
  url = `/${asset}`;
101
106
  } else {
102
- url = `${this.#options.assetsUrl}/${asset}`;
107
+ url = this.#generateAssetUrl(asset);
103
108
  }
104
109
  if (this.#isCssPath(asset)) {
105
110
  return this.#makeStyleTag(asset, url, attributes);
@@ -110,7 +115,7 @@ var Vite = class {
110
115
  * Generate style and script tags for the given entrypoints
111
116
  * Also adds the @vite/client script
112
117
  */
113
- #generateEntryPointsTagsForHotMode(entryPoints, attributes) {
118
+ #generateEntryPointsTagsForDevMode(entryPoints, attributes) {
114
119
  const viteHmr = this.#getViteHmrScript(attributes);
115
120
  const tags = entryPoints.map((entrypoint) => this.#generateTag(entrypoint, attributes));
116
121
  const result = viteHmr ? [viteHmr].concat(tags) : tags;
@@ -119,13 +124,26 @@ var Vite = class {
119
124
  /**
120
125
  * Get a chunk from the manifest file for a given file name
121
126
  */
122
- #chunk(manifest, fileName) {
123
- const chunk = manifest[fileName];
127
+ #chunk(manifest, entrypoint) {
128
+ const chunk = manifest[entrypoint];
124
129
  if (!chunk) {
125
- throw new Error(`Cannot find "${fileName}" chunk in the manifest file`);
130
+ throw new Error(`Cannot find "${entrypoint}" chunk in the manifest file`);
126
131
  }
127
132
  return chunk;
128
133
  }
134
+ /**
135
+ * Get a list of chunks for a given filename
136
+ */
137
+ #chunksByFile(manifest, file) {
138
+ return Object.entries(manifest).filter(([, chunk]) => chunk.file === file).map(([_, chunk]) => chunk);
139
+ }
140
+ /**
141
+ * Generate preload tag for a given url
142
+ */
143
+ #makePreloadTagForUrl(url) {
144
+ const attributes = this.#isCssPath(url) ? { rel: "preload", as: "style", href: url } : { rel: "modulepreload", href: url };
145
+ return this.#generateElement({ tag: "link", attributes });
146
+ }
129
147
  /**
130
148
  * Generate style and script tags for the given entrypoints
131
149
  * using the manifest file
@@ -133,17 +151,35 @@ var Vite = class {
133
151
  #generateEntryPointsTagsWithManifest(entryPoints, attributes) {
134
152
  const manifest = this.manifest();
135
153
  const tags = [];
154
+ const preloads = [];
136
155
  for (const entryPoint of entryPoints) {
137
156
  const chunk = this.#chunk(manifest, entryPoint);
157
+ preloads.push({ path: this.#generateAssetUrl(chunk.file) });
138
158
  tags.push({
139
159
  path: chunk.file,
140
160
  tag: this.#generateTag(chunk.file, { ...attributes, integrity: chunk.integrity })
141
161
  });
142
162
  for (const css of chunk.css || []) {
163
+ preloads.push({ path: this.#generateAssetUrl(css) });
143
164
  tags.push({ path: css, tag: this.#generateTag(css) });
144
165
  }
166
+ for (const importNode of chunk.imports || []) {
167
+ preloads.push({ path: this.#generateAssetUrl(manifest[importNode].file) });
168
+ for (const css of manifest[importNode].css || []) {
169
+ const subChunk = this.#chunksByFile(manifest, css);
170
+ preloads.push({ path: this.#generateAssetUrl(css) });
171
+ tags.push({
172
+ path: this.#generateAssetUrl(css),
173
+ tag: this.#generateTag(css, {
174
+ ...attributes,
175
+ integrity: subChunk[0]?.integrity
176
+ })
177
+ });
178
+ }
179
+ }
145
180
  }
146
- return uniqBy(tags, "path").sort((a) => a.path.endsWith(".css") ? -1 : 1).map((tag) => tag.tag);
181
+ const preloadsElements = uniqBy(preloads, "path").sort((preload) => this.#isCssPath(preload.path) ? -1 : 1).map((preload) => this.#makePreloadTagForUrl(preload.path));
182
+ return preloadsElements.concat(tags.map(({ tag }) => tag));
147
183
  }
148
184
  /**
149
185
  * Generate tags for the entry points
@@ -151,29 +187,25 @@ var Vite = class {
151
187
  generateEntryPointsTags(entryPoints, attributes) {
152
188
  entryPoints = Array.isArray(entryPoints) ? entryPoints : [entryPoints];
153
189
  if (this.isViteRunning) {
154
- return this.#generateEntryPointsTagsForHotMode(entryPoints, attributes);
190
+ return this.#generateEntryPointsTagsForDevMode(entryPoints, attributes);
155
191
  }
156
192
  return this.#generateEntryPointsTagsWithManifest(entryPoints, attributes);
157
193
  }
158
194
  /**
159
- * Returns the dev server URL when running in hot
160
- * mode, otherwise returns the explicitly configured
161
- * "assets" URL
195
+ * Returns the explicitly configured assetsUrl
162
196
  */
163
197
  assetsUrl() {
164
- if (this.isViteRunning)
165
- return this.#devServer.config.server.host;
166
198
  return this.#options.assetsUrl;
167
199
  }
168
200
  /**
169
- * Returns path to a given asset file
201
+ * Returns path to a given asset file using the manifest file
170
202
  */
171
203
  assetPath(asset) {
172
204
  if (this.isViteRunning) {
173
205
  return `/${asset}`;
174
206
  }
175
207
  const chunk = this.#chunk(this.manifest(), asset);
176
- return `${this.#options.assetsUrl}/${chunk.file}`;
208
+ return this.#generateAssetUrl(chunk.file);
177
209
  }
178
210
  /**
179
211
  * Returns the manifest file contents
@@ -196,12 +228,20 @@ var Vite = class {
196
228
  * since we don't need it
197
229
  */
198
230
  async createDevServer() {
199
- const { createViteRuntime, createServer } = await import("vite");
231
+ const { createServer } = await import("vite");
200
232
  this.#devServer = await createServer({
201
233
  server: { middlewareMode: true, hmr: { port: 3001 } },
202
234
  appType: "custom"
203
235
  });
204
- this.#runtime = await createViteRuntime(this.#devServer);
236
+ }
237
+ /**
238
+ * Create a runtime instance
239
+ * Will not be available when running in production since
240
+ * it needs the Vite Dev server
241
+ */
242
+ async createRuntime(options = {}) {
243
+ const { createViteRuntime } = await import("vite");
244
+ return createViteRuntime(this.#devServer, options);
205
245
  }
206
246
  /**
207
247
  * Stop the Vite Dev server
@@ -216,13 +256,6 @@ var Vite = class {
216
256
  getDevServer() {
217
257
  return this.#devServer;
218
258
  }
219
- /**
220
- * Get the Vite runtime instance
221
- * Will not be available when running in production
222
- */
223
- getRuntime() {
224
- return this.#runtime;
225
- }
226
259
  /**
227
260
  * Returns the script needed for the HMR working with React
228
261
  */
@@ -252,4 +285,4 @@ var Vite = class {
252
285
  export {
253
286
  Vite
254
287
  };
255
- //# sourceMappingURL=chunk-55PHIY4G.js.map
288
+ //# sourceMappingURL=chunk-DMQY6MHR.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/vite.ts"],"sourcesContent":["/*\n * @adonisjs/vite\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { readFileSync } from 'node:fs'\nimport type { ViteRuntime } from 'vite/runtime'\nimport type { MainThreadRuntimeOptions, Manifest, ViteDevServer } from 'vite'\n\nimport { makeAttributes, uniqBy } from './utils.js'\nimport type { AdonisViteElement, SetAttributes, ViteOptions } from './types.js'\n\n/**\n * Vite class exposes the APIs to generate tags and URLs for\n * assets processed using vite.\n */\nexport class Vite {\n /**\n * We cache the manifest file content in production\n * to avoid reading the file multiple times\n */\n #manifestCache?: Manifest\n #options: ViteOptions\n #devServer?: ViteDevServer\n\n constructor(\n protected isViteRunning: boolean,\n options: ViteOptions\n ) {\n this.#options = options\n this.#options.assetsUrl = (this.#options.assetsUrl || '/').replace(/\\/$/, '')\n }\n\n /**\n * Reads the file contents as JSON\n */\n #readFileAsJSON(filePath: string) {\n return JSON.parse(readFileSync(filePath, 'utf-8'))\n }\n\n /**\n * Generates a JSON element with a custom toString implementation\n */\n #generateElement(element: AdonisViteElement) {\n return {\n ...element,\n toString() {\n const attributes = `${makeAttributes(element.attributes)}`\n if (element.tag === 'link') {\n return `<${element.tag} ${attributes}/>`\n }\n\n return `<${element.tag} ${attributes}>${element.children.join('\\n')}</${element.tag}>`\n },\n }\n }\n\n /**\n * Returns the script needed for the HMR working with Vite\n */\n #getViteHmrScript(attributes?: Record<string, any>): AdonisViteElement | null {\n return this.#generateElement({\n tag: 'script',\n attributes: {\n type: 'module',\n src: '/@vite/client',\n ...attributes,\n },\n children: [],\n })\n }\n\n /**\n * Check if the given path is a CSS path\n */\n #isCssPath(path: string) {\n return path.match(/\\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/) !== null\n }\n\n /**\n * Unwrap attributes from the user defined function or return\n * the attributes as it is\n */\n #unwrapAttributes(src: string, url: string, attributes?: SetAttributes) {\n if (typeof attributes === 'function') {\n return attributes({ src, url })\n }\n\n return attributes\n }\n\n /**\n * Create a style tag for the given path\n */\n #makeStyleTag(src: string, url: string, attributes?: Record<string, any>): AdonisViteElement {\n const customAttributes = this.#unwrapAttributes(src, url, this.#options?.styleAttributes)\n return this.#generateElement({\n tag: 'link',\n attributes: { rel: 'stylesheet', ...customAttributes, ...attributes, href: url },\n })\n }\n\n /**\n * Create a script tag for the given path\n */\n #makeScriptTag(src: string, url: string, attributes?: Record<string, any>): AdonisViteElement {\n const customAttributes = this.#unwrapAttributes(src, url, this.#options?.scriptAttributes)\n return this.#generateElement({\n tag: 'script',\n attributes: { type: 'module', ...customAttributes, ...attributes, src: url },\n children: [],\n })\n }\n\n /**\n * Generate an asset URL for a given asset path\n */\n #generateAssetUrl(path: string): string {\n return `${this.#options.assetsUrl}/${path}`\n }\n\n /**\n * Generate a HTML tag for the given asset\n */\n #generateTag(asset: string, attributes?: Record<string, any>): AdonisViteElement {\n let url = ''\n if (this.isViteRunning) {\n url = `/${asset}`\n } else {\n url = this.#generateAssetUrl(asset)\n }\n\n if (this.#isCssPath(asset)) {\n return this.#makeStyleTag(asset, url, attributes)\n }\n\n return this.#makeScriptTag(asset, url, attributes)\n }\n\n /**\n * Generate style and script tags for the given entrypoints\n * Also adds the @vite/client script\n */\n #generateEntryPointsTagsForDevMode(\n entryPoints: string[],\n attributes?: Record<string, any>\n ): AdonisViteElement[] {\n const viteHmr = this.#getViteHmrScript(attributes)\n const tags = entryPoints.map((entrypoint) => this.#generateTag(entrypoint, attributes))\n\n const result = viteHmr ? [viteHmr].concat(tags) : tags\n\n return result\n }\n\n /**\n * Get a chunk from the manifest file for a given file name\n */\n #chunk(manifest: Manifest, entrypoint: string) {\n const chunk = manifest[entrypoint]\n\n if (!chunk) {\n throw new Error(`Cannot find \"${entrypoint}\" chunk in the manifest file`)\n }\n\n return chunk\n }\n\n /**\n * Get a list of chunks for a given filename\n */\n #chunksByFile(manifest: Manifest, file: string) {\n return Object.entries(manifest)\n .filter(([, chunk]) => chunk.file === file)\n .map(([_, chunk]) => chunk)\n }\n\n /**\n * Generate preload tag for a given url\n */\n #makePreloadTagForUrl(url: string) {\n const attributes = this.#isCssPath(url)\n ? { rel: 'preload', as: 'style', href: url }\n : { rel: 'modulepreload', href: url }\n\n return this.#generateElement({ tag: 'link', attributes })\n }\n\n /**\n * Generate style and script tags for the given entrypoints\n * using the manifest file\n */\n #generateEntryPointsTagsWithManifest(\n entryPoints: string[],\n attributes?: Record<string, any>\n ): AdonisViteElement[] {\n const manifest = this.manifest()\n const tags: { path: string; tag: AdonisViteElement }[] = []\n const preloads: Array<{ path: string }> = []\n\n for (const entryPoint of entryPoints) {\n /**\n * 1. We generate tags + modulepreload for the entrypoint\n */\n const chunk = this.#chunk(manifest, entryPoint)\n preloads.push({ path: this.#generateAssetUrl(chunk.file) })\n tags.push({\n path: chunk.file,\n tag: this.#generateTag(chunk.file, { ...attributes, integrity: chunk.integrity }),\n })\n\n /**\n * 2. We go through the CSS files that are imported by the entrypoint\n * and generate tags + preload for them\n */\n for (const css of chunk.css || []) {\n preloads.push({ path: this.#generateAssetUrl(css) })\n tags.push({ path: css, tag: this.#generateTag(css) })\n }\n\n /**\n * 3. We go through every import of the entrypoint and generate preload\n */\n for (const importNode of chunk.imports || []) {\n preloads.push({ path: this.#generateAssetUrl(manifest[importNode].file) })\n\n /**\n * 4. Finally, we generate tags + preload for the CSS files imported by the import\n * of the entrypoint\n */\n for (const css of manifest[importNode].css || []) {\n const subChunk = this.#chunksByFile(manifest, css)\n\n preloads.push({ path: this.#generateAssetUrl(css) })\n tags.push({\n path: this.#generateAssetUrl(css),\n tag: this.#generateTag(css, {\n ...attributes,\n integrity: subChunk[0]?.integrity,\n }),\n })\n }\n }\n }\n\n /**\n * We sort the preload to ensure that CSS files are preloaded first\n */\n const preloadsElements = uniqBy(preloads, 'path')\n .sort((preload) => (this.#isCssPath(preload.path) ? -1 : 1))\n .map((preload) => this.#makePreloadTagForUrl(preload.path))\n\n /**\n * And finally, we return the preloads + script and link tags\n */\n return preloadsElements.concat(tags.map(({ tag }) => tag))\n }\n\n /**\n * Generate tags for the entry points\n */\n generateEntryPointsTags(\n entryPoints: string[] | string,\n attributes?: Record<string, any>\n ): AdonisViteElement[] {\n entryPoints = Array.isArray(entryPoints) ? entryPoints : [entryPoints]\n\n if (this.isViteRunning) {\n return this.#generateEntryPointsTagsForDevMode(entryPoints, attributes)\n }\n\n return this.#generateEntryPointsTagsWithManifest(entryPoints, attributes)\n }\n\n /**\n * Returns the explicitly configured assetsUrl\n */\n assetsUrl() {\n return this.#options.assetsUrl\n }\n\n /**\n * Returns path to a given asset file using the manifest file\n */\n assetPath(asset: string): string {\n if (this.isViteRunning) {\n return `/${asset}`\n }\n\n const chunk = this.#chunk(this.manifest(), asset)\n return this.#generateAssetUrl(chunk.file)\n }\n\n /**\n * Returns the manifest file contents\n *\n * @throws Will throw an exception when running in dev\n */\n manifest(): Manifest {\n if (this.isViteRunning) {\n throw new Error('Cannot read the manifest file when running in dev mode')\n }\n\n if (!this.#manifestCache) {\n this.#manifestCache = this.#readFileAsJSON(this.#options.manifestFile)\n }\n\n return this.#manifestCache!\n }\n\n /**\n * Create the Vite Dev Server and runtime\n *\n * We lazy load the APIs to avoid loading it in production\n * since we don't need it\n */\n async createDevServer() {\n const { createServer } = await import('vite')\n\n this.#devServer = await createServer({\n server: { middlewareMode: true, hmr: { port: 3001 } },\n appType: 'custom',\n })\n }\n\n /**\n * Create a runtime instance\n * Will not be available when running in production since\n * it needs the Vite Dev server\n */\n async createRuntime(options: MainThreadRuntimeOptions = {}): Promise<ViteRuntime> {\n const { createViteRuntime } = await import('vite')\n\n return createViteRuntime(this.#devServer!, options)\n }\n\n /**\n * Stop the Vite Dev server\n */\n async stopDevServer() {\n await this.#devServer?.close()\n }\n\n /**\n * Get the Vite Dev server instance\n * Will not be available when running in production\n */\n getDevServer() {\n return this.#devServer\n }\n\n /**\n * Returns the script needed for the HMR working with React\n */\n getReactHmrScript(attributes?: Record<string, any>): AdonisViteElement | null {\n if (!this.isViteRunning) {\n return null\n }\n\n return this.#generateElement({\n tag: 'script',\n attributes: {\n type: 'module',\n ...attributes,\n },\n children: [\n '',\n `import RefreshRuntime from '/@react-refresh'`,\n `RefreshRuntime.injectIntoGlobalHook(window)`,\n `window.$RefreshReg$ = () => {}`,\n `window.$RefreshSig$ = () => (type) => type`,\n `window.__vite_plugin_react_preamble_installed__ = true`,\n '',\n ],\n })\n }\n}\n"],"mappings":";;;;;;AASA,SAAS,oBAAoB;AAWtB,IAAM,OAAN,MAAW;AAAA,EAShB,YACY,eACV,SACA;AAFU;AAGV,SAAK,WAAW;AAChB,SAAK,SAAS,aAAa,KAAK,SAAS,aAAa,KAAK,QAAQ,OAAO,EAAE;AAAA,EAC9E;AAAA;AAAA;AAAA;AAAA;AAAA,EAVA;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA,EAaA,gBAAgB,UAAkB;AAChC,WAAO,KAAK,MAAM,aAAa,UAAU,OAAO,CAAC;AAAA,EACnD;AAAA;AAAA;AAAA;AAAA,EAKA,iBAAiB,SAA4B;AAC3C,WAAO;AAAA,MACL,GAAG;AAAA,MACH,WAAW;AACT,cAAM,aAAa,GAAG,eAAe,QAAQ,UAAU,CAAC;AACxD,YAAI,QAAQ,QAAQ,QAAQ;AAC1B,iBAAO,IAAI,QAAQ,GAAG,IAAI,UAAU;AAAA,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AOA,MAAM,cAAc,UAAoC,CAAC,GAAyB;AAChF,UAAM,EAAE,kBAAkB,IAAI,MAAM,OAAO,MAAM;AAEjD,WAAO,kBAAkB,KAAK,YAAa,OAAO;AAAA,EACpD;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,gBAAgB;AACpB,UAAM,KAAK,YAAY,MAAM;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,eAAe;AACb,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,kBAAkB,YAA4D;AAC5E,QAAI,CAAC,KAAK,eAAe;AACvB,aAAO;AAAA,IACT;AAEA,WAAO,KAAK,iBAAiB;AAAA,MAC3B,KAAK;AAAA,MACL,YAAY;AAAA,QACV,MAAM;AAAA,QACN,GAAG;AAAA,MACL;AAAA,MACA,UAAU;AAAA,QACR;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AACF;","names":[]}
@@ -1,4 +1,4 @@
1
- // src/middlewares/vite_middleware.ts
1
+ // src/middleware/vite_middleware.ts
2
2
  var ViteMiddleware = class {
3
3
  constructor(vite) {
4
4
  this.vite = vite;
@@ -17,4 +17,4 @@ var ViteMiddleware = class {
17
17
  export {
18
18
  ViteMiddleware
19
19
  };
20
- //# sourceMappingURL=chunk-YCD5RGKQ.js.map
20
+ //# sourceMappingURL=chunk-MQRASPMO.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/middleware/vite_middleware.ts"],"sourcesContent":["/*\n * @adonisjs/vite\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type { ViteDevServer } from 'vite'\nimport type { HttpContext } from '@adonisjs/core/http'\nimport type { NextFn } from '@adonisjs/core/types/http'\n\nimport type { Vite } from '../vite.js'\n\n/**\n * Since Vite dev server is integrated within the AdonisJS process, this\n * middleware is used to proxy the requests to it.\n *\n * Some of the requests are directly handled by the Vite dev server,\n * like the one for the assets, while others are passed down to the\n * AdonisJS server.\n */\nexport default class ViteMiddleware {\n #devServer: ViteDevServer\n\n constructor(protected vite: Vite) {\n this.#devServer = this.vite.getDevServer()!\n }\n\n async handle({ request, response }: HttpContext, next: NextFn) {\n return await new Promise((resolve) => {\n this.#devServer.middlewares.handle(request.request, response.response, () => {\n return resolve(next())\n })\n })\n }\n}\n"],"mappings":";AAuBA,IAAqB,iBAArB,MAAoC;AAAA,EAGlC,YAAsB,MAAY;AAAZ;AACpB,SAAK,aAAa,KAAK,KAAK,aAAa;AAAA,EAC3C;AAAA,EAJA;AAAA,EAMA,MAAM,OAAO,EAAE,SAAS,SAAS,GAAgB,MAAc;AAC7D,WAAO,MAAM,IAAI,QAAQ,CAAC,YAAY;AACpC,WAAK,WAAW,YAAY,OAAO,QAAQ,SAAS,SAAS,UAAU,MAAM;AAC3E,eAAO,QAAQ,KAAK,CAAC;AAAA,MACvB,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF;","names":[]}
package/build/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Vite
3
- } from "./chunk-55PHIY4G.js";
3
+ } from "./chunk-DMQY6MHR.js";
4
4
  import "./chunk-CFRBPZ4N.js";
5
5
 
6
6
  // stubs/main.ts
@@ -13,6 +13,10 @@ export default class ViteProvider {
13
13
  * Registers edge plugin when edge is installed
14
14
  */
15
15
  protected registerEdgePlugin(): Promise<void>;
16
+ /**
17
+ * Registers CSP keywords when @adonisjs/shield is installed
18
+ */
19
+ protected registerShieldKeywords(): Promise<void>;
16
20
  /**
17
21
  * Register Vite bindings
18
22
  */
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  Vite
3
- } from "../chunk-55PHIY4G.js";
3
+ } from "../chunk-DMQY6MHR.js";
4
4
  import "../chunk-CFRBPZ4N.js";
5
5
  import {
6
6
  ViteMiddleware
7
- } from "../chunk-YCD5RGKQ.js";
7
+ } from "../chunk-MQRASPMO.js";
8
8
 
9
9
  // providers/vite_provider.ts
10
10
  var ViteProvider = class {
@@ -25,6 +25,27 @@ var ViteProvider = class {
25
25
  edge.default.use(edgePluginVite(vite));
26
26
  }
27
27
  }
28
+ /**
29
+ * Registers CSP keywords when @adonisjs/shield is installed
30
+ */
31
+ async registerShieldKeywords() {
32
+ let cspKeywords = null;
33
+ try {
34
+ const shieldExports = await import("@adonisjs/shield");
35
+ cspKeywords = shieldExports.cspKeywords;
36
+ } catch {
37
+ }
38
+ if (!cspKeywords)
39
+ return;
40
+ const vite = await this.app.container.make("vite");
41
+ cspKeywords.register("@viteUrl", function() {
42
+ const assetsURL = vite.assetsUrl();
43
+ if (!assetsURL || !assetsURL.startsWith("http://") || assetsURL.startsWith("https://")) {
44
+ return "";
45
+ }
46
+ return assetsURL;
47
+ });
48
+ }
28
49
  /**
29
50
  * Register Vite bindings
30
51
  */
@@ -45,7 +66,7 @@ var ViteProvider = class {
45
66
  const vite = await this.app.container.make("vite");
46
67
  const server = await this.app.container.make("server");
47
68
  await vite.createDevServer();
48
- server.use([() => import("../vite_middleware-HYLIJP2B.js")]);
69
+ server.use([() => import("../vite_middleware-AQXJUYMB.js")]);
49
70
  }
50
71
  /**
51
72
  * Stop Vite server when running in development or test
@@ -1 +1 @@
1
- {"version":3,"sources":["../../providers/vite_provider.ts"],"sourcesContent":["/*\n * @adonisjs/vite\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type { ApplicationService } from '@adonisjs/core/types'\n\nimport { Vite } from '../src/vite.js'\nimport type { ViteOptions } from '../src/types.js'\nimport ViteMiddleware from '../src/middlewares/vite_middleware.js'\n\ndeclare module '@adonisjs/core/types' {\n interface ContainerBindings {\n vite: Vite\n }\n}\n\nexport default class ViteProvider {\n #shouldRunVite: boolean\n\n constructor(protected app: ApplicationService) {\n /**\n * We should only run Vite in development and test environments\n */\n const env = this.app.getEnvironment()\n this.#shouldRunVite = (this.app.inDev || this.app.inTest) && (env === 'web' || env === 'test')\n }\n\n /**\n * Registers edge plugin when edge is installed\n */\n protected async registerEdgePlugin() {\n if (this.app.usingEdgeJS) {\n const edge = await import('edge.js')\n const vite = await this.app.container.make('vite')\n const { edgePluginVite } = await import('../src/plugins/edge.js')\n edge.default.use(edgePluginVite(vite))\n }\n }\n\n /**\n * Register Vite bindings\n */\n register() {\n const config = this.app.config.get<ViteOptions>('vite')\n\n const vite = new Vite(this.#shouldRunVite, config)\n this.app.container.bind('vite', () => vite)\n this.app.container.bind(ViteMiddleware, () => new ViteMiddleware(vite))\n }\n\n /**\n * - Register edge tags\n * - Start Vite server when running in development or test\n */\n async boot() {\n await this.registerEdgePlugin()\n\n if (!this.#shouldRunVite) return\n\n const vite = await this.app.container.make('vite')\n const server = await this.app.container.make('server')\n\n await vite.createDevServer()\n server.use([() => import('../src/middlewares/vite_middleware.js')])\n }\n\n /**\n * Stop Vite server when running in development or test\n */\n async shutdown() {\n if (!this.#shouldRunVite) return\n\n const vite = await this.app.container.make('vite')\n await vite.stopDevServer()\n }\n}\n"],"mappings":";;;;;;;;;AAqBA,IAAqB,eAArB,MAAkC;AAAA,EAGhC,YAAsB,KAAyB;AAAzB;AAIpB,UAAM,MAAM,KAAK,IAAI,eAAe;AACpC,SAAK,kBAAkB,KAAK,IAAI,SAAS,KAAK,IAAI,YAAY,QAAQ,SAAS,QAAQ;AAAA,EACzF;AAAA,EARA;AAAA;AAAA;AAAA;AAAA,EAaA,MAAgB,qBAAqB;AACnC,QAAI,KAAK,IAAI,aAAa;AACxB,YAAM,OAAO,MAAM,OAAO,SAAS;AACnC,YAAM,OAAO,MAAM,KAAK,IAAI,UAAU,KAAK,MAAM;AACjD,YAAM,EAAE,eAAe,IAAI,MAAM,OAAO,wBAAwB;AAChE,WAAK,QAAQ,IAAI,eAAe,IAAI,CAAC;AAAA,IACvC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,WAAW;AACT,UAAM,SAAS,KAAK,IAAI,OAAO,IAAiB,MAAM;AAEtD,UAAM,OAAO,IAAI,KAAK,KAAK,gBAAgB,MAAM;AACjD,SAAK,IAAI,UAAU,KAAK,QAAQ,MAAM,IAAI;AAC1C,SAAK,IAAI,UAAU,KAAK,gBAAgB,MAAM,IAAI,eAAe,IAAI,CAAC;AAAA,EACxE;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,OAAO;AACX,UAAM,KAAK,mBAAmB;AAE9B,QAAI,CAAC,KAAK;AAAgB;AAE1B,UAAM,OAAO,MAAM,KAAK,IAAI,UAAU,KAAK,MAAM;AACjD,UAAM,SAAS,MAAM,KAAK,IAAI,UAAU,KAAK,QAAQ;AAErD,UAAM,KAAK,gBAAgB;AAC3B,WAAO,IAAI,CAAC,MAAM,OAAO,gCAAuC,CAAC,CAAC;AAAA,EACpE;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,WAAW;AACf,QAAI,CAAC,KAAK;AAAgB;AAE1B,UAAM,OAAO,MAAM,KAAK,IAAI,UAAU,KAAK,MAAM;AACjD,UAAM,KAAK,cAAc;AAAA,EAC3B;AACF;","names":[]}
1
+ {"version":3,"sources":["../../providers/vite_provider.ts"],"sourcesContent":["/*\n * @adonisjs/vite\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type { ApplicationService } from '@adonisjs/core/types'\nimport type { cspKeywords as ShieldCSPKeywords } from '@adonisjs/shield'\n\nimport { Vite } from '../src/vite.js'\nimport type { ViteOptions } from '../src/types.js'\nimport ViteMiddleware from '../src/middleware/vite_middleware.js'\n\ndeclare module '@adonisjs/core/types' {\n interface ContainerBindings {\n vite: Vite\n }\n}\n\nexport default class ViteProvider {\n #shouldRunVite: boolean\n\n constructor(protected app: ApplicationService) {\n /**\n * We should only run Vite in development and test environments\n */\n const env = this.app.getEnvironment()\n this.#shouldRunVite = (this.app.inDev || this.app.inTest) && (env === 'web' || env === 'test')\n }\n\n /**\n * Registers edge plugin when edge is installed\n */\n protected async registerEdgePlugin() {\n if (this.app.usingEdgeJS) {\n const edge = await import('edge.js')\n const vite = await this.app.container.make('vite')\n const { edgePluginVite } = await import('../src/plugins/edge.js')\n edge.default.use(edgePluginVite(vite))\n }\n }\n\n /**\n * Registers CSP keywords when @adonisjs/shield is installed\n */\n protected async registerShieldKeywords() {\n let cspKeywords: typeof ShieldCSPKeywords | null = null\n try {\n const shieldExports = await import('@adonisjs/shield')\n cspKeywords = shieldExports.cspKeywords\n } catch {}\n\n if (!cspKeywords) return\n\n const vite = await this.app.container.make('vite')\n\n /**\n * Registering the @viteUrl keyword for CSP directives.\n * Returns http URL to the dev or the CDN server, otherwise\n * an empty string\n */\n cspKeywords.register('@viteUrl', function () {\n const assetsURL = vite.assetsUrl()\n if (!assetsURL || !assetsURL.startsWith('http://') || assetsURL.startsWith('https://')) {\n return ''\n }\n\n return assetsURL\n })\n }\n\n /**\n * Register Vite bindings\n */\n register() {\n const config = this.app.config.get<ViteOptions>('vite')\n\n const vite = new Vite(this.#shouldRunVite, config)\n this.app.container.bind('vite', () => vite)\n this.app.container.bind(ViteMiddleware, () => new ViteMiddleware(vite))\n }\n\n /**\n * - Register edge tags\n * - Start Vite server when running in development or test\n */\n async boot() {\n await this.registerEdgePlugin()\n\n if (!this.#shouldRunVite) return\n\n const vite = await this.app.container.make('vite')\n const server = await this.app.container.make('server')\n\n await vite.createDevServer()\n server.use([() => import('../src/middleware/vite_middleware.js')])\n }\n\n /**\n * Stop Vite server when running in development or test\n */\n async shutdown() {\n if (!this.#shouldRunVite) return\n\n const vite = await this.app.container.make('vite')\n await vite.stopDevServer()\n }\n}\n"],"mappings":";;;;;;;;;AAsBA,IAAqB,eAArB,MAAkC;AAAA,EAGhC,YAAsB,KAAyB;AAAzB;AAIpB,UAAM,MAAM,KAAK,IAAI,eAAe;AACpC,SAAK,kBAAkB,KAAK,IAAI,SAAS,KAAK,IAAI,YAAY,QAAQ,SAAS,QAAQ;A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AAQ;AAErD,UAAM,KAAK,gBAAgB;AAC3B,WAAO,IAAI,CAAC,MAAM,OAAO,gCAAsC,CAAC,CAAC;AAAA,EACnE;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,WAAW;AACf,QAAI,CAAC,KAAK;AAAgB;AAE1B,UAAM,OAAO,MAAM,KAAK,IAAI,UAAU,KAAK,MAAM;AACjD,UAAM,KAAK,cAAc;AAAA,EAC3B;AACF;","names":[]}
@@ -1,5 +1,5 @@
1
1
  import type { ViteRuntime } from 'vite/runtime';
2
- import type { Manifest, ViteDevServer } from 'vite';
2
+ import type { MainThreadRuntimeOptions, Manifest, ViteDevServer } from 'vite';
3
3
  import type { AdonisViteElement, ViteOptions } from './types.js';
4
4
  /**
5
5
  * Vite class exposes the APIs to generate tags and URLs for
@@ -14,13 +14,11 @@ export declare class Vite {
14
14
  */
15
15
  generateEntryPointsTags(entryPoints: string[] | string, attributes?: Record<string, any>): AdonisViteElement[];
16
16
  /**
17
- * Returns the dev server URL when running in hot
18
- * mode, otherwise returns the explicitly configured
19
- * "assets" URL
17
+ * Returns the explicitly configured assetsUrl
20
18
  */
21
- assetsUrl(): string | boolean | undefined;
19
+ assetsUrl(): string | undefined;
22
20
  /**
23
- * Returns path to a given asset file
21
+ * Returns path to a given asset file using the manifest file
24
22
  */
25
23
  assetPath(asset: string): string;
26
24
  /**
@@ -36,6 +34,12 @@ export declare class Vite {
36
34
  * since we don't need it
37
35
  */
38
36
  createDevServer(): Promise<void>;
37
+ /**
38
+ * Create a runtime instance
39
+ * Will not be available when running in production since
40
+ * it needs the Vite Dev server
41
+ */
42
+ createRuntime(options?: MainThreadRuntimeOptions): Promise<ViteRuntime>;
39
43
  /**
40
44
  * Stop the Vite Dev server
41
45
  */
@@ -45,11 +49,6 @@ export declare class Vite {
45
49
  * Will not be available when running in production
46
50
  */
47
51
  getDevServer(): ViteDevServer | undefined;
48
- /**
49
- * Get the Vite runtime instance
50
- * Will not be available when running in production
51
- */
52
- getRuntime(): ViteRuntime | undefined;
53
52
  /**
54
53
  * Returns the script needed for the HMR working with React
55
54
  */
@@ -0,0 +1,7 @@
1
+ import {
2
+ ViteMiddleware
3
+ } from "./chunk-MQRASPMO.js";
4
+ export {
5
+ ViteMiddleware as default
6
+ };
7
+ //# sourceMappingURL=vite_middleware-AQXJUYMB.js.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@adonisjs/vite",
3
3
  "description": "Vite plugin for AdonisJS",
4
- "version": "3.0.0-3",
4
+ "version": "3.0.0-5",
5
5
  "engines": {
6
6
  "node": ">=20.6.0"
7
7
  },
@@ -43,11 +43,13 @@
43
43
  "@adonisjs/core": "6.3.1",
44
44
  "@adonisjs/eslint-config": "^1.2.1",
45
45
  "@adonisjs/prettier-config": "^1.2.1",
46
+ "@adonisjs/session": "^7.1.1",
46
47
  "@adonisjs/shield": "^8.1.1",
47
48
  "@adonisjs/tsconfig": "^1.2.1",
48
49
  "@japa/assert": "2.1.0",
49
50
  "@japa/file-system": "^2.2.0",
50
51
  "@japa/runner": "3.1.1",
52
+ "@japa/snapshot": "^2.0.4",
51
53
  "@swc/core": "^1.4.2",
52
54
  "@types/node": "^20.11.20",
53
55
  "c8": "^9.1.0",
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/vite.ts"],"sourcesContent":["/*\n * @adonisjs/vite\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { readFileSync } from 'node:fs'\nimport type { ViteRuntime } from 'vite/runtime'\nimport type { Manifest, ViteDevServer } from 'vite'\n\nimport { makeAttributes, uniqBy } from './utils.js'\nimport type { AdonisViteElement, SetAttributes, ViteOptions } from './types.js'\n\n/**\n * Vite class exposes the APIs to generate tags and URLs for\n * assets processed using vite.\n */\nexport class Vite {\n /**\n * We cache the manifest file content in production\n * to avoid reading the file multiple times\n */\n #manifestCache?: Manifest\n #options: ViteOptions\n #runtime?: ViteRuntime\n #devServer?: ViteDevServer\n\n constructor(\n protected isViteRunning: boolean,\n options: ViteOptions\n ) {\n this.#options = options\n this.#options.assetsUrl = (this.#options.assetsUrl || '/').replace(/\\/$/, '')\n }\n\n /**\n * Reads the file contents as JSON\n */\n #readFileAsJSON(filePath: string) {\n return JSON.parse(readFileSync(filePath, 'utf-8'))\n }\n\n /**\n * Generates a JSON element with a custom toString implementation\n */\n #generateElement(element: AdonisViteElement) {\n return {\n ...element,\n toString() {\n const attributes = `${makeAttributes(element.attributes)}`\n if (element.tag === 'link') {\n return `<${element.tag} ${attributes}/>`\n }\n\n return `<${element.tag} ${attributes}>${element.children.join('\\n')}</${element.tag}>`\n },\n }\n }\n\n /**\n * Returns the script needed for the HMR working with Vite\n */\n #getViteHmrScript(attributes?: Record<string, any>): AdonisViteElement | null {\n return this.#generateElement({\n tag: 'script',\n attributes: {\n type: 'module',\n src: '/@vite/client',\n ...attributes,\n },\n children: [],\n })\n }\n\n /**\n * Check if the given path is a CSS path\n */\n #isCssPath(path: string) {\n return path.match(/\\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/) !== null\n }\n\n /**\n * Unwrap attributes from the user defined function or return\n * the attributes as it is\n */\n #unwrapAttributes(src: string, url: string, attributes?: SetAttributes) {\n if (typeof attributes === 'function') {\n return attributes({ src, url })\n }\n\n return attributes\n }\n\n /**\n * Create a style tag for the given path\n */\n #makeStyleTag(src: string, url: string, attributes?: Record<string, any>): AdonisViteElement {\n const customAttributes = this.#unwrapAttributes(src, url, this.#options?.styleAttributes)\n return this.#generateElement({\n tag: 'link',\n attributes: { rel: 'stylesheet', ...customAttributes, ...attributes, href: url },\n })\n }\n\n /**\n * Create a script tag for the given path\n */\n #makeScriptTag(src: string, url: string, attributes?: Record<string, any>): AdonisViteElement {\n const customAttributes = this.#unwrapAttributes(src, url, this.#options?.scriptAttributes)\n return this.#generateElement({\n tag: 'script',\n attributes: { type: 'module', ...customAttributes, ...attributes, src: url },\n children: [],\n })\n }\n\n /**\n * Generate a HTML tag for the given asset\n */\n #generateTag(asset: string, attributes?: Record<string, any>): AdonisViteElement {\n let url = ''\n if (this.isViteRunning) {\n url = `/${asset}`\n } else {\n url = `${this.#options.assetsUrl}/${asset}`\n }\n\n if (this.#isCssPath(asset)) {\n return this.#makeStyleTag(asset, url, attributes)\n }\n\n return this.#makeScriptTag(asset, url, attributes)\n }\n\n /**\n * Generate style and script tags for the given entrypoints\n * Also adds the @vite/client script\n */\n #generateEntryPointsTagsForHotMode(\n entryPoints: string[],\n attributes?: Record<string, any>\n ): AdonisViteElement[] {\n const viteHmr = this.#getViteHmrScript(attributes)\n const tags = entryPoints.map((entrypoint) => this.#generateTag(entrypoint, attributes))\n\n const result = viteHmr ? [viteHmr].concat(tags) : tags\n\n return result\n }\n\n /**\n * Get a chunk from the manifest file for a given file name\n */\n #chunk(manifest: Manifest, fileName: string) {\n const chunk = manifest[fileName]\n\n if (!chunk) {\n throw new Error(`Cannot find \"${fileName}\" chunk in the manifest file`)\n }\n\n return chunk\n }\n\n /**\n * Generate style and script tags for the given entrypoints\n * using the manifest file\n */\n #generateEntryPointsTagsWithManifest(\n entryPoints: string[],\n attributes?: Record<string, any>\n ): AdonisViteElement[] {\n const manifest = this.manifest()\n const tags: { path: string; tag: AdonisViteElement }[] = []\n\n for (const entryPoint of entryPoints) {\n const chunk = this.#chunk(manifest, entryPoint)\n tags.push({\n path: chunk.file,\n tag: this.#generateTag(chunk.file, { ...attributes, integrity: chunk.integrity }),\n })\n\n for (const css of chunk.css || []) {\n tags.push({ path: css, tag: this.#generateTag(css) })\n }\n }\n\n return uniqBy(tags, 'path')\n .sort((a) => (a.path.endsWith('.css') ? -1 : 1))\n .map((tag) => tag.tag)\n }\n\n /**\n * Generate tags for the entry points\n */\n generateEntryPointsTags(\n entryPoints: string[] | string,\n attributes?: Record<string, any>\n ): AdonisViteElement[] {\n entryPoints = Array.isArray(entryPoints) ? entryPoints : [entryPoints]\n\n if (this.isViteRunning) {\n return this.#generateEntryPointsTagsForHotMode(entryPoints, attributes)\n }\n\n return this.#generateEntryPointsTagsWithManifest(entryPoints, attributes)\n }\n\n /**\n * Returns the dev server URL when running in hot\n * mode, otherwise returns the explicitly configured\n * \"assets\" URL\n */\n assetsUrl() {\n if (this.isViteRunning) return this.#devServer!.config.server.host\n\n return this.#options.assetsUrl\n }\n\n /**\n * Returns path to a given asset file\n */\n assetPath(asset: string): string {\n if (this.isViteRunning) {\n return `/${asset}`\n }\n\n const chunk = this.#chunk(this.manifest(), asset)\n return `${this.#options.assetsUrl}/${chunk.file}`\n }\n\n /**\n * Returns the manifest file contents\n *\n * @throws Will throw an exception when running in dev\n */\n manifest(): Manifest {\n if (this.isViteRunning) {\n throw new Error('Cannot read the manifest file when running in dev mode')\n }\n\n if (!this.#manifestCache) {\n this.#manifestCache = this.#readFileAsJSON(this.#options.manifestFile)\n }\n\n return this.#manifestCache!\n }\n\n /**\n * Create the Vite Dev Server and runtime\n *\n * We lazy load the APIs to avoid loading it in production\n * since we don't need it\n */\n async createDevServer() {\n const { createViteRuntime, createServer } = await import('vite')\n\n this.#devServer = await createServer({\n server: { middlewareMode: true, hmr: { port: 3001 } },\n appType: 'custom',\n })\n\n this.#runtime = await createViteRuntime(this.#devServer)\n }\n\n /**\n * Stop the Vite Dev server\n */\n async stopDevServer() {\n await this.#devServer?.close()\n }\n\n /**\n * Get the Vite Dev server instance\n * Will not be available when running in production\n */\n getDevServer() {\n return this.#devServer\n }\n\n /**\n * Get the Vite runtime instance\n * Will not be available when running in production\n */\n getRuntime() {\n return this.#runtime\n }\n\n /**\n * Returns the script needed for the HMR working with React\n */\n getReactHmrScript(attributes?: Record<string, any>): AdonisViteElement | null {\n if (!this.isViteRunning) {\n return null\n }\n\n return this.#generateElement({\n tag: 'script',\n attributes: {\n type: 'module',\n ...attributes,\n },\n children: [\n '',\n `import RefreshRuntime from '/@react-refresh'`,\n `RefreshRuntime.injectIntoGlobalHook(window)`,\n `window.$RefreshReg$ = () => {}`,\n `window.$RefreshSig$ = () => (type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,CAAC;AAAA,EACH;AACF;","names":[]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/middlewares/vite_middleware.ts"],"sourcesContent":["/*\n * @adonisjs/vite\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type { ViteDevServer } from 'vite'\nimport type { HttpContext } from '@adonisjs/core/http'\nimport type { NextFn } from '@adonisjs/core/types/http'\n\nimport type { Vite } from '../vite.js'\n\n/**\n * Since Vite dev server is integrated within the AdonisJS process, this\n * middleware is used to proxy the requests to it.\n *\n * Some of the requests are directly handled by the Vite dev server,\n * like the one for the assets, while others are passed down to the\n * AdonisJS server.\n */\nexport default class ViteMiddleware {\n #devServer: ViteDevServer\n\n constructor(protected vite: Vite) {\n this.#devServer = this.vite.getDevServer()!\n }\n\n async handle({ request, response }: HttpContext, next: NextFn) {\n return await new Promise((resolve) => {\n this.#devServer.middlewares.handle(request.request, response.response, () => {\n return resolve(next())\n })\n })\n }\n}\n"],"mappings":";AAuBA,IAAqB,iBAArB,MAAoC;AAAA,EAGlC,YAAsB,MAAY;AAAZ;AACpB,SAAK,aAAa,KAAK,KAAK,aAAa;AAAA,EAC3C;AAAA,EAJA;AAAA,EAMA,MAAM,OAAO,EAAE,SAAS,SAAS,GAAgB,MAAc;AAC7D,WAAO,MAAM,IAAI,QAAQ,CAAC,YAAY;AACpC,WAAK,WAAW,YAAY,OAAO,QAAQ,SAAS,SAAS,UAAU,MAAM;AAC3E,eAAO,QAAQ,KAAK,CAAC;AAAA,MACvB,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF;","names":[]}
@@ -1,7 +0,0 @@
1
- import {
2
- ViteMiddleware
3
- } from "./chunk-YCD5RGKQ.js";
4
- export {
5
- ViteMiddleware as default
6
- };
7
- //# sourceMappingURL=vite_middleware-HYLIJP2B.js.map