@adonisjs/vite 0.0.1-0 → 0.0.1-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/configure.d.ts +3 -1
- package/build/configure.js +24 -10
- package/build/index.d.ts +1 -1
- package/build/index.js +9 -0
- package/build/providers/vite_provider.d.ts +14 -4
- package/build/providers/vite_provider.js +82 -36
- package/build/services/vite.d.ts +1 -2
- package/build/services/vite.js +12 -0
- package/build/src/backend/debug.d.ts +3 -0
- package/{src/backend/types/extended.ts → build/src/backend/debug.js} +2 -11
- package/build/src/backend/define_config.d.ts +5 -0
- package/build/src/backend/define_config.js +19 -0
- package/build/src/backend/edge_plugin_vite.d.ts +7 -0
- package/build/src/backend/edge_plugin_vite.js +86 -0
- package/build/src/backend/types/extended.d.ts +8 -1
- package/build/src/backend/types/extended.js +8 -0
- package/build/src/backend/types/main.d.ts +57 -1
- package/build/src/backend/types/main.js +8 -0
- package/build/src/backend/utils.d.ts +7 -1
- package/build/src/backend/utils.js +29 -0
- package/build/src/backend/vite.d.ts +33 -13
- package/build/src/backend/vite.js +206 -113
- package/build/src/{vite_plugin → client}/config.d.ts +7 -2
- package/build/src/{vite_plugin → client}/config.js +28 -2
- package/build/src/{vite_plugin → client}/config_resolver.d.ts +13 -2
- package/build/src/{vite_plugin → client}/config_resolver.js +22 -3
- package/build/src/{vite_plugin → client}/helpers/inertia.d.ts +3 -1
- package/build/src/{vite_plugin → client}/helpers/inertia.js +11 -0
- package/build/src/{vite_plugin → client}/hot_file.d.ts +6 -1
- package/build/src/{vite_plugin → client}/hot_file.js +21 -2
- package/build/src/client/main.d.ts +6 -0
- package/build/src/client/main.js +24 -0
- package/build/src/client/types.d.ts +35 -0
- package/build/src/client/types.js +9 -0
- package/build/src/{vite_plugin → client}/utils.d.ts +7 -2
- package/build/src/{vite_plugin → client}/utils.js +14 -0
- package/build/stubs/client_config.stub +22 -0
- package/build/stubs/config.stub +21 -0
- package/build/stubs/index.d.ts +0 -1
- package/build/stubs/index.js +2 -2
- package/build/stubs/js_entrypoint.stub +4 -0
- package/package.json +74 -81
- package/build/configure.d.ts.map +0 -1
- package/build/index.d.ts.map +0 -1
- package/build/providers/vite_provider.d.ts.map +0 -1
- package/build/services/vite.d.ts.map +0 -1
- package/build/src/backend/types/extended.d.ts.map +0 -1
- package/build/src/backend/types/main.d.ts.map +0 -1
- package/build/src/backend/utils.d.ts.map +0 -1
- package/build/src/backend/vite.d.ts.map +0 -1
- package/build/src/vite_plugin/config.d.ts.map +0 -1
- package/build/src/vite_plugin/config_resolver.d.ts.map +0 -1
- package/build/src/vite_plugin/helpers/inertia.d.ts.map +0 -1
- package/build/src/vite_plugin/hot_file.d.ts.map +0 -1
- package/build/src/vite_plugin/index.d.ts +0 -4
- package/build/src/vite_plugin/index.d.ts.map +0 -1
- package/build/src/vite_plugin/index.js +0 -16
- package/build/src/vite_plugin/types/index.d.ts +0 -10
- package/build/src/vite_plugin/types/index.d.ts.map +0 -1
- package/build/src/vite_plugin/types/index.js +0 -1
- package/build/src/vite_plugin/utils.d.ts.map +0 -1
- package/build/stubs/index.d.ts.map +0 -1
- package/build/stubs/vite/vite_config.stub +0 -14
- package/configure.ts +0 -30
- package/index.ts +0 -15
- package/providers/vite_provider.ts +0 -89
- package/services/vite.ts +0 -23
- package/src/backend/types/main.ts +0 -31
- package/src/backend/utils.ts +0 -19
- package/src/backend/vite.ts +0 -367
- package/src/vite_plugin/config.ts +0 -97
- package/src/vite_plugin/config_resolver.ts +0 -62
- package/src/vite_plugin/helpers/inertia.ts +0 -27
- package/src/vite_plugin/hot_file.ts +0 -59
- package/src/vite_plugin/index.ts +0 -38
- package/src/vite_plugin/types/index.ts +0 -56
- package/src/vite_plugin/utils.ts +0 -40
|
@@ -1,67 +1,175 @@
|
|
|
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
|
+
*/
|
|
1
9
|
import { join } from 'node:path';
|
|
2
|
-
import { fileURLToPath } from 'node:url';
|
|
3
10
|
import { existsSync, readFileSync } from 'node:fs';
|
|
4
|
-
import
|
|
11
|
+
import debug from './debug.js';
|
|
12
|
+
import { makeAttributes, uniqBy } from './utils.js';
|
|
13
|
+
/**
|
|
14
|
+
* Vite class exposes the APIs to generate tags and URLs for
|
|
15
|
+
* assets processed using vite.
|
|
16
|
+
*/
|
|
5
17
|
export class Vite {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Manifest file name
|
|
20
|
+
*/
|
|
9
21
|
#manifestFilename = 'manifest.json';
|
|
10
|
-
|
|
22
|
+
/**
|
|
23
|
+
* We cache the manifest file content in production
|
|
24
|
+
* to avoid reading the file multiple times
|
|
25
|
+
*/
|
|
11
26
|
#manifestCache = null;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
this.#
|
|
27
|
+
/**
|
|
28
|
+
* Configuration options
|
|
29
|
+
*/
|
|
30
|
+
#options;
|
|
31
|
+
constructor(options) {
|
|
32
|
+
this.#options = options;
|
|
33
|
+
this.#options.assetsUrl = (this.#options.assetsUrl || '/').replace(/\/$/, '');
|
|
34
|
+
debug('vite config %O', this.#options);
|
|
18
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Checks if the application is running in hot mode
|
|
38
|
+
*/
|
|
19
39
|
#isRunningHot() {
|
|
20
|
-
return existsSync(this.#hotFile);
|
|
40
|
+
return existsSync(this.#options.hotFile);
|
|
21
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Reads the file contents as JSON
|
|
44
|
+
*/
|
|
22
45
|
#readFileAsJSON(filePath) {
|
|
23
46
|
return JSON.parse(readFileSync(filePath, 'utf-8'));
|
|
24
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns the parsed hot file content
|
|
50
|
+
*/
|
|
25
51
|
#readHotFile() {
|
|
26
|
-
return this.#readFileAsJSON(this.#hotFile);
|
|
52
|
+
return this.#readFileAsJSON(this.#options.hotFile);
|
|
27
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Get the path to an asset when running in hot mode
|
|
56
|
+
*/
|
|
28
57
|
#hotAsset(asset) {
|
|
29
58
|
return this.#readHotFile().url + '/' + asset;
|
|
30
59
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Unwrap attributes from the user defined function or return
|
|
62
|
+
* the attributes as it is
|
|
63
|
+
*/
|
|
64
|
+
#unwrapAttributes(src, url, attributes) {
|
|
65
|
+
if (typeof attributes === 'function') {
|
|
66
|
+
return attributes({ src, url });
|
|
34
67
|
}
|
|
35
|
-
return
|
|
68
|
+
return attributes;
|
|
36
69
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
70
|
+
/**
|
|
71
|
+
* Create a script tag for the given path
|
|
72
|
+
*/
|
|
73
|
+
#makeScriptTag(src, url, attributes) {
|
|
74
|
+
const customAttributes = this.#unwrapAttributes(src, url, this.#options.scriptAttributes);
|
|
75
|
+
return this.#generateElement({
|
|
76
|
+
tag: 'script',
|
|
77
|
+
attributes: { type: 'module', ...customAttributes, ...attributes, src: url },
|
|
78
|
+
children: [],
|
|
79
|
+
});
|
|
41
80
|
}
|
|
42
|
-
|
|
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
|
+
* Generate a HTML tag for the given asset
|
|
93
|
+
*/
|
|
94
|
+
#generateTag(asset, attributes) {
|
|
95
|
+
let url = '';
|
|
96
|
+
if (this.#isRunningHot()) {
|
|
97
|
+
url = this.#hotAsset(asset);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
url = `${this.#options.assetsUrl}/${asset}`;
|
|
101
|
+
}
|
|
102
|
+
if (this.#isCssPath(asset)) {
|
|
103
|
+
return this.#makeStyleTag(asset, url, attributes);
|
|
104
|
+
}
|
|
105
|
+
return this.#makeScriptTag(asset, url, attributes);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Generates a JSON element with a custom toString implementation
|
|
109
|
+
*/
|
|
110
|
+
#generateElement(element) {
|
|
111
|
+
return {
|
|
112
|
+
...element,
|
|
113
|
+
toString() {
|
|
114
|
+
const attributes = `${makeAttributes(element.attributes)}`;
|
|
115
|
+
if (element.tag === 'link') {
|
|
116
|
+
return `<${element.tag} ${attributes}/>`;
|
|
117
|
+
}
|
|
118
|
+
return `<${element.tag} ${attributes}>${element.children.join('\n')}</${element.tag}>`;
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Returns the script needed for the HMR working with Vite
|
|
124
|
+
*/
|
|
125
|
+
#getViteHmrScript(attributes) {
|
|
126
|
+
return this.#generateElement({
|
|
127
|
+
tag: 'script',
|
|
128
|
+
attributes: {
|
|
129
|
+
type: 'module',
|
|
130
|
+
src: this.#hotAsset('@vite/client'),
|
|
131
|
+
...attributes,
|
|
132
|
+
},
|
|
133
|
+
children: [],
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Generate style and script tags for the given entrypoints
|
|
138
|
+
* Also adds the @vite/client script
|
|
139
|
+
*/
|
|
140
|
+
#generateEntryPointsTagsForHotMode(entryPoints, attributes) {
|
|
141
|
+
const viteHmr = this.#getViteHmrScript(attributes);
|
|
142
|
+
const tags = entryPoints.map((entrypoint) => this.#generateTag(entrypoint, attributes));
|
|
143
|
+
return viteHmr ? [viteHmr].concat(tags) : tags;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Generate style and script tags for the given entrypoints
|
|
147
|
+
* using the manifest file
|
|
148
|
+
*/
|
|
149
|
+
#generateEntryPointsTagsWithManifest(entryPoints, attributes) {
|
|
43
150
|
const manifest = this.manifest();
|
|
44
151
|
const tags = [];
|
|
45
152
|
for (const entryPoint of entryPoints) {
|
|
46
153
|
const chunk = this.#chunk(manifest, entryPoint);
|
|
47
|
-
tags.push({
|
|
154
|
+
tags.push({
|
|
155
|
+
path: chunk.file,
|
|
156
|
+
tag: this.#generateTag(chunk.file, { ...attributes, integrity: chunk.integrity }),
|
|
157
|
+
});
|
|
48
158
|
for (const css of chunk.css || []) {
|
|
49
159
|
const cssChunk = this.#chunkByFile(manifest, css);
|
|
50
|
-
tags.push({
|
|
160
|
+
tags.push({
|
|
161
|
+
path: cssChunk.file,
|
|
162
|
+
tag: this.#generateTag(cssChunk.file, { ...attributes, integrity: cssChunk.integrity }),
|
|
163
|
+
});
|
|
51
164
|
}
|
|
52
165
|
}
|
|
53
166
|
return uniqBy(tags, 'path')
|
|
54
167
|
.sort((a) => (a.path.endsWith('.css') ? -1 : 1))
|
|
55
|
-
.map((
|
|
56
|
-
.join('\n');
|
|
57
|
-
}
|
|
58
|
-
generateEntryPointsTags(entryPoints) {
|
|
59
|
-
entryPoints = Array.isArray(entryPoints) ? entryPoints : [entryPoints];
|
|
60
|
-
if (this.#isRunningHot()) {
|
|
61
|
-
return this.#generateEntryPointsTagsForHotmode(entryPoints);
|
|
62
|
-
}
|
|
63
|
-
return this.#generateEntryPointsTagsWithManifest(entryPoints);
|
|
168
|
+
.map((tag) => tag.tag);
|
|
64
169
|
}
|
|
170
|
+
/**
|
|
171
|
+
* Get a chunk from the manifest file for a given file name
|
|
172
|
+
*/
|
|
65
173
|
#chunk(manifest, fileName) {
|
|
66
174
|
const chunk = manifest[fileName];
|
|
67
175
|
if (!chunk) {
|
|
@@ -69,6 +177,9 @@ export class Vite {
|
|
|
69
177
|
}
|
|
70
178
|
return chunk;
|
|
71
179
|
}
|
|
180
|
+
/**
|
|
181
|
+
* Get a chunk from the manifest file for a given hashed file name
|
|
182
|
+
*/
|
|
72
183
|
#chunkByFile(manifest, fileName) {
|
|
73
184
|
const chunk = Object.values(manifest).find((c) => c.file === fileName);
|
|
74
185
|
if (!chunk) {
|
|
@@ -76,107 +187,89 @@ export class Vite {
|
|
|
76
187
|
}
|
|
77
188
|
return chunk;
|
|
78
189
|
}
|
|
190
|
+
/**
|
|
191
|
+
* Check if the given path is a CSS path
|
|
192
|
+
*/
|
|
79
193
|
#isCssPath(path) {
|
|
80
194
|
return path.match(/\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/) !== null;
|
|
81
195
|
}
|
|
82
|
-
|
|
83
|
-
|
|
196
|
+
/**
|
|
197
|
+
* Generate tags for the entry points
|
|
198
|
+
*/
|
|
199
|
+
generateEntryPointsTags(entryPoints, attributes) {
|
|
200
|
+
entryPoints = Array.isArray(entryPoints) ? entryPoints : [entryPoints];
|
|
84
201
|
if (this.#isRunningHot()) {
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
url = `${this.#assetsUrl}/assets/${asset}`;
|
|
89
|
-
}
|
|
90
|
-
if (this.#isCssPath(asset)) {
|
|
91
|
-
return this.#makeStyleTag(asset, url);
|
|
202
|
+
return this.#generateEntryPointsTagsForHotMode(entryPoints, attributes);
|
|
92
203
|
}
|
|
93
|
-
return this.#
|
|
204
|
+
return this.#generateEntryPointsTagsWithManifest(entryPoints, attributes);
|
|
94
205
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
206
|
+
/**
|
|
207
|
+
* Returns the dev server URL when running in hot
|
|
208
|
+
* mode. Otherwise an empty string
|
|
209
|
+
*/
|
|
210
|
+
devUrl() {
|
|
211
|
+
if (this.#isRunningHot()) {
|
|
212
|
+
return this.#readHotFile().url;
|
|
98
213
|
}
|
|
99
|
-
return
|
|
214
|
+
return '';
|
|
100
215
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
.filter((attr) => attr !== null)
|
|
112
|
-
.join(' ');
|
|
113
|
-
}
|
|
114
|
-
#makeScriptTag(src, url) {
|
|
115
|
-
const customAttributes = this.#unwrapAttributes(src, url, this.#scriptAttributes);
|
|
116
|
-
const attributes = { type: 'module', ...customAttributes };
|
|
117
|
-
return `<script ${this.#makeAttributes(attributes)} src="${url}"></script>`;
|
|
118
|
-
}
|
|
119
|
-
#makeStyleTag(src, url) {
|
|
120
|
-
const customAttributes = this.#unwrapAttributes(src, url, this.#styleAttributes);
|
|
121
|
-
const attributes = { rel: 'stylesheet', ...customAttributes };
|
|
122
|
-
return `<link ${this.#makeAttributes(attributes)} href="${url}">`;
|
|
216
|
+
/**
|
|
217
|
+
* Returns the dev server URL when running in hot
|
|
218
|
+
* mode, otherwise returns the explicitly configured
|
|
219
|
+
* "assets" URL
|
|
220
|
+
*/
|
|
221
|
+
assetsUrl() {
|
|
222
|
+
if (this.#isRunningHot()) {
|
|
223
|
+
return this.#readHotFile().url;
|
|
224
|
+
}
|
|
225
|
+
return this.#options.assetsUrl;
|
|
123
226
|
}
|
|
227
|
+
/**
|
|
228
|
+
* Returns path to a given asset file
|
|
229
|
+
*/
|
|
124
230
|
assetPath(asset) {
|
|
125
231
|
if (this.#isRunningHot()) {
|
|
126
232
|
return this.#hotAsset(asset);
|
|
127
233
|
}
|
|
128
234
|
const chunk = this.#chunk(this.manifest(), asset);
|
|
129
|
-
return `${this.#assetsUrl}
|
|
235
|
+
return `${this.#options.assetsUrl}/${chunk.file}`;
|
|
130
236
|
}
|
|
237
|
+
/**
|
|
238
|
+
* Returns the manifest file contents
|
|
239
|
+
*
|
|
240
|
+
* @throws Will throw an exception when running in hot mode
|
|
241
|
+
*/
|
|
131
242
|
manifest() {
|
|
132
243
|
if (this.#isRunningHot()) {
|
|
133
244
|
throw new Error('Cannot read the manifest file when running in hot mode');
|
|
134
245
|
}
|
|
135
|
-
if (this.#manifestCache) {
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
const manifest = this.#readFileAsJSON(join(this.#buildDirectory, this.#manifestFilename));
|
|
139
|
-
if (this.application.inProduction) {
|
|
140
|
-
this.#manifestCache = manifest;
|
|
246
|
+
if (!this.#manifestCache) {
|
|
247
|
+
this.#manifestCache = this.#readFileAsJSON(join(this.#options.buildDirectory, this.#manifestFilename));
|
|
141
248
|
}
|
|
142
|
-
return
|
|
249
|
+
return this.#manifestCache;
|
|
143
250
|
}
|
|
144
|
-
|
|
251
|
+
/**
|
|
252
|
+
* Returns the script needed for the HMR working with React
|
|
253
|
+
*/
|
|
254
|
+
getReactHmrScript(attributes) {
|
|
145
255
|
if (!this.#isRunningHot()) {
|
|
146
|
-
return
|
|
256
|
+
return null;
|
|
147
257
|
}
|
|
148
|
-
return
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
return this;
|
|
165
|
-
}
|
|
166
|
-
setBuildDirectory(path) {
|
|
167
|
-
this.#buildDirectory = this.application.publicPath(path);
|
|
168
|
-
return this;
|
|
169
|
-
}
|
|
170
|
-
setAssetsUrl(url) {
|
|
171
|
-
this.#assetsUrl = url.endsWith('/') ? url : `${url}/`;
|
|
172
|
-
return this;
|
|
173
|
-
}
|
|
174
|
-
setScriptAttributes(attributes) {
|
|
175
|
-
this.#scriptAttributes = attributes;
|
|
176
|
-
return this;
|
|
177
|
-
}
|
|
178
|
-
setStyleAttributes(attributes) {
|
|
179
|
-
this.#styleAttributes = attributes;
|
|
180
|
-
return this;
|
|
258
|
+
return this.#generateElement({
|
|
259
|
+
tag: 'script',
|
|
260
|
+
attributes: {
|
|
261
|
+
type: 'module',
|
|
262
|
+
...attributes,
|
|
263
|
+
},
|
|
264
|
+
children: [
|
|
265
|
+
'',
|
|
266
|
+
`import RefreshRuntime from '${this.#hotAsset('@react-refresh')}'`,
|
|
267
|
+
`RefreshRuntime.injectIntoGlobalHook(window)`,
|
|
268
|
+
`window.$RefreshReg$ = () => {}`,
|
|
269
|
+
`window.$RefreshSig$ = () => (type) => type`,
|
|
270
|
+
`window.__vite_plugin_react_preamble_installed__ = true`,
|
|
271
|
+
'',
|
|
272
|
+
],
|
|
273
|
+
});
|
|
181
274
|
}
|
|
182
275
|
}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { ConfigEnv, Plugin, UserConfig } from 'vite';
|
|
2
|
-
import { PluginFullOptions } from './types
|
|
2
|
+
import type { PluginFullOptions } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Vite config hook
|
|
5
|
+
*/
|
|
3
6
|
export declare const configHook: (options: PluginFullOptions, userConfig: UserConfig, { command }: ConfigEnv) => UserConfig;
|
|
7
|
+
/**
|
|
8
|
+
* Update the user vite config to match the Adonis requirements
|
|
9
|
+
*/
|
|
4
10
|
export declare const config: (options: PluginFullOptions) => Plugin;
|
|
5
|
-
//# sourceMappingURL=config.d.ts.map
|
|
@@ -1,14 +1,29 @@
|
|
|
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
|
+
*/
|
|
1
9
|
import { defu } from 'defu';
|
|
2
|
-
import {
|
|
10
|
+
import { join } from 'node:path';
|
|
3
11
|
import { HotFile } from './hot_file.js';
|
|
12
|
+
import { resolveDevServerUrl } from './utils.js';
|
|
4
13
|
import { ConfigResolver } from './config_resolver.js';
|
|
5
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Vite config hook
|
|
16
|
+
*/
|
|
6
17
|
export const configHook = (options, userConfig, { command }) => {
|
|
7
18
|
const config = {
|
|
8
19
|
publicDir: userConfig.publicDir ?? false,
|
|
9
20
|
base: ConfigResolver.resolveBase(userConfig, options, command),
|
|
10
21
|
resolve: { alias: ConfigResolver.resolveAlias(userConfig) },
|
|
11
22
|
server: {
|
|
23
|
+
/**
|
|
24
|
+
* Will allow to rewrite the URL to the public path
|
|
25
|
+
* in dev mode
|
|
26
|
+
*/
|
|
12
27
|
origin: '__adonis_vite__',
|
|
13
28
|
},
|
|
14
29
|
build: {
|
|
@@ -23,11 +38,17 @@ export const configHook = (options, userConfig, { command }) => {
|
|
|
23
38
|
};
|
|
24
39
|
return defu(config, userConfig);
|
|
25
40
|
};
|
|
41
|
+
/**
|
|
42
|
+
* Update the user vite config to match the Adonis requirements
|
|
43
|
+
*/
|
|
26
44
|
export const config = (options) => {
|
|
27
45
|
let devServerUrl;
|
|
28
46
|
return {
|
|
29
47
|
name: 'vite-plugin-adonis:config',
|
|
30
48
|
config: configHook.bind(null, options),
|
|
49
|
+
/**
|
|
50
|
+
* Store the dev server url for further usage when rewriting URLs
|
|
51
|
+
*/
|
|
31
52
|
configureServer(server) {
|
|
32
53
|
const hotfile = new HotFile(options.hotFile);
|
|
33
54
|
server.httpServer?.once('listening', async () => {
|
|
@@ -36,6 +57,11 @@ export const config = (options) => {
|
|
|
36
57
|
});
|
|
37
58
|
server.httpServer?.on('close', () => hotfile.clean());
|
|
38
59
|
},
|
|
60
|
+
/**
|
|
61
|
+
* Rewrite URL to the public path in dev mode
|
|
62
|
+
*
|
|
63
|
+
* See : https://nystudio107.com/blog/using-vite-js-next-generation-frontend-tooling-with-craft-cms#vite-processed-assets
|
|
64
|
+
*/
|
|
39
65
|
transform: (code) => ({
|
|
40
66
|
code: code.replace(/__adonis_vite__/g, devServerUrl),
|
|
41
67
|
map: null,
|
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
import { ResolvedConfig, UserConfig, AliasOptions } from 'vite';
|
|
2
|
-
import { PluginFullOptions } from './types
|
|
2
|
+
import { PluginFullOptions } from './types.js';
|
|
3
3
|
export declare class ConfigResolver {
|
|
4
4
|
static resolvedConfig?: ResolvedConfig;
|
|
5
|
+
/**
|
|
6
|
+
* Resolve the `config.base` value
|
|
7
|
+
*/
|
|
5
8
|
static resolveBase(config: UserConfig, options: PluginFullOptions, command: 'build' | 'serve'): string;
|
|
9
|
+
/**
|
|
10
|
+
* Resolve the `config.resolve.alias` value
|
|
11
|
+
*
|
|
12
|
+
* Basically we are merging the user defined alias with the
|
|
13
|
+
* default alias.
|
|
14
|
+
*/
|
|
6
15
|
static resolveAlias(config: UserConfig): AliasOptions;
|
|
16
|
+
/**
|
|
17
|
+
* Resolve the `config.build.outDir` value
|
|
18
|
+
*/
|
|
7
19
|
static resolveOutDir(config: UserConfig, options: PluginFullOptions): string;
|
|
8
20
|
}
|
|
9
|
-
//# sourceMappingURL=config_resolver.d.ts.map
|
|
@@ -1,16 +1,32 @@
|
|
|
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
|
+
*/
|
|
1
9
|
import { addTrailingSlash } from './utils.js';
|
|
2
|
-
import { join } from 'node:path';
|
|
3
10
|
export class ConfigResolver {
|
|
4
11
|
static resolvedConfig;
|
|
12
|
+
/**
|
|
13
|
+
* Resolve the `config.base` value
|
|
14
|
+
*/
|
|
5
15
|
static resolveBase(config, options, command) {
|
|
6
16
|
if (config.base) {
|
|
7
17
|
return config.base;
|
|
8
18
|
}
|
|
9
19
|
if (command === 'build') {
|
|
10
|
-
return addTrailingSlash(options.assetsUrl)
|
|
20
|
+
return addTrailingSlash(options.assetsUrl);
|
|
11
21
|
}
|
|
12
22
|
return '/';
|
|
13
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Resolve the `config.resolve.alias` value
|
|
26
|
+
*
|
|
27
|
+
* Basically we are merging the user defined alias with the
|
|
28
|
+
* default alias.
|
|
29
|
+
*/
|
|
14
30
|
static resolveAlias(config) {
|
|
15
31
|
const defaultAlias = { '@/': `/resources/js/` };
|
|
16
32
|
if (Array.isArray(config.resolve?.alias)) {
|
|
@@ -21,7 +37,10 @@ export class ConfigResolver {
|
|
|
21
37
|
}
|
|
22
38
|
return { ...defaultAlias, ...config.resolve?.alias };
|
|
23
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Resolve the `config.build.outDir` value
|
|
42
|
+
*/
|
|
24
43
|
static resolveOutDir(config, options) {
|
|
25
|
-
return config.build?.outDir ??
|
|
44
|
+
return config.build?.outDir ?? options.buildDirectory;
|
|
26
45
|
}
|
|
27
46
|
}
|
|
@@ -1,3 +1,14 @@
|
|
|
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
|
+
/**
|
|
10
|
+
* Resolves a page component.
|
|
11
|
+
*/
|
|
1
12
|
export async function resolvePageComponent(name, pages) {
|
|
2
13
|
const path = Object.keys(pages)
|
|
3
14
|
.sort((a, b) => a.length - b.length)
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
export declare class HotFile {
|
|
2
2
|
#private;
|
|
3
3
|
constructor(path: string);
|
|
4
|
+
/**
|
|
5
|
+
* Write the hot file
|
|
6
|
+
*/
|
|
4
7
|
write(data: {
|
|
5
8
|
url: string;
|
|
6
9
|
}): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Delete the hot file
|
|
12
|
+
*/
|
|
7
13
|
clean(): void;
|
|
8
14
|
}
|
|
9
|
-
//# sourceMappingURL=hot_file.d.ts.map
|
|
@@ -1,9 +1,23 @@
|
|
|
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 { dirname, join } from 'node:path';
|
|
1
10
|
import { existsSync, rmSync } from 'node:fs';
|
|
2
11
|
import { mkdir, writeFile } from 'node:fs/promises';
|
|
3
|
-
import { dirname, join } from 'node:path';
|
|
4
12
|
import { ConfigResolver } from './config_resolver.js';
|
|
5
13
|
export class HotFile {
|
|
14
|
+
/**
|
|
15
|
+
* Path to the hot file
|
|
16
|
+
*/
|
|
6
17
|
#path;
|
|
18
|
+
/**
|
|
19
|
+
* Register hooks to clean the hot file on exit
|
|
20
|
+
*/
|
|
7
21
|
#cleanHotFileOnExit() {
|
|
8
22
|
const clean = this.clean.bind(this);
|
|
9
23
|
process.on('exit', clean);
|
|
@@ -11,16 +25,21 @@ export class HotFile {
|
|
|
11
25
|
process.on('SIGTERM', process.exit);
|
|
12
26
|
process.on('SIGHUP', process.exit);
|
|
13
27
|
process.on('SIGBREAK', process.exit);
|
|
14
|
-
process.on('SIGKILL', process.exit);
|
|
15
28
|
}
|
|
16
29
|
constructor(path) {
|
|
17
30
|
this.#path = join(ConfigResolver.resolvedConfig.root, path);
|
|
18
31
|
this.#cleanHotFileOnExit();
|
|
19
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Write the hot file
|
|
35
|
+
*/
|
|
20
36
|
async write(data) {
|
|
21
37
|
await mkdir(dirname(this.#path), { recursive: true });
|
|
22
38
|
await writeFile(this.#path, JSON.stringify(data, null, 2));
|
|
23
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Delete the hot file
|
|
42
|
+
*/
|
|
24
43
|
clean() {
|
|
25
44
|
if (!existsSync(this.#path)) {
|
|
26
45
|
return;
|
|
@@ -0,0 +1,24 @@
|
|
|
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 { defu } from 'defu';
|
|
10
|
+
import PluginRestart from 'vite-plugin-restart';
|
|
11
|
+
import { config } from './config.js';
|
|
12
|
+
const VitePluginRestart = PluginRestart;
|
|
13
|
+
/**
|
|
14
|
+
* Vite plugin for adonisjs
|
|
15
|
+
*/
|
|
16
|
+
export default function adonisjs(options) {
|
|
17
|
+
const fullOptions = defu(options, {
|
|
18
|
+
buildDirectory: 'public/assets',
|
|
19
|
+
assetsUrl: '/assets',
|
|
20
|
+
hotFile: 'public/assets/hot.json',
|
|
21
|
+
reload: ['./resources/views/**/*.edge'],
|
|
22
|
+
});
|
|
23
|
+
return [VitePluginRestart({ reload: fullOptions.reload }), config(fullOptions)];
|
|
24
|
+
}
|