@adonisjs/vite 5.1.0 → 5.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,451 +0,0 @@
1
- import {
2
- makeAttributes,
3
- uniqBy
4
- } from "./chunk-ZCQTQOMI.js";
5
-
6
- // src/vite.ts
7
- import { join } from "path";
8
- import string from "@poppinss/utils/string";
9
- import { existsSync, readFileSync } from "fs";
10
- var STYLE_FILE_REGEX = /\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\?)/;
11
- var Vite = class {
12
- /**
13
- * We cache the manifest file content in production
14
- * to avoid reading the file multiple times
15
- */
16
- #manifestCache;
17
- #options;
18
- #devServer;
19
- /**
20
- * Indicates whether the Vite manifest file exists on disk
21
- */
22
- hasManifestFile;
23
- get useDevServer() {
24
- return !!this.#devServer;
25
- }
26
- /**
27
- * Creates a new Vite instance for managing asset compilation and serving
28
- *
29
- * @param options - Configuration options for Vite integration
30
- *
31
- * @example
32
- * const vite = new Vite({
33
- * buildDirectory: 'build',
34
- * assetsUrl: '/assets',
35
- * manifestFile: 'build/manifest.json'
36
- * })
37
- */
38
- constructor(options) {
39
- this.#options = options;
40
- this.#options.assetsUrl = (this.#options.assetsUrl || "/").replace(/\/$/, "");
41
- this.hasManifestFile = existsSync(this.#options.manifestFile);
42
- }
43
- /**
44
- * Reads the file contents as JSON
45
- */
46
- #readFileAsJSON(filePath) {
47
- return JSON.parse(readFileSync(filePath, "utf-8"));
48
- }
49
- /**
50
- * Generates a JSON element with a custom toString implementation
51
- */
52
- #generateElement(element) {
53
- return {
54
- ...element,
55
- toString() {
56
- const attributes = `${makeAttributes(element.attributes)}`;
57
- if (element.tag === "link") {
58
- return `<${element.tag} ${attributes}/>`;
59
- }
60
- return `<${element.tag} ${attributes}>${element.children.join("\n")}</${element.tag}>`;
61
- }
62
- };
63
- }
64
- /**
65
- * Returns the script needed for the HMR working with Vite
66
- */
67
- #getViteHmrScript(attributes) {
68
- return this.#generateElement({
69
- tag: "script",
70
- attributes: {
71
- type: "module",
72
- src: "/@vite/client",
73
- ...attributes
74
- },
75
- children: []
76
- });
77
- }
78
- /**
79
- * Check if the given path is a CSS path
80
- */
81
- #isCssPath(path) {
82
- return path.match(STYLE_FILE_REGEX) !== null;
83
- }
84
- /**
85
- * If the module is a style module
86
- */
87
- #isStyleModule(mod) {
88
- if (this.#isCssPath(mod.url) || mod.id && /\?vue&type=style/.test(mod.id)) {
89
- return true;
90
- }
91
- return false;
92
- }
93
- /**
94
- * Unwrap attributes from the user defined function or return
95
- * the attributes as it is
96
- */
97
- #unwrapAttributes(src, url, attributes) {
98
- if (typeof attributes === "function") {
99
- return attributes({ src, url });
100
- }
101
- return attributes;
102
- }
103
- /**
104
- * Create a style tag for the given path
105
- */
106
- #makeStyleTag(src, url, attributes) {
107
- const customAttributes = this.#unwrapAttributes(src, url, this.#options?.styleAttributes);
108
- return this.#generateElement({
109
- tag: "link",
110
- attributes: { rel: "stylesheet", ...customAttributes, ...attributes, href: url }
111
- });
112
- }
113
- /**
114
- * Create a script tag for the given path
115
- */
116
- #makeScriptTag(src, url, attributes) {
117
- const customAttributes = this.#unwrapAttributes(src, url, this.#options?.scriptAttributes);
118
- return this.#generateElement({
119
- tag: "script",
120
- attributes: { type: "module", ...customAttributes, ...attributes, src: url },
121
- children: []
122
- });
123
- }
124
- /**
125
- * Generate an asset URL for a given asset path
126
- */
127
- #generateAssetUrl(path) {
128
- return `${this.#options.assetsUrl}/${path}`;
129
- }
130
- /**
131
- * Generate a HTML tag for the given asset
132
- */
133
- #generateTag(asset, attributes) {
134
- let url = "";
135
- if (this.useDevServer) {
136
- url = `/${asset}`;
137
- } else {
138
- url = this.#generateAssetUrl(asset);
139
- }
140
- if (this.#isCssPath(asset)) {
141
- return this.#makeStyleTag(asset, url, attributes);
142
- }
143
- return this.#makeScriptTag(asset, url, attributes);
144
- }
145
- /**
146
- * Collect CSS files from the module graph recursively
147
- */
148
- #collectCss(mod, styleUrls, visitedModules, importer) {
149
- if (!mod.url) {
150
- return;
151
- }
152
- if (visitedModules.has(mod.url)) {
153
- return;
154
- }
155
- visitedModules.add(mod.url);
156
- if (this.#isStyleModule(mod) && (!importer || !this.#isStyleModule(importer))) {
157
- if (mod.url.startsWith("/")) {
158
- styleUrls.add(mod.url);
159
- } else if (mod.url.startsWith("\0")) {
160
- styleUrls.add(`/@id/__x00__${mod.url.substring(1)}`);
161
- } else {
162
- styleUrls.add(`/@id/${mod.url}`);
163
- }
164
- }
165
- mod.importedModules.forEach((dep) => this.#collectCss(dep, styleUrls, visitedModules, mod));
166
- }
167
- /**
168
- * Generate style and script tags for the given entrypoints
169
- * Also adds the @vite/client script
170
- */
171
- async #generateEntryPointsTagsForDevMode(entryPoints, attributes) {
172
- const server = this.getDevServer();
173
- const tags = entryPoints.map((entrypoint) => this.#generateTag(entrypoint, attributes));
174
- const jsEntrypoints = entryPoints.filter((entrypoint) => !this.#isCssPath(entrypoint));
175
- if (server?.moduleGraph.idToModuleMap.size === 0) {
176
- await Promise.allSettled(
177
- jsEntrypoints.map((entrypoint) => server.warmupRequest(`/${entrypoint}`))
178
- );
179
- }
180
- const preloadUrls = /* @__PURE__ */ new Set();
181
- const visitedModules = /* @__PURE__ */ new Set();
182
- const cssTagsElement = /* @__PURE__ */ new Set();
183
- for (const entryPoint of jsEntrypoints) {
184
- const filePath = join(server.config.root, entryPoint);
185
- const entryMod = server.moduleGraph.getModuleById(string.toUnixSlash(filePath));
186
- if (entryMod) {
187
- this.#collectCss(entryMod, preloadUrls, visitedModules);
188
- }
189
- }
190
- const elements = Array.from(preloadUrls).map(
191
- (href) => this.#generateElement({
192
- tag: "link",
193
- attributes: { rel: "stylesheet", href }
194
- })
195
- );
196
- elements.forEach((element) => cssTagsElement.add(element));
197
- const viteHmr = this.#getViteHmrScript(attributes);
198
- const result = [...cssTagsElement, viteHmr].concat(tags);
199
- return result.sort((tag) => tag.tag === "link" ? -1 : 1);
200
- }
201
- /**
202
- * Get a chunk from the manifest file for a given file name
203
- */
204
- #chunk(manifest, entrypoint) {
205
- const chunk = manifest[entrypoint];
206
- if (!chunk) {
207
- throw new Error(`Cannot find "${entrypoint}" chunk in the manifest file`);
208
- }
209
- return chunk;
210
- }
211
- /**
212
- * Get a list of chunks for a given filename
213
- */
214
- #chunksByFile(manifest, file) {
215
- return Object.entries(manifest).filter(([, chunk]) => chunk.file === file).map(([_, chunk]) => chunk);
216
- }
217
- /**
218
- * Generate preload tag for a given url
219
- */
220
- #makePreloadTagForUrl(url) {
221
- const attributes = this.#isCssPath(url) ? { rel: "preload", as: "style", href: url } : { rel: "modulepreload", href: url };
222
- return this.#generateElement({ tag: "link", attributes });
223
- }
224
- /**
225
- * Generate style and script tags for the given entrypoints
226
- * using the manifest file
227
- */
228
- #generateEntryPointsTagsWithManifest(entryPoints, attributes) {
229
- const manifest = this.manifest();
230
- const tags = [];
231
- const preloads = [];
232
- for (const entryPoint of entryPoints) {
233
- const chunk = this.#chunk(manifest, entryPoint);
234
- preloads.push({ path: this.#generateAssetUrl(chunk.file) });
235
- tags.push({
236
- path: chunk.file,
237
- tag: this.#generateTag(chunk.file, { ...attributes, integrity: chunk.integrity })
238
- });
239
- for (const css of chunk.css || []) {
240
- preloads.push({ path: this.#generateAssetUrl(css) });
241
- tags.push({ path: css, tag: this.#generateTag(css) });
242
- }
243
- for (const importNode of chunk.imports || []) {
244
- preloads.push({ path: this.#generateAssetUrl(manifest[importNode].file) });
245
- for (const css of manifest[importNode].css || []) {
246
- const subChunk = this.#chunksByFile(manifest, css);
247
- preloads.push({ path: this.#generateAssetUrl(css) });
248
- tags.push({
249
- path: this.#generateAssetUrl(css),
250
- tag: this.#generateTag(css, {
251
- ...attributes,
252
- integrity: subChunk[0]?.integrity
253
- })
254
- });
255
- }
256
- }
257
- }
258
- const preloadsElements = uniqBy(preloads, "path").sort((preload) => this.#isCssPath(preload.path) ? -1 : 1).map((preload) => this.#makePreloadTagForUrl(preload.path));
259
- return preloadsElements.concat(tags.map(({ tag }) => tag));
260
- }
261
- /**
262
- * Generate HTML tags (script and link) for the specified entry points
263
- *
264
- * In development mode, includes HMR script and dynamically discovers CSS files.
265
- * In production mode, uses the manifest file to generate optimized tags with preloading.
266
- *
267
- * @param entryPoints - Single entry point or array of entry points to generate tags for
268
- * @param attributes - Additional HTML attributes to apply to the generated tags
269
- *
270
- * @example
271
- * // Generate tags for a single entry point
272
- * const tags = await vite.generateEntryPointsTags('app.js')
273
- *
274
- * @example
275
- * // Generate tags for multiple entry points with custom attributes
276
- * const tags = await vite.generateEntryPointsTags(
277
- * ['app.js', 'admin.js'],
278
- * { defer: true }
279
- * )
280
- */
281
- async generateEntryPointsTags(entryPoints, attributes) {
282
- entryPoints = Array.isArray(entryPoints) ? entryPoints : [entryPoints];
283
- if (this.useDevServer) {
284
- return this.#generateEntryPointsTagsForDevMode(entryPoints, attributes);
285
- }
286
- return this.#generateEntryPointsTagsWithManifest(entryPoints, attributes);
287
- }
288
- /**
289
- * Returns the base URL for serving static assets
290
- *
291
- * @example
292
- * const url = vite.assetsUrl()
293
- * // Returns: '/assets' or '/build' depending on configuration
294
- */
295
- assetsUrl() {
296
- return this.#options.assetsUrl;
297
- }
298
- /**
299
- * Returns the full URL path to a specific asset file
300
- *
301
- * In development mode, returns the asset path with leading slash.
302
- * In production mode, uses the manifest file to return the versioned/hashed asset URL.
303
- *
304
- * @param asset - The relative path to the asset file
305
- *
306
- * @example
307
- * const path = vite.assetPath('images/logo.png')
308
- * // Dev: '/images/logo.png'
309
- * // Prod: '/assets/images/logo-abc123.png'
310
- */
311
- assetPath(asset) {
312
- if (this.useDevServer) {
313
- return `/${asset}`;
314
- }
315
- const chunk = this.#chunk(this.manifest(), asset);
316
- return this.#generateAssetUrl(chunk.file);
317
- }
318
- /**
319
- * Returns the parsed Vite manifest file contents
320
- *
321
- * The manifest file contains information about compiled assets including
322
- * file paths, integrity hashes, and import dependencies.
323
- *
324
- * @throws Will throw an exception when running in development mode
325
- *
326
- * @example
327
- * const manifest = vite.manifest()
328
- * console.log(manifest['app.js'].file) // 'assets/app-abc123.js'
329
- */
330
- manifest() {
331
- if (this.useDevServer) {
332
- throw new Error("Cannot read the manifest file when running in dev mode");
333
- }
334
- if (!this.hasManifestFile) {
335
- throw new Error("Missing manifest file. Make sure to first create a build");
336
- }
337
- if (!this.#manifestCache) {
338
- this.#manifestCache = this.#readFileAsJSON(this.#options.manifestFile);
339
- }
340
- return this.#manifestCache;
341
- }
342
- /**
343
- * Creates and initializes the Vite development server
344
- *
345
- * Lazy loads Vite APIs to avoid importing them in production.
346
- * The server runs in middleware mode and is configured for custom app integration.
347
- *
348
- * @param options - Additional Vite configuration options to merge with defaults
349
- *
350
- * @example
351
- * await vite.createDevServer({
352
- * root: './src',
353
- * server: { port: 3000 }
354
- * })
355
- */
356
- async createDevServer(options) {
357
- const { createServer } = await import("vite");
358
- const hmrPort = Number(process.env.VITE_HMR_PORT);
359
- this.#devServer = await createServer({
360
- server: {
361
- middlewareMode: true,
362
- ...hmrPort && !Number.isNaN(hmrPort) ? { hmr: { port: hmrPort } } : {}
363
- },
364
- appType: "custom",
365
- ...options
366
- });
367
- }
368
- /**
369
- * Creates a server-side module runner for executing modules in Node.js context
370
- *
371
- * Only available in development mode as it requires the Vite dev server.
372
- * Useful for server-side rendering and module transformation.
373
- *
374
- * @param options - Configuration options for the module runner
375
- *
376
- * @example
377
- * const runner = await vite.createModuleRunner({
378
- * hmr: { port: 24678 }
379
- * })
380
- * const mod = await runner.import('./app.js')
381
- */
382
- async createModuleRunner(options = {}) {
383
- const { createServerModuleRunner } = await import("vite");
384
- return createServerModuleRunner(this.#devServer.environments.ssr, options);
385
- }
386
- /**
387
- * Gracefully stops the Vite development server
388
- *
389
- * Waits for the server creation promise to complete before closing.
390
- *
391
- * @example
392
- * await vite.stopDevServer()
393
- */
394
- async stopDevServer() {
395
- await this.#devServer?.close();
396
- }
397
- /**
398
- * Returns the Vite development server instance
399
- *
400
- * Only available in development mode after calling createDevServer().
401
- * Returns undefined in production or if the server hasn't been created yet.
402
- *
403
- * @example
404
- * const server = vite.getDevServer()
405
- * if (server) {
406
- * console.log('Dev server is running on', server.config.server.port)
407
- * }
408
- */
409
- getDevServer() {
410
- return this.#devServer;
411
- }
412
- /**
413
- * Generates the React Hot Module Replacement (HMR) script for development
414
- *
415
- * Only returns a script element in development mode. In production mode,
416
- * returns null since HMR is not needed.
417
- *
418
- * @param attributes - Additional HTML attributes to apply to the script tag
419
- *
420
- * @example
421
- * const hmrScript = vite.getReactHmrScript({ async: true })
422
- * if (hmrScript) {
423
- * console.log(hmrScript.toString()) // <script type="module" async>...</script>
424
- * }
425
- */
426
- getReactHmrScript(attributes) {
427
- if (!this.useDevServer) {
428
- return null;
429
- }
430
- return this.#generateElement({
431
- tag: "script",
432
- attributes: {
433
- type: "module",
434
- ...attributes
435
- },
436
- children: [
437
- "",
438
- `import RefreshRuntime from '/@react-refresh'`,
439
- `RefreshRuntime.injectIntoGlobalHook(window)`,
440
- `window.$RefreshReg$ = () => {}`,
441
- `window.$RefreshSig$ = () => (type) => type`,
442
- `window.__vite_plugin_react_preamble_installed__ = true`,
443
- ""
444
- ]
445
- });
446
- }
447
- };
448
-
449
- export {
450
- Vite
451
- };
@@ -1,29 +0,0 @@
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
- };