@adonisjs/vite 3.0.0-0 → 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.
package/build/src/vite.js DELETED
@@ -1,261 +0,0 @@
1
- /*
2
- * @adonisjs/vite
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- import { readFileSync } from 'node:fs';
10
- import { makeAttributes, uniqBy } from './utils.js';
11
- /**
12
- * Vite class exposes the APIs to generate tags and URLs for
13
- * assets processed using vite.
14
- */
15
- export class Vite {
16
- inDev;
17
- /**
18
- * We cache the manifest file content in production
19
- * to avoid reading the file multiple times
20
- */
21
- #manifestCache;
22
- #options;
23
- #runtime;
24
- #devServer;
25
- constructor(inDev, options) {
26
- this.inDev = inDev;
27
- this.#options = options;
28
- this.#options.assetsUrl = (this.#options.assetsUrl || '/').replace(/\/$/, '');
29
- }
30
- /**
31
- * Reads the file contents as JSON
32
- */
33
- #readFileAsJSON(filePath) {
34
- return JSON.parse(readFileSync(filePath, 'utf-8'));
35
- }
36
- /**
37
- * Generates a JSON element with a custom toString implementation
38
- */
39
- #generateElement(element) {
40
- return {
41
- ...element,
42
- toString() {
43
- const attributes = `${makeAttributes(element.attributes)}`;
44
- if (element.tag === 'link') {
45
- return `<${element.tag} ${attributes}/>`;
46
- }
47
- return `<${element.tag} ${attributes}>${element.children.join('\n')}</${element.tag}>`;
48
- },
49
- };
50
- }
51
- /**
52
- * Returns the script needed for the HMR working with Vite
53
- */
54
- #getViteHmrScript(attributes) {
55
- return this.#generateElement({
56
- tag: 'script',
57
- attributes: {
58
- type: 'module',
59
- src: '/@vite/client',
60
- ...attributes,
61
- },
62
- children: [],
63
- });
64
- }
65
- /**
66
- * Check if the given path is a CSS path
67
- */
68
- #isCssPath(path) {
69
- return path.match(/\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/) !== null;
70
- }
71
- /**
72
- * Unwrap attributes from the user defined function or return
73
- * the attributes as it is
74
- */
75
- #unwrapAttributes(src, url, attributes) {
76
- if (typeof attributes === 'function') {
77
- return attributes({ src, url });
78
- }
79
- return attributes;
80
- }
81
- /**
82
- * Create a style tag for the given path
83
- */
84
- #makeStyleTag(src, url, attributes) {
85
- const customAttributes = this.#unwrapAttributes(src, url, this.#options?.styleAttributes);
86
- return this.#generateElement({
87
- tag: 'link',
88
- attributes: { rel: 'stylesheet', ...customAttributes, ...attributes, href: url },
89
- });
90
- }
91
- /**
92
- * Create a script tag for the given path
93
- */
94
- #makeScriptTag(src, url, attributes) {
95
- const customAttributes = this.#unwrapAttributes(src, url, this.#options?.scriptAttributes);
96
- return this.#generateElement({
97
- tag: 'script',
98
- attributes: { type: 'module', ...customAttributes, ...attributes, src: url },
99
- children: [],
100
- });
101
- }
102
- /**
103
- * Generate a HTML tag for the given asset
104
- */
105
- #generateTag(asset, attributes) {
106
- let url = '';
107
- if (this.inDev) {
108
- url = `/${asset}`;
109
- }
110
- else {
111
- url = `${this.#options.assetsUrl}/${asset}`;
112
- }
113
- if (this.#isCssPath(asset)) {
114
- return this.#makeStyleTag(asset, url, attributes);
115
- }
116
- return this.#makeScriptTag(asset, url, attributes);
117
- }
118
- /**
119
- * Generate style and script tags for the given entrypoints
120
- * Also adds the @vite/client script
121
- */
122
- #generateEntryPointsTagsForHotMode(entryPoints, attributes) {
123
- const viteHmr = this.#getViteHmrScript(attributes);
124
- const tags = entryPoints.map((entrypoint) => this.#generateTag(entrypoint, attributes));
125
- const result = viteHmr ? [viteHmr].concat(tags) : tags;
126
- return result;
127
- }
128
- /**
129
- * Get a chunk from the manifest file for a given file name
130
- */
131
- #chunk(manifest, fileName) {
132
- const chunk = manifest[fileName];
133
- if (!chunk) {
134
- throw new Error(`Cannot find "${fileName}" chunk in the manifest file`);
135
- }
136
- return chunk;
137
- }
138
- /**
139
- * Generate style and script tags for the given entrypoints
140
- * using the manifest file
141
- */
142
- #generateEntryPointsTagsWithManifest(entryPoints, attributes) {
143
- const manifest = this.manifest();
144
- const tags = [];
145
- for (const entryPoint of entryPoints) {
146
- const chunk = this.#chunk(manifest, entryPoint);
147
- tags.push({
148
- path: chunk.file,
149
- tag: this.#generateTag(chunk.file, { ...attributes, integrity: chunk.integrity }),
150
- });
151
- for (const css of chunk.css || []) {
152
- tags.push({ path: css, tag: this.#generateTag(css) });
153
- }
154
- }
155
- return uniqBy(tags, 'path')
156
- .sort((a) => (a.path.endsWith('.css') ? -1 : 1))
157
- .map((tag) => tag.tag);
158
- }
159
- /**
160
- * Generate tags for the entry points
161
- */
162
- generateEntryPointsTags(entryPoints, attributes) {
163
- entryPoints = Array.isArray(entryPoints) ? entryPoints : [entryPoints];
164
- if (this.inDev) {
165
- return this.#generateEntryPointsTagsForHotMode(entryPoints, attributes);
166
- }
167
- return this.#generateEntryPointsTagsWithManifest(entryPoints, attributes);
168
- }
169
- /**
170
- * Returns the dev server URL when running in hot
171
- * mode, otherwise returns the explicitly configured
172
- * "assets" URL
173
- */
174
- assetsUrl() {
175
- if (this.inDev)
176
- return this.#devServer.config.server.host;
177
- return this.#options.assetsUrl;
178
- }
179
- /**
180
- * Returns path to a given asset file
181
- */
182
- assetPath(asset) {
183
- if (this.inDev) {
184
- return `/${asset}`;
185
- }
186
- const chunk = this.#chunk(this.manifest(), asset);
187
- return `${this.#options.assetsUrl}/${chunk.file}`;
188
- }
189
- /**
190
- * Returns the manifest file contents
191
- *
192
- * @throws Will throw an exception when running in dev
193
- */
194
- manifest() {
195
- if (this.inDev) {
196
- throw new Error('Cannot read the manifest file when running in dev mode');
197
- }
198
- if (!this.#manifestCache) {
199
- this.#manifestCache = this.#readFileAsJSON(this.#options.manifestFile);
200
- }
201
- return this.#manifestCache;
202
- }
203
- /**
204
- * Create the Vite Dev Server and runtime
205
- *
206
- * We lazy load the APIs to avoid loading it in production
207
- * since we don't need it
208
- */
209
- async createDevServer() {
210
- const { createViteRuntime, createServer } = await import('vite');
211
- this.#devServer = await createServer({
212
- server: { middlewareMode: true, hmr: { port: 3001 } },
213
- appType: 'custom',
214
- });
215
- this.#runtime = await createViteRuntime(this.#devServer);
216
- }
217
- /**
218
- * Stop the Vite Dev server
219
- */
220
- async stopDevServer() {
221
- await this.#devServer?.close();
222
- }
223
- /**
224
- * Get the Vite Dev server instance
225
- * Will not be available when running in production
226
- */
227
- getDevServer() {
228
- return this.#devServer;
229
- }
230
- /**
231
- * Get the Vite runtime instance
232
- * Will not be available when running in production
233
- */
234
- getRuntime() {
235
- return this.#runtime;
236
- }
237
- /**
238
- * Returns the script needed for the HMR working with React
239
- */
240
- getReactHmrScript(attributes) {
241
- if (!this.inDev) {
242
- return null;
243
- }
244
- return this.#generateElement({
245
- tag: 'script',
246
- attributes: {
247
- type: 'module',
248
- ...attributes,
249
- },
250
- children: [
251
- '',
252
- `import RefreshRuntime from '/@react-refresh'`,
253
- `RefreshRuntime.injectIntoGlobalHook(window)`,
254
- `window.$RefreshReg$ = () => {}`,
255
- `window.$RefreshSig$ = () => (type) => type`,
256
- `window.__vite_plugin_react_preamble_installed__ = true`,
257
- '',
258
- ],
259
- });
260
- }
261
- }
@@ -1,10 +0,0 @@
1
- /*
2
- * @adonisjs/vite
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- import { getDirname } from '@poppinss/utils';
10
- export const stubsRoot = getDirname(import.meta.url);