@adonisjs/vite 3.0.0-1 → 3.0.0-2

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.
@@ -0,0 +1,30 @@
1
+ // src/utils.ts
2
+ function uniqBy(array, key) {
3
+ const seen = /* @__PURE__ */ new Set();
4
+ return array.filter((item) => {
5
+ const k = item[key];
6
+ return seen.has(k) ? false : seen.add(k);
7
+ });
8
+ }
9
+ function makeAttributes(attributes) {
10
+ return Object.keys(attributes).map((key) => {
11
+ const value = attributes[key];
12
+ if (value === true) {
13
+ return key;
14
+ }
15
+ if (!value) {
16
+ return null;
17
+ }
18
+ return `${key}="${value}"`;
19
+ }).filter((attr) => attr !== null).join(" ");
20
+ }
21
+ var addTrailingSlash = (url) => {
22
+ return url.endsWith("/") ? url : url + "/";
23
+ };
24
+
25
+ export {
26
+ uniqBy,
27
+ makeAttributes,
28
+ addTrailingSlash
29
+ };
30
+ //# sourceMappingURL=chunk-CFRBPZ4N.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utils.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\n/**\n * Returns a new array with unique items by the given key\n */\nexport function uniqBy<T>(array: T[], key: keyof T): T[] {\n const seen = new Set()\n return array.filter((item) => {\n const k = item[key]\n return seen.has(k) ? false : seen.add(k)\n })\n}\n\n/**\n * Convert Record of attributes to a valid HTML string\n */\nexport function makeAttributes(attributes: Record<string, string | boolean>) {\n return Object.keys(attributes)\n .map((key) => {\n const value = attributes[key]\n\n if (value === true) {\n return key\n }\n\n if (!value) {\n return null\n }\n\n return `${key}=\"${value}\"`\n })\n .filter((attr) => attr !== null)\n .join(' ')\n}\n\n/**\n * Add a trailing slash if missing\n */\nexport const addTrailingSlash = (url: string) => {\n return url.endsWith('/') ? url : url + '/'\n}\n"],"mappings":";AAYO,SAAS,OAAU,OAAY,KAAmB;AACvD,QAAM,OAAO,oBAAI,IAAI;AACrB,SAAO,MAAM,OAAO,CAAC,SAAS;AAC5B,UAAM,IAAI,KAAK,GAAG;AAClB,WAAO,KAAK,IAAI,CAAC,IAAI,QAAQ,KAAK,IAAI,CAAC;AAAA,EACzC,CAAC;AACH;AAKO,SAAS,eAAe,YAA8C;AAC3E,SAAO,OAAO,KAAK,UAAU,EAC1B,IAAI,CAAC,QAAQ;AACZ,UAAM,QAAQ,WAAW,GAAG;AAE5B,QAAI,UAAU,MAAM;AAClB,aAAO;AAAA,IACT;AAEA,QAAI,CAAC,OAAO;AACV,aAAO;AAAA,IACT;AAEA,WAAO,GAAG,GAAG,KAAK,KAAK;AAAA,EACzB,CAAC,EACA,OAAO,CAAC,SAAS,SAAS,IAAI,EAC9B,KAAK,GAAG;AACb;AAKO,IAAM,mBAAmB,CAAC,QAAgB;AAC/C,SAAO,IAAI,SAAS,GAAG,IAAI,MAAM,MAAM;AACzC;","names":[]}
@@ -0,0 +1,255 @@
1
+ import {
2
+ makeAttributes,
3
+ uniqBy
4
+ } from "./chunk-CFRBPZ4N.js";
5
+
6
+ // src/vite.ts
7
+ import { readFileSync } from "node:fs";
8
+ var Vite = class {
9
+ constructor(inDev, options) {
10
+ this.inDev = inDev;
11
+ this.#options = options;
12
+ this.#options.assetsUrl = (this.#options.assetsUrl || "/").replace(/\/$/, "");
13
+ }
14
+ /**
15
+ * We cache the manifest file content in production
16
+ * to avoid reading the file multiple times
17
+ */
18
+ #manifestCache;
19
+ #options;
20
+ #runtime;
21
+ #devServer;
22
+ /**
23
+ * Reads the file contents as JSON
24
+ */
25
+ #readFileAsJSON(filePath) {
26
+ return JSON.parse(readFileSync(filePath, "utf-8"));
27
+ }
28
+ /**
29
+ * Generates a JSON element with a custom toString implementation
30
+ */
31
+ #generateElement(element) {
32
+ return {
33
+ ...element,
34
+ toString() {
35
+ const attributes = `${makeAttributes(element.attributes)}`;
36
+ if (element.tag === "link") {
37
+ return `<${element.tag} ${attributes}/>`;
38
+ }
39
+ return `<${element.tag} ${attributes}>${element.children.join("\n")}</${element.tag}>`;
40
+ }
41
+ };
42
+ }
43
+ /**
44
+ * Returns the script needed for the HMR working with Vite
45
+ */
46
+ #getViteHmrScript(attributes) {
47
+ return this.#generateElement({
48
+ tag: "script",
49
+ attributes: {
50
+ type: "module",
51
+ src: "/@vite/client",
52
+ ...attributes
53
+ },
54
+ children: []
55
+ });
56
+ }
57
+ /**
58
+ * Check if the given path is a CSS path
59
+ */
60
+ #isCssPath(path) {
61
+ return path.match(/\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/) !== null;
62
+ }
63
+ /**
64
+ * Unwrap attributes from the user defined function or return
65
+ * the attributes as it is
66
+ */
67
+ #unwrapAttributes(src, url, attributes) {
68
+ if (typeof attributes === "function") {
69
+ return attributes({ src, url });
70
+ }
71
+ return attributes;
72
+ }
73
+ /**
74
+ * Create a style tag for the given path
75
+ */
76
+ #makeStyleTag(src, url, attributes) {
77
+ const customAttributes = this.#unwrapAttributes(src, url, this.#options?.styleAttributes);
78
+ return this.#generateElement({
79
+ tag: "link",
80
+ attributes: { rel: "stylesheet", ...customAttributes, ...attributes, href: url }
81
+ });
82
+ }
83
+ /**
84
+ * Create a script tag for the given path
85
+ */
86
+ #makeScriptTag(src, url, attributes) {
87
+ const customAttributes = this.#unwrapAttributes(src, url, this.#options?.scriptAttributes);
88
+ return this.#generateElement({
89
+ tag: "script",
90
+ attributes: { type: "module", ...customAttributes, ...attributes, src: url },
91
+ children: []
92
+ });
93
+ }
94
+ /**
95
+ * Generate a HTML tag for the given asset
96
+ */
97
+ #generateTag(asset, attributes) {
98
+ let url = "";
99
+ if (this.inDev) {
100
+ url = `/${asset}`;
101
+ } else {
102
+ url = `${this.#options.assetsUrl}/${asset}`;
103
+ }
104
+ if (this.#isCssPath(asset)) {
105
+ return this.#makeStyleTag(asset, url, attributes);
106
+ }
107
+ return this.#makeScriptTag(asset, url, attributes);
108
+ }
109
+ /**
110
+ * Generate style and script tags for the given entrypoints
111
+ * Also adds the @vite/client script
112
+ */
113
+ #generateEntryPointsTagsForHotMode(entryPoints, attributes) {
114
+ const viteHmr = this.#getViteHmrScript(attributes);
115
+ const tags = entryPoints.map((entrypoint) => this.#generateTag(entrypoint, attributes));
116
+ const result = viteHmr ? [viteHmr].concat(tags) : tags;
117
+ return result;
118
+ }
119
+ /**
120
+ * Get a chunk from the manifest file for a given file name
121
+ */
122
+ #chunk(manifest, fileName) {
123
+ const chunk = manifest[fileName];
124
+ if (!chunk) {
125
+ throw new Error(`Cannot find "${fileName}" chunk in the manifest file`);
126
+ }
127
+ return chunk;
128
+ }
129
+ /**
130
+ * Generate style and script tags for the given entrypoints
131
+ * using the manifest file
132
+ */
133
+ #generateEntryPointsTagsWithManifest(entryPoints, attributes) {
134
+ const manifest = this.manifest();
135
+ const tags = [];
136
+ for (const entryPoint of entryPoints) {
137
+ const chunk = this.#chunk(manifest, entryPoint);
138
+ tags.push({
139
+ path: chunk.file,
140
+ tag: this.#generateTag(chunk.file, { ...attributes, integrity: chunk.integrity })
141
+ });
142
+ for (const css of chunk.css || []) {
143
+ tags.push({ path: css, tag: this.#generateTag(css) });
144
+ }
145
+ }
146
+ return uniqBy(tags, "path").sort((a) => a.path.endsWith(".css") ? -1 : 1).map((tag) => tag.tag);
147
+ }
148
+ /**
149
+ * Generate tags for the entry points
150
+ */
151
+ generateEntryPointsTags(entryPoints, attributes) {
152
+ entryPoints = Array.isArray(entryPoints) ? entryPoints : [entryPoints];
153
+ if (this.inDev) {
154
+ return this.#generateEntryPointsTagsForHotMode(entryPoints, attributes);
155
+ }
156
+ return this.#generateEntryPointsTagsWithManifest(entryPoints, attributes);
157
+ }
158
+ /**
159
+ * Returns the dev server URL when running in hot
160
+ * mode, otherwise returns the explicitly configured
161
+ * "assets" URL
162
+ */
163
+ assetsUrl() {
164
+ if (this.inDev)
165
+ return this.#devServer.config.server.host;
166
+ return this.#options.assetsUrl;
167
+ }
168
+ /**
169
+ * Returns path to a given asset file
170
+ */
171
+ assetPath(asset) {
172
+ if (this.inDev) {
173
+ return `/${asset}`;
174
+ }
175
+ const chunk = this.#chunk(this.manifest(), asset);
176
+ return `${this.#options.assetsUrl}/${chunk.file}`;
177
+ }
178
+ /**
179
+ * Returns the manifest file contents
180
+ *
181
+ * @throws Will throw an exception when running in dev
182
+ */
183
+ manifest() {
184
+ if (this.inDev) {
185
+ throw new Error("Cannot read the manifest file when running in dev mode");
186
+ }
187
+ if (!this.#manifestCache) {
188
+ this.#manifestCache = this.#readFileAsJSON(this.#options.manifestFile);
189
+ }
190
+ return this.#manifestCache;
191
+ }
192
+ /**
193
+ * Create the Vite Dev Server and runtime
194
+ *
195
+ * We lazy load the APIs to avoid loading it in production
196
+ * since we don't need it
197
+ */
198
+ async createDevServer() {
199
+ const { createViteRuntime, createServer } = await import("vite");
200
+ this.#devServer = await createServer({
201
+ server: { middlewareMode: true, hmr: { port: 3001 } },
202
+ appType: "custom"
203
+ });
204
+ this.#runtime = await createViteRuntime(this.#devServer);
205
+ }
206
+ /**
207
+ * Stop the Vite Dev server
208
+ */
209
+ async stopDevServer() {
210
+ await this.#devServer?.close();
211
+ }
212
+ /**
213
+ * Get the Vite Dev server instance
214
+ * Will not be available when running in production
215
+ */
216
+ getDevServer() {
217
+ return this.#devServer;
218
+ }
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
+ /**
227
+ * Returns the script needed for the HMR working with React
228
+ */
229
+ getReactHmrScript(attributes) {
230
+ if (!this.inDev) {
231
+ return null;
232
+ }
233
+ return this.#generateElement({
234
+ tag: "script",
235
+ attributes: {
236
+ type: "module",
237
+ ...attributes
238
+ },
239
+ children: [
240
+ "",
241
+ `import RefreshRuntime from '/@react-refresh'`,
242
+ `RefreshRuntime.injectIntoGlobalHook(window)`,
243
+ `window.$RefreshReg$ = () => {}`,
244
+ `window.$RefreshSig$ = () => (type) => type`,
245
+ `window.__vite_plugin_react_preamble_installed__ = true`,
246
+ ""
247
+ ]
248
+ });
249
+ }
250
+ };
251
+
252
+ export {
253
+ Vite
254
+ };
255
+ //# sourceMappingURL=chunk-J6JUW6YH.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 { 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 inDev: 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.inDev) {\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.inDev) {\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.inDev) 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.inDev) {\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.inDev) {\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.inDev) {\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,EAUhB,YACY,OACV,SACA;AAFU;AAGV,SAAK,WAAW;AAChB,SAAK,SAAS,aAAa,KAAK,SAAS,aAAa,KAAK,QAAQ,OAAO,EAAE;AAAA,EAC9E;AAAA;AAAA;AAAA;AAAA;AAAA,EAXA;AAAA,EACA;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;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BAAkB,KAAK,UAAU;AAAA,EACzD;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;AAAA,EAMA,aAAa;AACX,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,kBAAkB,YAA4D;AAC5E,QAAI,CAAC,KAAK,OAAO;AACf,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":[]}
@@ -0,0 +1,20 @@
1
+ // src/middlewares/vite_middleware.ts
2
+ var ViteMiddleware = class {
3
+ constructor(vite) {
4
+ this.vite = vite;
5
+ this.#devServer = this.vite.getDevServer();
6
+ }
7
+ #devServer;
8
+ async handle({ request, response }, next) {
9
+ return await new Promise((resolve) => {
10
+ this.#devServer.middlewares.handle(request.request, response.response, () => {
11
+ return resolve(next());
12
+ });
13
+ });
14
+ }
15
+ };
16
+
17
+ export {
18
+ ViteMiddleware
19
+ };
20
+ //# sourceMappingURL=chunk-YCD5RGKQ.js.map
@@ -0,0 +1 @@
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":[]}
@@ -0,0 +1,70 @@
1
+ // src/plugins/edge.ts
2
+ import { EdgeError } from "edge-error";
3
+ var edgePluginVite = (vite) => {
4
+ const edgeVite = (edge) => {
5
+ edge.global("vite", vite);
6
+ edge.global("asset", vite.assetPath.bind(vite));
7
+ edge.registerTag({
8
+ tagName: "viteReactRefresh",
9
+ seekable: true,
10
+ block: false,
11
+ compile(parser, buffer, token) {
12
+ let attributes = "";
13
+ if (token.properties.jsArg.trim()) {
14
+ const jsArg = `a,${token.properties.jsArg}`;
15
+ const parsed = parser.utils.transformAst(
16
+ parser.utils.generateAST(jsArg, token.loc, token.filename),
17
+ token.filename,
18
+ parser
19
+ );
20
+ attributes = parser.utils.stringify(parsed.expressions[1]);
21
+ }
22
+ buffer.writeExpression(
23
+ `const __vite_hmr_script = state.vite.getReactHmrScript(${attributes})`,
24
+ token.filename,
25
+ token.loc.start.line
26
+ );
27
+ buffer.writeStatement("if(__vite_hmr_script) {", token.filename, token.loc.start.line);
28
+ buffer.outputExpression(
29
+ `__vite_hmr_script.toString()`,
30
+ token.filename,
31
+ token.loc.start.line,
32
+ false
33
+ );
34
+ buffer.writeStatement("}", token.filename, token.loc.start.line);
35
+ }
36
+ });
37
+ edge.registerTag({
38
+ tagName: "vite",
39
+ seekable: true,
40
+ block: false,
41
+ compile(parser, buffer, token) {
42
+ if (!token.properties.jsArg.trim()) {
43
+ throw new EdgeError("Missing entrypoint name", "E_RUNTIME_EXCEPTION", {
44
+ filename: token.filename,
45
+ line: token.loc.start.line,
46
+ col: token.loc.start.col
47
+ });
48
+ }
49
+ const parsed = parser.utils.transformAst(
50
+ parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename),
51
+ token.filename,
52
+ parser
53
+ );
54
+ const entrypoints = parser.utils.stringify(parsed);
55
+ const methodCall = parsed.type === "SequenceExpression" ? `generateEntryPointsTags${entrypoints}` : `generateEntryPointsTags(${entrypoints})`;
56
+ buffer.outputExpression(
57
+ `state.vite.${methodCall}.join('\\n')`,
58
+ token.filename,
59
+ token.loc.start.line,
60
+ false
61
+ );
62
+ }
63
+ });
64
+ };
65
+ return edgeVite;
66
+ };
67
+ export {
68
+ edgePluginVite
69
+ };
70
+ //# sourceMappingURL=edge-SDXKV2NF.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/plugins/edge.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 { Edge } from 'edge.js'\nimport { EdgeError } from 'edge-error'\nimport type { PluginFn } from 'edge.js/types'\n\nimport type { Vite } from '../vite.js'\n\n/**\n * The edge plugin for vite to share vite service with edge\n * and register custom tags\n */\nexport const edgePluginVite: (vite: Vite) => PluginFn<undefined> = (vite) => {\n const edgeVite = (edge: Edge) => {\n edge.global('vite', vite)\n edge.global('asset', vite.assetPath.bind(vite))\n\n edge.registerTag({\n tagName: 'viteReactRefresh',\n seekable: true,\n block: false,\n compile(parser, buffer, token) {\n let attributes = ''\n if (token.properties.jsArg.trim()) {\n /**\n * Converting a single argument to a SequenceExpression so that we\n * work around the following edge cases.\n *\n * - If someone passes an object literal to the tag, ie { nonce: 'foo' }\n * it will be parsed as a LabeledStatement and not an object.\n * - If we wrap the object literal inside parenthesis, ie ({nonce: 'foo'})\n * then we will end up messing other expressions like a variable reference\n * , or a member expression and so on.\n * - So the best bet is to convert user supplied argument to a sequence expression\n * and hence ignore it during stringification.\n */\n const jsArg = `a,${token.properties.jsArg}`\n\n const parsed = parser.utils.transformAst(\n parser.utils.generateAST(jsArg, token.loc, token.filename),\n token.filename,\n parser\n )\n attributes = parser.utils.stringify(parsed.expressions[1])\n }\n\n /**\n * Get HMR script\n */\n buffer.writeExpression(\n `const __vite_hmr_script = state.vite.getReactHmrScript(${attributes})`,\n token.filename,\n token.loc.start.line\n )\n\n /**\n * Check if the script exists (only in hot mode)\n */\n buffer.writeStatement('if(__vite_hmr_script) {', token.filename, token.loc.start.line)\n\n /**\n * Write output\n */\n buffer.outputExpression(\n `__vite_hmr_script.toString()`,\n token.filename,\n token.loc.start.line,\n false\n )\n\n /**\n * Close if block\n */\n buffer.writeStatement('}', token.filename, token.loc.start.line)\n },\n })\n\n edge.registerTag({\n tagName: 'vite',\n seekable: true,\n block: false,\n compile(parser, buffer, token) {\n /**\n * Ensure an argument is defined\n */\n if (!token.properties.jsArg.trim()) {\n throw new EdgeError('Missing entrypoint name', 'E_RUNTIME_EXCEPTION', {\n filename: token.filename,\n line: token.loc.start.line,\n col: token.loc.start.col,\n })\n }\n\n const parsed = parser.utils.transformAst(\n parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename),\n token.filename,\n parser\n )\n\n const entrypoints = parser.utils.stringify(parsed)\n const methodCall =\n parsed.type === 'SequenceExpression'\n ? `generateEntryPointsTags${entrypoints}`\n : `generateEntryPointsTags(${entrypoints})`\n\n buffer.outputExpression(\n `state.vite.${methodCall}.join('\\\\n')`,\n token.filename,\n token.loc.start.line,\n false\n )\n },\n })\n }\n\n return edgeVite\n}\n"],"mappings":";AAUA,SAAS,iBAAiB;AASnB,IAAM,iBAAsD,CAAC,SAAS;AAC3E,QAAM,WAAW,CAAC,SAAe;AAC/B,SAAK,OAAO,QAAQ,IAAI;AACxB,SAAK,OAAO,SAAS,KAAK,UAAU,KAAK,IAAI,CAAC;AAE9C,SAAK,YAAY;AAAA,MACf,SAAS;AAAA,MACT,UAAU;AAAA,MACV,OAAO;AAAA,MACP,QAAQ,QAAQ,QAAQ,OAAO;AAC7B,YAAI,aAAa;AACjB,YAAI,MAAM,WAAW,MAAM,KAAK,GAAG;AAajC,gBAAM,QAAQ,KAAK,MAAM,WAAW,KAAK;AAEzC,gBAAM,SAAS,OAAO,MAAM;AAAA,YAC1B,OAAO,MAAM,YAAY,OAAO,MAAM,KAAK,MAAM,QAAQ;AAAA,YACzD,MAAM;AAAA,YACN;AAAA,UACF;AACA,uBAAa,OAAO,MAAM,UAAU,OAAO,YAAY,CAAC,CAAC;AAAA,QAC3D;AAKA,eAAO;AAAA,UACL,0DAA0D,UAAU;AAAA,UACpE,MAAM;AAAA,UACN,MAAM,IAAI,MAAM;AAAA,QAClB;AAKA,eAAO,eAAe,2BAA2B,MAAM,UAAU,MAAM,IAAI,MAAM,IAAI;AAKrF,eAAO;AAAA,UACL;AAAA,UACA,MAAM;AAAA,UACN,MAAM,IAAI,MAAM;AAAA,UAChB;AAAA,QACF;AAKA,eAAO,eAAe,KAAK,MAAM,UAAU,MAAM,IAAI,MAAM,IAAI;AAAA,MACjE;AAAA,IACF,CAAC;AAED,SAAK,YAAY;AAAA,MACf,SAAS;AAAA,MACT,UAAU;AAAA,MACV,OAAO;AAAA,MACP,QAAQ,QAAQ,QAAQ,OAAO;AAI7B,YAAI,CAAC,MAAM,WAAW,MAAM,KAAK,GAAG;AAClC,gBAAM,IAAI,UAAU,2BAA2B,uBAAuB;AAAA,YACpE,UAAU,MAAM;AAAA,YAChB,MAAM,MAAM,IAAI,MAAM;AAAA,YACtB,KAAK,MAAM,IAAI,MAAM;AAAA,UACvB,CAAC;AAAA,QACH;AAEA,cAAM,SAAS,OAAO,MAAM;AAAA,UAC1B,OAAO,MAAM,YAAY,MAAM,WAAW,OAAO,MAAM,KAAK,MAAM,QAAQ;AAAA,UAC1E,MAAM;AAAA,UACN;AAAA,QACF;AAEA,cAAM,cAAc,OAAO,MAAM,UAAU,MAAM;AACjD,cAAM,aACJ,OAAO,SAAS,uBACZ,0BAA0B,WAAW,KACrC,2BAA2B,WAAW;AAE5C,eAAO;AAAA,UACL,cAAc,UAAU;AAAA,UACxB,MAAM;AAAA,UACN,MAAM,IAAI,MAAM;AAAA,UAChB;AAAA,QACF;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../stubs/main.ts","../configure.ts","../src/define_config.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 { getDirname } from '@poppinss/utils'\nexport const stubsRoot = getDirname(import.meta.url)\n","/*\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 Configure from '@adonisjs/core/commands/configure'\n\nimport { stubsRoot } from './stubs/main.js'\n\n/**\n * Configures the package\n */\nexport async function configure(command: Configure) {\n const codemods = await command.createCodemods()\n let shouldInstallPackages: boolean | undefined = command.parsedFlags.install\n\n /**\n * Publish stubs\n */\n await codemods.makeUsingStub(stubsRoot, 'config/vite.stub', {})\n await codemods.makeUsingStub(stubsRoot, 'vite.config.stub', {})\n await codemods.makeUsingStub(stubsRoot, 'js_entrypoint.stub', {})\n\n await codemods.updateRcFile((rcFile) => {\n rcFile.addProvider('@adonisjs/vite/vite_provider')\n rcFile.addMetaFile('public/**', false)\n })\n\n /**\n * Prompt when `install` or `--no-install` flags are\n * not used\n */\n if (shouldInstallPackages === undefined) {\n shouldInstallPackages = await command.prompt.confirm('Do you want to install \"vite\"?')\n }\n\n /**\n * Install dependency or list the command to install it\n */\n if (shouldInstallPackages) {\n await codemods.installPackages([{ name: 'vite', isDevDependency: true }])\n } else {\n await codemods.listPackagesToInstall([{ name: 'vite', isDevDependency: true }])\n }\n}\n","/*\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 { join } from 'node:path'\n\nimport type { ViteOptions } from './types.js'\n\n/**\n * Define the backend config for Vite\n */\nexport function defineConfig(config: Partial<ViteOptions>): ViteOptions {\n return {\n buildDirectory: 'public/assets',\n assetsUrl: '/assets',\n manifestFile: config.buildDirectory\n ? join(config.buildDirectory, '.vite/manifest.json')\n : 'public/assets/.vite/manifest.json',\n ...config,\n }\n}\n"],"mappings":";;;;;;AASA,SAAS,kBAAkB;AACpB,IAAM,YAAY,WAAW,YAAY,GAAG;;;ACMnD,eAAsB,UAAU,SAAoB;AAClD,QAAM,WAAW,MAAM,QAAQ,eAAe;AAC9C,MAAI,wBAA6C,QAAQ,YAAY;AAKrE,QAAM,SAAS,cAAc,WAAW,oBAAoB,CAAC,CAAC;AAC9D,QAAM,SAAS,cAAc,WAAW,oBAAoB,CAAC,CAAC;AAC9D,QAAM,SAAS,cAAc,WAAW,sBAAsB,CAAC,CAAC;AAEhE,QAAM,SAAS,aAAa,CAAC,WAAW;AACtC,WAAO,YAAY,8BAA8B;AACjD,WAAO,YAAY,aAAa,KAAK;AAAA,EACvC,CAAC;AAMD,MAAI,0BAA0B,QAAW;AACvC,4BAAwB,MAAM,QAAQ,OAAO,QAAQ,gCAAgC;AAAA,EACvF;AAKA,MAAI,uBAAuB;AACzB,UAAM,SAAS,gBAAgB,CAAC,EAAE,MAAM,QAAQ,iBAAiB,KAAK,CAAC,CAAC;AAAA,EAC1E,OAAO;AACL,UAAM,SAAS,sBAAsB,CAAC,EAAE,MAAM,QAAQ,iBAAiB,KAAK,CAAC,CAAC;AAAA,EAChF;AACF;;;ACvCA,SAAS,YAAY;AAOd,SAAS,aAAa,QAA2C;AACtE,SAAO;AAAA,IACL,gBAAgB;AAAA,IAChB,WAAW;AAAA,IACX,cAAc,OAAO,iBACjB,KAAK,OAAO,gBAAgB,qBAAqB,IACjD;AAAA,IACJ,GAAG;AAAA,EACL;AACF;","names":[]}
@@ -0,0 +1,7 @@
1
+ import {
2
+ ViteMiddleware
3
+ } from "./chunk-YCD5RGKQ.js";
4
+ export {
5
+ ViteMiddleware as default
6
+ };
7
+ //# sourceMappingURL=vite_middleware-HYLIJP2B.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
package/package.json CHANGED
@@ -1,21 +1,17 @@
1
1
  {
2
2
  "name": "@adonisjs/vite",
3
3
  "description": "Vite plugin for AdonisJS",
4
- "version": "3.0.0-1",
4
+ "version": "3.0.0-2",
5
5
  "engines": {
6
6
  "node": ">=20.6.0"
7
7
  },
8
8
  "main": "build/index.js",
9
9
  "type": "module",
10
10
  "files": [
11
- "build/providers",
12
- "build/services",
13
- "build/src",
14
- "build/stubs",
15
- "build/configure.js",
16
- "build/configure.d.ts",
17
- "build/index.js",
18
- "build/index.d.ts"
11
+ "build",
12
+ "!build/bin",
13
+ "!build/tests",
14
+ "!build/tests_helpers"
19
15
  ],
20
16
  "exports": {
21
17
  ".": "./build/index.js",