@adonisjs/vite 3.0.0-0 → 3.0.0-10
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.
- package/build/chunk-CFRBPZ4N.js +30 -0
- package/build/chunk-CFRBPZ4N.js.map +1 -0
- package/build/chunk-MQRASPMO.js +20 -0
- package/build/chunk-MQRASPMO.js.map +1 -0
- package/build/chunk-W6CJOSLQ.js +347 -0
- package/build/chunk-W6CJOSLQ.js.map +1 -0
- package/build/index.js +47 -12
- package/build/index.js.map +1 -0
- package/build/providers/vite_provider.d.ts +16 -1
- package/build/providers/vite_provider.js +84 -47
- package/build/providers/vite_provider.js.map +1 -0
- package/build/services/vite.js +8 -16
- package/build/services/vite.js.map +1 -0
- package/build/src/client/config.d.ts +1 -8
- package/build/src/client/main.js +57 -20
- package/build/src/client/main.js.map +1 -0
- package/build/src/hooks/build_hook.d.ts +1 -1
- package/build/src/hooks/build_hook.js +15 -24
- package/build/src/hooks/build_hook.js.map +1 -0
- package/build/src/plugins/edge.js +69 -83
- package/build/src/plugins/edge.js.map +1 -0
- package/build/src/types.js +1 -9
- package/build/src/types.js.map +1 -0
- package/build/src/vite.d.ts +14 -15
- package/build/vite_middleware-AQXJUYMB.js +7 -0
- package/build/vite_middleware-AQXJUYMB.js.map +1 -0
- package/package.json +34 -18
- package/build/configure.js +0 -42
- package/build/src/client/config.js +0 -67
- package/build/src/client/types.js +0 -9
- package/build/src/define_config.js +0 -22
- package/build/src/middlewares/vite_middleware.js +0 -31
- package/build/src/utils.js +0 -42
- package/build/src/vite.js +0 -261
- package/build/stubs/main.js +0 -10
- /package/build/{stubs/config → config}/vite.stub +0 -0
- /package/build/{stubs/js_entrypoint.stub → js_entrypoint.stub} +0 -0
- /package/build/src/{middlewares → middleware}/vite_middleware.d.ts +0 -0
- /package/build/{stubs/vite.config.stub → vite.config.stub} +0 -0
|
@@ -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,20 @@
|
|
|
1
|
+
// src/middleware/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-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":[]}
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import {
|
|
2
|
+
makeAttributes,
|
|
3
|
+
uniqBy
|
|
4
|
+
} from "./chunk-CFRBPZ4N.js";
|
|
5
|
+
|
|
6
|
+
// src/vite.ts
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { readFileSync } from "node:fs";
|
|
9
|
+
import { slash } from "@poppinss/utils";
|
|
10
|
+
var styleFileRegex = /\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\?)/;
|
|
11
|
+
var Vite = class {
|
|
12
|
+
constructor(isViteRunning, options) {
|
|
13
|
+
this.isViteRunning = isViteRunning;
|
|
14
|
+
this.#options = options;
|
|
15
|
+
this.#options.assetsUrl = (this.#options.assetsUrl || "/").replace(/\/$/, "");
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* We cache the manifest file content in production
|
|
19
|
+
* to avoid reading the file multiple times
|
|
20
|
+
*/
|
|
21
|
+
#manifestCache;
|
|
22
|
+
#options;
|
|
23
|
+
#devServer;
|
|
24
|
+
#createServerPromise;
|
|
25
|
+
/**
|
|
26
|
+
* Reads the file contents as JSON
|
|
27
|
+
*/
|
|
28
|
+
#readFileAsJSON(filePath) {
|
|
29
|
+
return JSON.parse(readFileSync(filePath, "utf-8"));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Generates a JSON element with a custom toString implementation
|
|
33
|
+
*/
|
|
34
|
+
#generateElement(element) {
|
|
35
|
+
return {
|
|
36
|
+
...element,
|
|
37
|
+
toString() {
|
|
38
|
+
const attributes = `${makeAttributes(element.attributes)}`;
|
|
39
|
+
if (element.tag === "link") {
|
|
40
|
+
return `<${element.tag} ${attributes}/>`;
|
|
41
|
+
}
|
|
42
|
+
return `<${element.tag} ${attributes}>${element.children.join("\n")}</${element.tag}>`;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Returns the script needed for the HMR working with Vite
|
|
48
|
+
*/
|
|
49
|
+
#getViteHmrScript(attributes) {
|
|
50
|
+
return this.#generateElement({
|
|
51
|
+
tag: "script",
|
|
52
|
+
attributes: {
|
|
53
|
+
type: "module",
|
|
54
|
+
src: "/@vite/client",
|
|
55
|
+
...attributes
|
|
56
|
+
},
|
|
57
|
+
children: []
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Check if the given path is a CSS path
|
|
62
|
+
*/
|
|
63
|
+
#isCssPath(path) {
|
|
64
|
+
return path.match(styleFileRegex) !== null;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* If the module is a style module
|
|
68
|
+
*/
|
|
69
|
+
#isStyleModule(mod) {
|
|
70
|
+
if (this.#isCssPath(mod.url) || mod.id && /\?vue&type=style/.test(mod.id)) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Unwrap attributes from the user defined function or return
|
|
77
|
+
* the attributes as it is
|
|
78
|
+
*/
|
|
79
|
+
#unwrapAttributes(src, url, attributes) {
|
|
80
|
+
if (typeof attributes === "function") {
|
|
81
|
+
return attributes({ src, url });
|
|
82
|
+
}
|
|
83
|
+
return attributes;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Create a style tag for the given path
|
|
87
|
+
*/
|
|
88
|
+
#makeStyleTag(src, url, attributes) {
|
|
89
|
+
const customAttributes = this.#unwrapAttributes(src, url, this.#options?.styleAttributes);
|
|
90
|
+
return this.#generateElement({
|
|
91
|
+
tag: "link",
|
|
92
|
+
attributes: { rel: "stylesheet", ...customAttributes, ...attributes, href: url }
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Create a script tag for the given path
|
|
97
|
+
*/
|
|
98
|
+
#makeScriptTag(src, url, attributes) {
|
|
99
|
+
const customAttributes = this.#unwrapAttributes(src, url, this.#options?.scriptAttributes);
|
|
100
|
+
return this.#generateElement({
|
|
101
|
+
tag: "script",
|
|
102
|
+
attributes: { type: "module", ...customAttributes, ...attributes, src: url },
|
|
103
|
+
children: []
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Generate an asset URL for a given asset path
|
|
108
|
+
*/
|
|
109
|
+
#generateAssetUrl(path) {
|
|
110
|
+
return `${this.#options.assetsUrl}/${path}`;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Generate a HTML tag for the given asset
|
|
114
|
+
*/
|
|
115
|
+
#generateTag(asset, attributes) {
|
|
116
|
+
let url = "";
|
|
117
|
+
if (this.isViteRunning) {
|
|
118
|
+
url = `/${asset}`;
|
|
119
|
+
} else {
|
|
120
|
+
url = this.#generateAssetUrl(asset);
|
|
121
|
+
}
|
|
122
|
+
if (this.#isCssPath(asset)) {
|
|
123
|
+
return this.#makeStyleTag(asset, url, attributes);
|
|
124
|
+
}
|
|
125
|
+
return this.#makeScriptTag(asset, url, attributes);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Collect CSS files from the module graph recursively
|
|
129
|
+
*/
|
|
130
|
+
#collectCss(mod, styleUrls, visitedModules, importer) {
|
|
131
|
+
if (!mod.url)
|
|
132
|
+
return;
|
|
133
|
+
if (visitedModules.has(mod.url))
|
|
134
|
+
return;
|
|
135
|
+
visitedModules.add(mod.url);
|
|
136
|
+
if (this.#isStyleModule(mod) && (!importer || !this.#isStyleModule(importer))) {
|
|
137
|
+
if (mod.url.startsWith("/")) {
|
|
138
|
+
styleUrls.add(mod.url);
|
|
139
|
+
} else if (mod.url.startsWith("\0")) {
|
|
140
|
+
styleUrls.add(`/@id/__x00__${mod.url.substring(1)}`);
|
|
141
|
+
} else {
|
|
142
|
+
styleUrls.add(`/@id/${mod.url}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
mod.importedModules.forEach((dep) => this.#collectCss(dep, styleUrls, visitedModules, mod));
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Generate style and script tags for the given entrypoints
|
|
149
|
+
* Also adds the @vite/client script
|
|
150
|
+
*/
|
|
151
|
+
async #generateEntryPointsTagsForDevMode(entryPoints, attributes) {
|
|
152
|
+
const server = this.getDevServer();
|
|
153
|
+
const tags = entryPoints.map((entrypoint) => this.#generateTag(entrypoint, attributes));
|
|
154
|
+
const jsEntrypoints = entryPoints.filter((entrypoint) => !this.#isCssPath(entrypoint));
|
|
155
|
+
if (server?.moduleGraph.idToModuleMap.size === 0) {
|
|
156
|
+
await Promise.allSettled(
|
|
157
|
+
jsEntrypoints.map((entrypoint) => server.warmupRequest(`/${entrypoint}`))
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
const preloadUrls = /* @__PURE__ */ new Set();
|
|
161
|
+
const visitedModules = /* @__PURE__ */ new Set();
|
|
162
|
+
const cssTagsElement = /* @__PURE__ */ new Set();
|
|
163
|
+
for (const entryPoint of jsEntrypoints) {
|
|
164
|
+
const filePath = join(server.config.root, entryPoint);
|
|
165
|
+
const entryMod = server.moduleGraph.getModuleById(slash(filePath));
|
|
166
|
+
if (entryMod)
|
|
167
|
+
this.#collectCss(entryMod, preloadUrls, visitedModules);
|
|
168
|
+
}
|
|
169
|
+
const elements = Array.from(preloadUrls).map(
|
|
170
|
+
(href) => this.#generateElement({
|
|
171
|
+
tag: "link",
|
|
172
|
+
attributes: { rel: "stylesheet", as: "style", href }
|
|
173
|
+
})
|
|
174
|
+
);
|
|
175
|
+
elements.forEach((element) => cssTagsElement.add(element));
|
|
176
|
+
const viteHmr = this.#getViteHmrScript(attributes);
|
|
177
|
+
const result = [...cssTagsElement, viteHmr].concat(tags);
|
|
178
|
+
return result.sort((tag) => tag.tag === "link" ? -1 : 1);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Get a chunk from the manifest file for a given file name
|
|
182
|
+
*/
|
|
183
|
+
#chunk(manifest, entrypoint) {
|
|
184
|
+
const chunk = manifest[entrypoint];
|
|
185
|
+
if (!chunk) {
|
|
186
|
+
throw new Error(`Cannot find "${entrypoint}" chunk in the manifest file`);
|
|
187
|
+
}
|
|
188
|
+
return chunk;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Get a list of chunks for a given filename
|
|
192
|
+
*/
|
|
193
|
+
#chunksByFile(manifest, file) {
|
|
194
|
+
return Object.entries(manifest).filter(([, chunk]) => chunk.file === file).map(([_, chunk]) => chunk);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Generate preload tag for a given url
|
|
198
|
+
*/
|
|
199
|
+
#makePreloadTagForUrl(url) {
|
|
200
|
+
const attributes = this.#isCssPath(url) ? { rel: "preload", as: "style", href: url } : { rel: "modulepreload", href: url };
|
|
201
|
+
return this.#generateElement({ tag: "link", attributes });
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Generate style and script tags for the given entrypoints
|
|
205
|
+
* using the manifest file
|
|
206
|
+
*/
|
|
207
|
+
#generateEntryPointsTagsWithManifest(entryPoints, attributes) {
|
|
208
|
+
const manifest = this.manifest();
|
|
209
|
+
const tags = [];
|
|
210
|
+
const preloads = [];
|
|
211
|
+
for (const entryPoint of entryPoints) {
|
|
212
|
+
const chunk = this.#chunk(manifest, entryPoint);
|
|
213
|
+
preloads.push({ path: this.#generateAssetUrl(chunk.file) });
|
|
214
|
+
tags.push({
|
|
215
|
+
path: chunk.file,
|
|
216
|
+
tag: this.#generateTag(chunk.file, { ...attributes, integrity: chunk.integrity })
|
|
217
|
+
});
|
|
218
|
+
for (const css of chunk.css || []) {
|
|
219
|
+
preloads.push({ path: this.#generateAssetUrl(css) });
|
|
220
|
+
tags.push({ path: css, tag: this.#generateTag(css) });
|
|
221
|
+
}
|
|
222
|
+
for (const importNode of chunk.imports || []) {
|
|
223
|
+
preloads.push({ path: this.#generateAssetUrl(manifest[importNode].file) });
|
|
224
|
+
for (const css of manifest[importNode].css || []) {
|
|
225
|
+
const subChunk = this.#chunksByFile(manifest, css);
|
|
226
|
+
preloads.push({ path: this.#generateAssetUrl(css) });
|
|
227
|
+
tags.push({
|
|
228
|
+
path: this.#generateAssetUrl(css),
|
|
229
|
+
tag: this.#generateTag(css, {
|
|
230
|
+
...attributes,
|
|
231
|
+
integrity: subChunk[0]?.integrity
|
|
232
|
+
})
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const preloadsElements = uniqBy(preloads, "path").sort((preload) => this.#isCssPath(preload.path) ? -1 : 1).map((preload) => this.#makePreloadTagForUrl(preload.path));
|
|
238
|
+
return preloadsElements.concat(tags.map(({ tag }) => tag));
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Generate tags for the entry points
|
|
242
|
+
*/
|
|
243
|
+
async generateEntryPointsTags(entryPoints, attributes) {
|
|
244
|
+
entryPoints = Array.isArray(entryPoints) ? entryPoints : [entryPoints];
|
|
245
|
+
if (this.isViteRunning) {
|
|
246
|
+
return this.#generateEntryPointsTagsForDevMode(entryPoints, attributes);
|
|
247
|
+
}
|
|
248
|
+
return this.#generateEntryPointsTagsWithManifest(entryPoints, attributes);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Returns the explicitly configured assetsUrl
|
|
252
|
+
*/
|
|
253
|
+
assetsUrl() {
|
|
254
|
+
return this.#options.assetsUrl;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Returns path to a given asset file using the manifest file
|
|
258
|
+
*/
|
|
259
|
+
assetPath(asset) {
|
|
260
|
+
if (this.isViteRunning) {
|
|
261
|
+
return `/${asset}`;
|
|
262
|
+
}
|
|
263
|
+
const chunk = this.#chunk(this.manifest(), asset);
|
|
264
|
+
return this.#generateAssetUrl(chunk.file);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Returns the manifest file contents
|
|
268
|
+
*
|
|
269
|
+
* @throws Will throw an exception when running in dev
|
|
270
|
+
*/
|
|
271
|
+
manifest() {
|
|
272
|
+
if (this.isViteRunning) {
|
|
273
|
+
throw new Error("Cannot read the manifest file when running in dev mode");
|
|
274
|
+
}
|
|
275
|
+
if (!this.#manifestCache) {
|
|
276
|
+
this.#manifestCache = this.#readFileAsJSON(this.#options.manifestFile);
|
|
277
|
+
}
|
|
278
|
+
return this.#manifestCache;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Create the Vite Dev Server and runtime
|
|
282
|
+
*
|
|
283
|
+
* We lazy load the APIs to avoid loading it in production
|
|
284
|
+
* since we don't need it
|
|
285
|
+
*/
|
|
286
|
+
async createDevServer(options) {
|
|
287
|
+
const { createServer } = await import("vite");
|
|
288
|
+
this.#createServerPromise = createServer({
|
|
289
|
+
server: { middlewareMode: true },
|
|
290
|
+
appType: "custom",
|
|
291
|
+
...options
|
|
292
|
+
});
|
|
293
|
+
this.#devServer = await this.#createServerPromise;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Create a runtime instance
|
|
297
|
+
* Will not be available when running in production since
|
|
298
|
+
* it needs the Vite Dev server
|
|
299
|
+
*/
|
|
300
|
+
async createRuntime(options = {}) {
|
|
301
|
+
const { createViteRuntime } = await import("vite");
|
|
302
|
+
return createViteRuntime(this.#devServer, options);
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Stop the Vite Dev server
|
|
306
|
+
*/
|
|
307
|
+
async stopDevServer() {
|
|
308
|
+
await this.#createServerPromise;
|
|
309
|
+
await this.#devServer?.close();
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Get the Vite Dev server instance
|
|
313
|
+
* Will not be available when running in production
|
|
314
|
+
*/
|
|
315
|
+
getDevServer() {
|
|
316
|
+
return this.#devServer;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Returns the script needed for the HMR working with React
|
|
320
|
+
*/
|
|
321
|
+
getReactHmrScript(attributes) {
|
|
322
|
+
if (!this.isViteRunning) {
|
|
323
|
+
return null;
|
|
324
|
+
}
|
|
325
|
+
return this.#generateElement({
|
|
326
|
+
tag: "script",
|
|
327
|
+
attributes: {
|
|
328
|
+
type: "module",
|
|
329
|
+
...attributes
|
|
330
|
+
},
|
|
331
|
+
children: [
|
|
332
|
+
"",
|
|
333
|
+
`import RefreshRuntime from '/@react-refresh'`,
|
|
334
|
+
`RefreshRuntime.injectIntoGlobalHook(window)`,
|
|
335
|
+
`window.$RefreshReg$ = () => {}`,
|
|
336
|
+
`window.$RefreshSig$ = () => (type) => type`,
|
|
337
|
+
`window.__vite_plugin_react_preamble_installed__ = true`,
|
|
338
|
+
""
|
|
339
|
+
]
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
export {
|
|
345
|
+
Vite
|
|
346
|
+
};
|
|
347
|
+
//# sourceMappingURL=chunk-W6CJOSLQ.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 { join } from 'node:path'\nimport { readFileSync } from 'node:fs'\nimport { slash } from '@poppinss/utils'\nimport type { ViteRuntime } from 'vite/runtime'\nimport type {\n InlineConfig,\n MainThreadRuntimeOptions,\n Manifest,\n ModuleNode,\n ViteDevServer,\n} from 'vite'\n\nimport { makeAttributes, uniqBy } from './utils.js'\nimport type { AdonisViteElement, SetAttributes, ViteOptions } from './types.js'\n\nconst styleFileRegex = /\\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\\?)/\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 #createServerPromise?: Promise<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>) {\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(styleFileRegex) !== null\n }\n\n /**\n * If the module is a style module\n */\n #isStyleModule(mod: ModuleNode) {\n if (this.#isCssPath(mod.url) || (mod.id && /\\?vue&type=style/.test(mod.id))) {\n return true\n }\n return false\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 * Collect CSS files from the module graph recursively\n */\n #collectCss(\n mod: ModuleNode,\n styleUrls: Set<string>,\n visitedModules: Set<string>,\n importer?: ModuleNode\n ): void {\n if (!mod.url) return\n\n /**\n * Prevent visiting the same module twice\n */\n if (visitedModules.has(mod.url)) return\n visitedModules.add(mod.url)\n\n if (this.#isStyleModule(mod) && (!importer || !this.#isStyleModule(importer))) {\n if (mod.url.startsWith('/')) {\n styleUrls.add(mod.url)\n } else if (mod.url.startsWith('\\0')) {\n // virtual modules are prefixed with \\0\n styleUrls.add(`/@id/__x00__${mod.url.substring(1)}`)\n } else {\n styleUrls.add(`/@id/${mod.url}`)\n }\n }\n\n mod.importedModules.forEach((dep) => this.#collectCss(dep, styleUrls, visitedModules, mod))\n }\n\n /**\n * Generate style and script tags for the given entrypoints\n * Also adds the @vite/client script\n */\n async #generateEntryPointsTagsForDevMode(\n entryPoints: string[],\n attributes?: Record<string, any>\n ): Promise<AdonisViteElement[]> {\n const server = this.getDevServer()!\n\n const tags = entryPoints.map((entrypoint) => this.#generateTag(entrypoint, attributes))\n const jsEntrypoints = entryPoints.filter((entrypoint) => !this.#isCssPath(entrypoint))\n\n /**\n * If the module graph is empty, that means we didn't execute the entrypoint\n * yet : we just started the AdonisJS dev server.\n * So let's execute the entrypoints to populate the module graph\n */\n if (server?.moduleGraph.idToModuleMap.size === 0) {\n await Promise.allSettled(\n jsEntrypoints.map((entrypoint) => server.warmupRequest(`/${entrypoint}`))\n )\n }\n\n /**\n * We need to collect the CSS files imported by the entrypoints\n * Otherwise, we gonna have a FOUC each time we full reload the page\n */\n const preloadUrls = new Set<string>()\n const visitedModules = new Set<string>()\n const cssTagsElement = new Set<AdonisViteElement>()\n\n /**\n * Let's search for the CSS files by browsing the module graph\n * generated by Vite.\n */\n for (const entryPoint of jsEntrypoints) {\n const filePath = join(server.config.root, entryPoint)\n const entryMod = server.moduleGraph.getModuleById(slash(filePath))\n if (entryMod) this.#collectCss(entryMod, preloadUrls, visitedModules)\n }\n\n /**\n * Once we have the CSS files, generate associated tags\n * that will be injected into the HTML\n */\n const elements = Array.from(preloadUrls).map((href) =>\n this.#generateElement({\n tag: 'link',\n attributes: { rel: 'stylesheet', as: 'style', href: href },\n })\n )\n elements.forEach((element) => cssTagsElement.add(element))\n\n const viteHmr = this.#getViteHmrScript(attributes)\n const result = [...cssTagsElement, viteHmr].concat(tags)\n\n return result.sort((tag) => (tag.tag === 'link' ? -1 : 1))\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 async generateEntryPointsTags(\n entryPoints: string[] | string,\n attributes?: Record<string, any>\n ): Promise<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(options?: InlineConfig) {\n const { createServer } = await import('vite')\n\n /**\n * We do not await the server creation since it will\n * slow down the boot process of AdonisJS\n */\n this.#createServerPromise = createServer({\n server: { middlewareMode: true },\n appType: 'custom',\n ...options,\n })\n\n this.#devServer = await this.#createServerPromise\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.#createServerPromise\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,YAAY;AACrB,SAAS,oBAAoB;AAC7B,SAAS,aAAa;AAatB,IAAM,iBAAiB;AAMhB,IAAM,OAAN,MAAW;AAAA,EAUhB,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,EAXA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAA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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":[]}
|
package/build/index.js
CHANGED
|
@@ -1,12 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import {
|
|
2
|
+
Vite
|
|
3
|
+
} from "./chunk-W6CJOSLQ.js";
|
|
4
|
+
import "./chunk-CFRBPZ4N.js";
|
|
5
|
+
|
|
6
|
+
// stubs/main.ts
|
|
7
|
+
import { getDirname } from "@poppinss/utils";
|
|
8
|
+
var stubsRoot = getDirname(import.meta.url);
|
|
9
|
+
|
|
10
|
+
// configure.ts
|
|
11
|
+
async function configure(command) {
|
|
12
|
+
const codemods = await command.createCodemods();
|
|
13
|
+
let shouldInstallPackages = command.parsedFlags.install;
|
|
14
|
+
await codemods.makeUsingStub(stubsRoot, "config/vite.stub", {});
|
|
15
|
+
await codemods.makeUsingStub(stubsRoot, "vite.config.stub", {});
|
|
16
|
+
await codemods.makeUsingStub(stubsRoot, "js_entrypoint.stub", {});
|
|
17
|
+
await codemods.updateRcFile((rcFile) => {
|
|
18
|
+
rcFile.addProvider("@adonisjs/vite/vite_provider");
|
|
19
|
+
rcFile.addMetaFile("public/**", false);
|
|
20
|
+
});
|
|
21
|
+
if (shouldInstallPackages === void 0) {
|
|
22
|
+
shouldInstallPackages = await command.prompt.confirm('Do you want to install "vite"?');
|
|
23
|
+
}
|
|
24
|
+
if (shouldInstallPackages) {
|
|
25
|
+
await codemods.installPackages([{ name: "vite", isDevDependency: true }]);
|
|
26
|
+
} else {
|
|
27
|
+
await codemods.listPackagesToInstall([{ name: "vite", isDevDependency: true }]);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/define_config.ts
|
|
32
|
+
import { join } from "node:path";
|
|
33
|
+
function defineConfig(config) {
|
|
34
|
+
return {
|
|
35
|
+
buildDirectory: "public/assets",
|
|
36
|
+
assetsUrl: "/assets",
|
|
37
|
+
manifestFile: config.buildDirectory ? join(config.buildDirectory, ".vite/manifest.json") : "public/assets/.vite/manifest.json",
|
|
38
|
+
...config
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export {
|
|
42
|
+
Vite,
|
|
43
|
+
configure,
|
|
44
|
+
defineConfig,
|
|
45
|
+
stubsRoot
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -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":[]}
|
|
@@ -6,13 +6,28 @@ declare module '@adonisjs/core/types' {
|
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
8
|
export default class ViteProvider {
|
|
9
|
+
#private;
|
|
9
10
|
protected app: ApplicationService;
|
|
10
11
|
constructor(app: ApplicationService);
|
|
11
12
|
/**
|
|
12
13
|
* Registers edge plugin when edge is installed
|
|
13
14
|
*/
|
|
14
15
|
protected registerEdgePlugin(): Promise<void>;
|
|
15
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Registers CSP keywords when @adonisjs/shield is installed
|
|
18
|
+
*/
|
|
19
|
+
protected registerShieldKeywords(): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Register Vite bindings
|
|
22
|
+
*/
|
|
23
|
+
register(): void;
|
|
24
|
+
/**
|
|
25
|
+
* - Register edge tags
|
|
26
|
+
* - Start Vite server when running in development or test
|
|
27
|
+
*/
|
|
16
28
|
boot(): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Stop Vite server when running in development or test
|
|
31
|
+
*/
|
|
17
32
|
shutdown(): Promise<void>;
|
|
18
33
|
}
|