@adonisjs/vite 0.0.1-0
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/LICENSE.md +9 -0
- package/README.md +36 -0
- package/build/configure.d.ts +3 -0
- package/build/configure.d.ts.map +1 -0
- package/build/configure.js +13 -0
- package/build/index.d.ts +5 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +4 -0
- package/build/providers/vite_provider.d.ts +10 -0
- package/build/providers/vite_provider.d.ts.map +1 -0
- package/build/providers/vite_provider.js +48 -0
- package/build/services/vite.d.ts +4 -0
- package/build/services/vite.d.ts.map +1 -0
- package/build/services/vite.js +6 -0
- package/build/src/backend/types/extended.d.ts +7 -0
- package/build/src/backend/types/extended.d.ts.map +1 -0
- package/build/src/backend/types/extended.js +1 -0
- package/build/src/backend/types/main.d.ts +9 -0
- package/build/src/backend/types/main.d.ts.map +1 -0
- package/build/src/backend/types/main.js +1 -0
- package/build/src/backend/utils.d.ts +2 -0
- package/build/src/backend/utils.d.ts.map +1 -0
- package/build/src/backend/utils.js +7 -0
- package/build/src/backend/vite.d.ts +19 -0
- package/build/src/backend/vite.d.ts.map +1 -0
- package/build/src/backend/vite.js +182 -0
- package/build/src/vite_plugin/config.d.ts +5 -0
- package/build/src/vite_plugin/config.d.ts.map +1 -0
- package/build/src/vite_plugin/config.js +47 -0
- package/build/src/vite_plugin/config_resolver.d.ts +9 -0
- package/build/src/vite_plugin/config_resolver.d.ts.map +1 -0
- package/build/src/vite_plugin/config_resolver.js +27 -0
- package/build/src/vite_plugin/helpers/inertia.d.ts +2 -0
- package/build/src/vite_plugin/helpers/inertia.d.ts.map +1 -0
- package/build/src/vite_plugin/helpers/inertia.js +11 -0
- package/build/src/vite_plugin/hot_file.d.ts +9 -0
- package/build/src/vite_plugin/hot_file.d.ts.map +1 -0
- package/build/src/vite_plugin/hot_file.js +30 -0
- package/build/src/vite_plugin/index.d.ts +4 -0
- package/build/src/vite_plugin/index.d.ts.map +1 -0
- package/build/src/vite_plugin/index.js +16 -0
- package/build/src/vite_plugin/types/index.d.ts +10 -0
- package/build/src/vite_plugin/types/index.d.ts.map +1 -0
- package/build/src/vite_plugin/types/index.js +1 -0
- package/build/src/vite_plugin/utils.d.ts +5 -0
- package/build/src/vite_plugin/utils.d.ts.map +1 -0
- package/build/src/vite_plugin/utils.js +16 -0
- package/build/stubs/index.d.ts +2 -0
- package/build/stubs/index.d.ts.map +1 -0
- package/build/stubs/index.js +2 -0
- package/build/stubs/vite/vite_config.stub +14 -0
- package/configure.ts +30 -0
- package/index.ts +15 -0
- package/package.json +147 -0
- package/providers/vite_provider.ts +89 -0
- package/services/vite.ts +23 -0
- package/src/backend/types/extended.ts +19 -0
- package/src/backend/types/main.ts +31 -0
- package/src/backend/utils.ts +19 -0
- package/src/backend/vite.ts +367 -0
- package/src/vite_plugin/config.ts +97 -0
- package/src/vite_plugin/config_resolver.ts +62 -0
- package/src/vite_plugin/helpers/inertia.ts +27 -0
- package/src/vite_plugin/hot_file.ts +59 -0
- package/src/vite_plugin/index.ts +38 -0
- package/src/vite_plugin/types/index.ts +56 -0
- package/src/vite_plugin/utils.ts +40 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2022 Harminder Virk, contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @adonisjs/vite
|
|
2
|
+
|
|
3
|
+
<br />
|
|
4
|
+
|
|
5
|
+
[![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url] [![snyk-image]][snyk-url]
|
|
6
|
+
|
|
7
|
+
## Introduction
|
|
8
|
+
Package to add [Vite](https://vitejs.dev/) as an assets bundler to AdonisJS.
|
|
9
|
+
|
|
10
|
+
## Official Documentation
|
|
11
|
+
The documentation is available on the [AdonisJS website](https://docs.adonisjs.com/guides/assets-bundlers/vite)
|
|
12
|
+
|
|
13
|
+
## Contributing
|
|
14
|
+
One of the primary goals of AdonisJS is to have a vibrant community of users and contributors who believes in the principles of the framework.
|
|
15
|
+
|
|
16
|
+
We encourage you to read the [contribution guide](https://github.com/adonisjs/.github/blob/main/docs/CONTRIBUTING.md) before contributing to the framework.
|
|
17
|
+
|
|
18
|
+
## Code of Conduct
|
|
19
|
+
In order to ensure that the AdonisJS community is welcoming to all, please review and abide by the [Code of Conduct](https://github.com/adonisjs/.github/blob/main/docs/CODE_OF_CONDUCT.md).
|
|
20
|
+
|
|
21
|
+
## License
|
|
22
|
+
AdonisJS static files middleware is open-sourced software licensed under the [MIT license](LICENSE.md).
|
|
23
|
+
|
|
24
|
+
[gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/adonisjs/vite/test.yml?style=for-the-badge
|
|
25
|
+
[gh-workflow-url]: https://github.com/adonisjs/vite/actions/workflows/test.yml "Github action"
|
|
26
|
+
|
|
27
|
+
[npm-image]: https://img.shields.io/npm/v/@adonisjs/vite/latest.svg?style=for-the-badge&logo=npm
|
|
28
|
+
[npm-url]: https://www.npmjs.com/package/@adonisjs/vite/v/latest "npm"
|
|
29
|
+
|
|
30
|
+
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
|
|
31
|
+
|
|
32
|
+
[license-url]: LICENSE.md
|
|
33
|
+
[license-image]: https://img.shields.io/github/license/adonisjs/vite?style=for-the-badge
|
|
34
|
+
|
|
35
|
+
[snyk-image]: https://img.shields.io/snyk/vulnerabilities/github/adonisjs/vite?label=Snyk%20Vulnerabilities&style=for-the-badge
|
|
36
|
+
[snyk-url]: https://snyk.io/test/github/adonisjs/vite?targetFile=package.json "snyk"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"configure.d.ts","sourceRoot":"","sources":["../configure.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,SAAS,MAAM,mCAAmC,CAAA;AAK9D,wBAAsB,SAAS,CAAC,OAAO,EAAE,SAAS,iBAajD"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
export async function configure(command) {
|
|
4
|
+
const stubDestination = join(fileURLToPath(command.app.appRoot), 'vite.config.js');
|
|
5
|
+
await command.publishStub('vite/vite_config.stub', {
|
|
6
|
+
destination: stubDestination,
|
|
7
|
+
});
|
|
8
|
+
await command.updateRcFile((rcFile) => {
|
|
9
|
+
rcFile.addProvider('@adonisjs/vite/providers/vite_provider');
|
|
10
|
+
});
|
|
11
|
+
const packagesToInstall = [{ name: 'vite', isDevDependency: true }];
|
|
12
|
+
command.listPackagesToInstall(packagesToInstall);
|
|
13
|
+
}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AASA,OAAO,iCAAiC,CAAA;AAExC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAE5C,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAA"}
|
package/build/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import '@adonisjs/view';
|
|
2
|
+
import { ApplicationService } from '@adonisjs/core/types';
|
|
3
|
+
export default class ViteServiceProvider {
|
|
4
|
+
#private;
|
|
5
|
+
protected app: ApplicationService;
|
|
6
|
+
constructor(app: ApplicationService);
|
|
7
|
+
register(): void;
|
|
8
|
+
boot(): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=vite_provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite_provider.d.ts","sourceRoot":"","sources":["../../providers/vite_provider.ts"],"names":[],"mappings":"AASA,OAAO,gBAAgB,CAAA;AACvB,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AAKzD,MAAM,CAAC,OAAO,OAAO,mBAAmB;;IAC1B,SAAS,CAAC,GAAG,EAAE,kBAAkB;gBAAvB,GAAG,EAAE,kBAAkB;IA2D7C,QAAQ;IAIF,IAAI;CASX"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import '@adonisjs/view';
|
|
2
|
+
import { Vite } from '../src/backend/vite.js';
|
|
3
|
+
import { EdgeError } from 'edge-error';
|
|
4
|
+
export default class ViteServiceProvider {
|
|
5
|
+
app;
|
|
6
|
+
constructor(app) {
|
|
7
|
+
this.app = app;
|
|
8
|
+
}
|
|
9
|
+
#registerViteTag(view) {
|
|
10
|
+
view.registerTag({
|
|
11
|
+
tagName: 'vite',
|
|
12
|
+
seekable: true,
|
|
13
|
+
block: false,
|
|
14
|
+
compile(parser, buffer, token) {
|
|
15
|
+
if (!token.properties.jsArg.trim()) {
|
|
16
|
+
throw new EdgeError('Missing entrypoint name', 'E_RUNTIME_EXCEPTION', {
|
|
17
|
+
filename: token.filename,
|
|
18
|
+
line: token.loc.start.line,
|
|
19
|
+
col: token.loc.start.col,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
const parsed = parser.utils.transformAst(parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename), token.filename, parser);
|
|
23
|
+
const entrypoints = parser.utils.stringify(parsed);
|
|
24
|
+
buffer.outputExpression(`state.vite.generateEntryPointsTags(${entrypoints})`, token.filename, token.loc.start.line, false);
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
#registerViteReactTag(view) {
|
|
29
|
+
view.registerTag({
|
|
30
|
+
tagName: 'viteReactRefresh',
|
|
31
|
+
seekable: true,
|
|
32
|
+
block: false,
|
|
33
|
+
compile(_parser, buffer, token) {
|
|
34
|
+
buffer.outputExpression(`state.vite.getReactHmrScript()`, token.filename, token.loc.start.line, false);
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
register() {
|
|
39
|
+
this.app.container.singleton('vite', async () => new Vite(this.app));
|
|
40
|
+
}
|
|
41
|
+
async boot() {
|
|
42
|
+
const view = await this.app.container.make('view');
|
|
43
|
+
const vite = await this.app.container.make('vite');
|
|
44
|
+
view.global('vite', vite);
|
|
45
|
+
this.#registerViteTag(view);
|
|
46
|
+
this.#registerViteReactTag(view);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite.d.ts","sourceRoot":"","sources":["../../services/vite.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAElC,QAAA,IAAI,IAAI,EAAE,IAAI,CAAA;AAUd,OAAO,EAAE,IAAI,IAAI,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extended.d.ts","sourceRoot":"","sources":["../../../../src/backend/types/extended.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAA;AAKjC,OAAO,QAAQ,sBAAsB,CAAC;IACpC,UAAU,iBAAiB;QACzB,IAAI,EAAE,IAAI,CAAA;KACX;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type HotFile = {
|
|
2
|
+
url: string;
|
|
3
|
+
};
|
|
4
|
+
export type SetAttributesCallbackParams = {
|
|
5
|
+
src: string;
|
|
6
|
+
url: string;
|
|
7
|
+
};
|
|
8
|
+
export type SetAttributes = Record<string, string | boolean> | ((params: SetAttributesCallbackParams) => Record<string, string | boolean>);
|
|
9
|
+
//# sourceMappingURL=main.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../../../src/backend/types/main.ts"],"names":[],"mappings":"AAYA,MAAM,MAAM,OAAO,GAAG;IACpB,GAAG,EAAE,MAAM,CAAA;CACZ,CAAA;AAKD,MAAM,MAAM,2BAA2B,GAAG;IACxC,GAAG,EAAE,MAAM,CAAA;IACX,GAAG,EAAE,MAAM,CAAA;CACZ,CAAA;AAMD,MAAM,MAAM,aAAa,GACrB,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,GAChC,CAAC,CAAC,MAAM,EAAE,2BAA2B,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/backend/utils.ts"],"names":[],"mappings":"AAYA,wBAAgB,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,GAAG,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,CAMvD"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Manifest } from 'vite';
|
|
2
|
+
import { SetAttributes } from './types/main.js';
|
|
3
|
+
import { ApplicationService } from '@adonisjs/core/types';
|
|
4
|
+
export declare class Vite {
|
|
5
|
+
#private;
|
|
6
|
+
private application;
|
|
7
|
+
constructor(application: ApplicationService);
|
|
8
|
+
generateEntryPointsTags(entryPoints: string[] | string): string;
|
|
9
|
+
assetPath(asset: string): string;
|
|
10
|
+
manifest(): Manifest;
|
|
11
|
+
getReactHmrScript(): string;
|
|
12
|
+
setHotFilePath(path: string): this;
|
|
13
|
+
setManifestFilename(name: string): this;
|
|
14
|
+
setBuildDirectory(path: string): this;
|
|
15
|
+
setAssetsUrl(url: string): this;
|
|
16
|
+
setScriptAttributes(attributes: SetAttributes): this;
|
|
17
|
+
setStyleAttributes(attributes: SetAttributes): this;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=vite.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite.d.ts","sourceRoot":"","sources":["../../../src/backend/vite.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAA;AAEpC,OAAO,EAAW,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAExD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AAGzD,qBAAa,IAAI;;IAyCH,OAAO,CAAC,WAAW;gBAAX,WAAW,EAAE,kBAAkB;IAkFnD,uBAAuB,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,MAAM,GAAG,MAAM;IAgH/D,SAAS,CAAC,KAAK,EAAE,MAAM;IAcvB,QAAQ,IAAI,QAAQ;IA6BpB,iBAAiB,IAAI,MAAM;IAqB3B,cAAc,CAAC,IAAI,EAAE,MAAM;IAU3B,mBAAmB,CAAC,IAAI,EAAE,MAAM;IAUhC,iBAAiB,CAAC,IAAI,EAAE,MAAM;IAU9B,YAAY,CAAC,GAAG,EAAE,MAAM;IAQxB,mBAAmB,CAAC,UAAU,EAAE,aAAa;IAQ7C,kBAAkB,CAAC,UAAU,EAAE,aAAa;CAI7C"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
4
|
+
import { uniqBy } from './utils.js';
|
|
5
|
+
export class Vite {
|
|
6
|
+
application;
|
|
7
|
+
#buildDirectory;
|
|
8
|
+
#hotFile;
|
|
9
|
+
#manifestFilename = 'manifest.json';
|
|
10
|
+
#assetsUrl = '';
|
|
11
|
+
#manifestCache = null;
|
|
12
|
+
#styleAttributes = {};
|
|
13
|
+
#scriptAttributes = {};
|
|
14
|
+
constructor(application) {
|
|
15
|
+
this.application = application;
|
|
16
|
+
this.#buildDirectory = this.application.publicPath('assets');
|
|
17
|
+
this.#hotFile = join(this.#buildDirectory, 'hot.json');
|
|
18
|
+
}
|
|
19
|
+
#isRunningHot() {
|
|
20
|
+
return existsSync(this.#hotFile);
|
|
21
|
+
}
|
|
22
|
+
#readFileAsJSON(filePath) {
|
|
23
|
+
return JSON.parse(readFileSync(filePath, 'utf-8'));
|
|
24
|
+
}
|
|
25
|
+
#readHotFile() {
|
|
26
|
+
return this.#readFileAsJSON(this.#hotFile);
|
|
27
|
+
}
|
|
28
|
+
#hotAsset(asset) {
|
|
29
|
+
return this.#readHotFile().url + '/' + asset;
|
|
30
|
+
}
|
|
31
|
+
#getViteHmrScript() {
|
|
32
|
+
if (!this.#isRunningHot()) {
|
|
33
|
+
return '';
|
|
34
|
+
}
|
|
35
|
+
return `<script type="module" src="${this.#hotAsset('@vite/client')}"></script>`;
|
|
36
|
+
}
|
|
37
|
+
#generateEntryPointsTagsForHotmode(entryPoints) {
|
|
38
|
+
const viteHmr = this.#getViteHmrScript();
|
|
39
|
+
const tags = entryPoints.map((entrypoint) => this.#generateTag(entrypoint));
|
|
40
|
+
return [viteHmr, ...tags].join('\n');
|
|
41
|
+
}
|
|
42
|
+
#generateEntryPointsTagsWithManifest(entryPoints) {
|
|
43
|
+
const manifest = this.manifest();
|
|
44
|
+
const tags = [];
|
|
45
|
+
for (const entryPoint of entryPoints) {
|
|
46
|
+
const chunk = this.#chunk(manifest, entryPoint);
|
|
47
|
+
tags.push({ path: chunk.file, tag: this.#generateTag(chunk.file) });
|
|
48
|
+
for (const css of chunk.css || []) {
|
|
49
|
+
const cssChunk = this.#chunkByFile(manifest, css);
|
|
50
|
+
tags.push({ path: cssChunk.file, tag: this.#generateTag(cssChunk.file) });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return uniqBy(tags, 'path')
|
|
54
|
+
.sort((a) => (a.path.endsWith('.css') ? -1 : 1))
|
|
55
|
+
.map((preload) => preload.tag)
|
|
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);
|
|
64
|
+
}
|
|
65
|
+
#chunk(manifest, fileName) {
|
|
66
|
+
const chunk = manifest[fileName];
|
|
67
|
+
if (!chunk) {
|
|
68
|
+
throw new Error(`Cannot find "${fileName}" chunk in the manifest file`);
|
|
69
|
+
}
|
|
70
|
+
return chunk;
|
|
71
|
+
}
|
|
72
|
+
#chunkByFile(manifest, fileName) {
|
|
73
|
+
const chunk = Object.values(manifest).find((c) => c.file === fileName);
|
|
74
|
+
if (!chunk) {
|
|
75
|
+
throw new Error(`Cannot find "${fileName}" chunk in the manifest file`);
|
|
76
|
+
}
|
|
77
|
+
return chunk;
|
|
78
|
+
}
|
|
79
|
+
#isCssPath(path) {
|
|
80
|
+
return path.match(/\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/) !== null;
|
|
81
|
+
}
|
|
82
|
+
#generateTag(asset) {
|
|
83
|
+
let url = '';
|
|
84
|
+
if (this.#isRunningHot()) {
|
|
85
|
+
url = this.#hotAsset(asset);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
url = `${this.#assetsUrl}/assets/${asset}`;
|
|
89
|
+
}
|
|
90
|
+
if (this.#isCssPath(asset)) {
|
|
91
|
+
return this.#makeStyleTag(asset, url);
|
|
92
|
+
}
|
|
93
|
+
return this.#makeScriptTag(asset, url);
|
|
94
|
+
}
|
|
95
|
+
#unwrapAttributes(src, url, attributes) {
|
|
96
|
+
if (typeof attributes === 'function') {
|
|
97
|
+
return attributes({ src, url });
|
|
98
|
+
}
|
|
99
|
+
return attributes;
|
|
100
|
+
}
|
|
101
|
+
#makeAttributes(attributes) {
|
|
102
|
+
return Object.keys(attributes)
|
|
103
|
+
.map((key) => {
|
|
104
|
+
const value = attributes[key];
|
|
105
|
+
if (value === true)
|
|
106
|
+
return key;
|
|
107
|
+
if (value === false)
|
|
108
|
+
return null;
|
|
109
|
+
return `${key}="${value}"`;
|
|
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}">`;
|
|
123
|
+
}
|
|
124
|
+
assetPath(asset) {
|
|
125
|
+
if (this.#isRunningHot()) {
|
|
126
|
+
return this.#hotAsset(asset);
|
|
127
|
+
}
|
|
128
|
+
const chunk = this.#chunk(this.manifest(), asset);
|
|
129
|
+
return `${this.#assetsUrl}/assets/${chunk.file}`;
|
|
130
|
+
}
|
|
131
|
+
manifest() {
|
|
132
|
+
if (this.#isRunningHot()) {
|
|
133
|
+
throw new Error('Cannot read the manifest file when running in hot mode');
|
|
134
|
+
}
|
|
135
|
+
if (this.#manifestCache) {
|
|
136
|
+
return this.#manifestCache;
|
|
137
|
+
}
|
|
138
|
+
const manifest = this.#readFileAsJSON(join(this.#buildDirectory, this.#manifestFilename));
|
|
139
|
+
if (this.application.inProduction) {
|
|
140
|
+
this.#manifestCache = manifest;
|
|
141
|
+
}
|
|
142
|
+
return manifest;
|
|
143
|
+
}
|
|
144
|
+
getReactHmrScript() {
|
|
145
|
+
if (!this.#isRunningHot()) {
|
|
146
|
+
return '';
|
|
147
|
+
}
|
|
148
|
+
return `
|
|
149
|
+
<script type="module">
|
|
150
|
+
import RefreshRuntime from '${this.#hotAsset('@react-refresh')}'
|
|
151
|
+
RefreshRuntime.injectIntoGlobalHook(window)
|
|
152
|
+
window.$RefreshReg$ = () => {}
|
|
153
|
+
window.$RefreshSig$ = () => (type) => type
|
|
154
|
+
window.__vite_plugin_react_preamble_installed__ = true
|
|
155
|
+
</script>
|
|
156
|
+
`;
|
|
157
|
+
}
|
|
158
|
+
setHotFilePath(path) {
|
|
159
|
+
this.#hotFile = join(fileURLToPath(this.application.appRoot), path);
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
setManifestFilename(name) {
|
|
163
|
+
this.#manifestFilename = name;
|
|
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;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ConfigEnv, Plugin, UserConfig } from 'vite';
|
|
2
|
+
import { PluginFullOptions } from './types/index.js';
|
|
3
|
+
export declare const configHook: (options: PluginFullOptions, userConfig: UserConfig, { command }: ConfigEnv) => UserConfig;
|
|
4
|
+
export declare const config: (options: PluginFullOptions) => Plugin;
|
|
5
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../src/vite_plugin/config.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,MAAM,CAAA;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AASpD,eAAO,MAAM,UAAU,YACZ,iBAAiB,cACd,UAAU,eACT,SAAS,KACrB,UA2BF,CAAA;AAKD,eAAO,MAAM,MAAM,YAAa,iBAAiB,KAAG,MAuCnD,CAAA"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { defu } from 'defu';
|
|
2
|
+
import { resolveDevServerUrl } from './utils.js';
|
|
3
|
+
import { HotFile } from './hot_file.js';
|
|
4
|
+
import { ConfigResolver } from './config_resolver.js';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
export const configHook = (options, userConfig, { command }) => {
|
|
7
|
+
const config = {
|
|
8
|
+
publicDir: userConfig.publicDir ?? false,
|
|
9
|
+
base: ConfigResolver.resolveBase(userConfig, options, command),
|
|
10
|
+
resolve: { alias: ConfigResolver.resolveAlias(userConfig) },
|
|
11
|
+
server: {
|
|
12
|
+
origin: '__adonis_vite__',
|
|
13
|
+
},
|
|
14
|
+
build: {
|
|
15
|
+
assetsDir: '',
|
|
16
|
+
manifest: userConfig.build?.manifest ?? true,
|
|
17
|
+
emptyOutDir: true,
|
|
18
|
+
outDir: ConfigResolver.resolveOutDir(userConfig, options),
|
|
19
|
+
rollupOptions: {
|
|
20
|
+
input: options.entrypoints.map((entrypoint) => join(userConfig.root || '', entrypoint)),
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
return defu(config, userConfig);
|
|
25
|
+
};
|
|
26
|
+
export const config = (options) => {
|
|
27
|
+
let devServerUrl;
|
|
28
|
+
return {
|
|
29
|
+
name: 'vite-plugin-adonis:config',
|
|
30
|
+
config: configHook.bind(null, options),
|
|
31
|
+
configureServer(server) {
|
|
32
|
+
const hotfile = new HotFile(options.hotFile);
|
|
33
|
+
server.httpServer?.once('listening', async () => {
|
|
34
|
+
devServerUrl = resolveDevServerUrl(server.httpServer.address(), server.config);
|
|
35
|
+
await hotfile.write({ url: devServerUrl });
|
|
36
|
+
});
|
|
37
|
+
server.httpServer?.on('close', () => hotfile.clean());
|
|
38
|
+
},
|
|
39
|
+
transform: (code) => ({
|
|
40
|
+
code: code.replace(/__adonis_vite__/g, devServerUrl),
|
|
41
|
+
map: null,
|
|
42
|
+
}),
|
|
43
|
+
configResolved: async (resolvedConfig) => {
|
|
44
|
+
ConfigResolver.resolvedConfig = resolvedConfig;
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ResolvedConfig, UserConfig, AliasOptions } from 'vite';
|
|
2
|
+
import { PluginFullOptions } from './types/index.js';
|
|
3
|
+
export declare class ConfigResolver {
|
|
4
|
+
static resolvedConfig?: ResolvedConfig;
|
|
5
|
+
static resolveBase(config: UserConfig, options: PluginFullOptions, command: 'build' | 'serve'): string;
|
|
6
|
+
static resolveAlias(config: UserConfig): AliasOptions;
|
|
7
|
+
static resolveOutDir(config: UserConfig, options: PluginFullOptions): string;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=config_resolver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config_resolver.d.ts","sourceRoot":"","sources":["../../../src/vite_plugin/config_resolver.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AAE/D,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAGpD,qBAAa,cAAc;IACzB,MAAM,CAAC,cAAc,CAAC,EAAE,cAAc,CAAA;IAKtC,MAAM,CAAC,WAAW,CAChB,MAAM,EAAE,UAAU,EAClB,OAAO,EAAE,iBAAiB,EAC1B,OAAO,EAAE,OAAO,GAAG,OAAO,GACzB,MAAM;IAkBT,MAAM,CAAC,YAAY,CAAC,MAAM,EAAE,UAAU,GAAG,YAAY;IAgBrD,MAAM,CAAC,aAAa,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,iBAAiB,GAAG,MAAM;CAG7E"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { addTrailingSlash } from './utils.js';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
export class ConfigResolver {
|
|
4
|
+
static resolvedConfig;
|
|
5
|
+
static resolveBase(config, options, command) {
|
|
6
|
+
if (config.base) {
|
|
7
|
+
return config.base;
|
|
8
|
+
}
|
|
9
|
+
if (command === 'build') {
|
|
10
|
+
return addTrailingSlash(options.assetsUrl) + addTrailingSlash(options.buildDirectory);
|
|
11
|
+
}
|
|
12
|
+
return '/';
|
|
13
|
+
}
|
|
14
|
+
static resolveAlias(config) {
|
|
15
|
+
const defaultAlias = { '@/': `/resources/js/` };
|
|
16
|
+
if (Array.isArray(config.resolve?.alias)) {
|
|
17
|
+
return [
|
|
18
|
+
...(config.resolve?.alias ?? []),
|
|
19
|
+
Object.entries(defaultAlias).map(([find, replacement]) => ({ find, replacement })),
|
|
20
|
+
];
|
|
21
|
+
}
|
|
22
|
+
return { ...defaultAlias, ...config.resolve?.alias };
|
|
23
|
+
}
|
|
24
|
+
static resolveOutDir(config, options) {
|
|
25
|
+
return config.build?.outDir ?? join(options.publicDirectory, options.buildDirectory);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inertia.d.ts","sourceRoot":"","sources":["../../../../src/vite_plugin/helpers/inertia.ts"],"names":[],"mappings":"AAYA,wBAAsB,oBAAoB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,gBAclF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export async function resolvePageComponent(name, pages) {
|
|
2
|
+
const path = Object.keys(pages)
|
|
3
|
+
.sort((a, b) => a.length - b.length)
|
|
4
|
+
.find((filepath) => filepath.endsWith(name));
|
|
5
|
+
if (!path) {
|
|
6
|
+
throw new Error(`Page component "${name}" could not be found.`);
|
|
7
|
+
}
|
|
8
|
+
let component = typeof pages[path] === 'function' ? await pages[path]() : pages[path];
|
|
9
|
+
component = component.default ?? component;
|
|
10
|
+
return component;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hot_file.d.ts","sourceRoot":"","sources":["../../../src/vite_plugin/hot_file.ts"],"names":[],"mappings":"AAcA,qBAAa,OAAO;;gBAqBN,IAAI,EAAE,MAAM;IAQlB,KAAK,CAAC,IAAI,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE;IAQjC,KAAK;CAON"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { existsSync, rmSync } from 'node:fs';
|
|
2
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
import { ConfigResolver } from './config_resolver.js';
|
|
5
|
+
export class HotFile {
|
|
6
|
+
#path;
|
|
7
|
+
#cleanHotFileOnExit() {
|
|
8
|
+
const clean = this.clean.bind(this);
|
|
9
|
+
process.on('exit', clean);
|
|
10
|
+
process.on('SIGINT', process.exit);
|
|
11
|
+
process.on('SIGTERM', process.exit);
|
|
12
|
+
process.on('SIGHUP', process.exit);
|
|
13
|
+
process.on('SIGBREAK', process.exit);
|
|
14
|
+
process.on('SIGKILL', process.exit);
|
|
15
|
+
}
|
|
16
|
+
constructor(path) {
|
|
17
|
+
this.#path = join(ConfigResolver.resolvedConfig.root, path);
|
|
18
|
+
this.#cleanHotFileOnExit();
|
|
19
|
+
}
|
|
20
|
+
async write(data) {
|
|
21
|
+
await mkdir(dirname(this.#path), { recursive: true });
|
|
22
|
+
await writeFile(this.#path, JSON.stringify(data, null, 2));
|
|
23
|
+
}
|
|
24
|
+
clean() {
|
|
25
|
+
if (!existsSync(this.#path)) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
rmSync(this.#path);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/vite_plugin/index.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AAEnC,OAAO,EAAqB,aAAa,EAAE,MAAM,kBAAkB,CAAA;AASnE,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,YAAY,EAAE,CAgBrE"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { defu } from 'defu';
|
|
2
|
+
import { config } from './config.js';
|
|
3
|
+
import PluginRestart from 'vite-plugin-restart';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
const VitePluginRestart = PluginRestart;
|
|
6
|
+
export default function Adonis(options) {
|
|
7
|
+
const hotfileDefaultDestination = join(options.publicDirectory || 'public', options.buildDirectory || 'assets', 'hot.json');
|
|
8
|
+
const fullOptions = defu(options, {
|
|
9
|
+
publicDirectory: 'public',
|
|
10
|
+
buildDirectory: 'assets',
|
|
11
|
+
assetsUrl: '',
|
|
12
|
+
hotFile: hotfileDefaultDestination,
|
|
13
|
+
reload: ['./resources/views/**/*.edge'],
|
|
14
|
+
});
|
|
15
|
+
return [VitePluginRestart({ reload: fullOptions.reload }), config(fullOptions)];
|
|
16
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type PluginOptions = {
|
|
2
|
+
hotFile?: string;
|
|
3
|
+
entrypoints: string[];
|
|
4
|
+
publicDirectory?: string;
|
|
5
|
+
assetsUrl?: string;
|
|
6
|
+
buildDirectory?: string;
|
|
7
|
+
reload?: string[];
|
|
8
|
+
};
|
|
9
|
+
export type PluginFullOptions = Required<PluginOptions>;
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/vite_plugin/types/index.ts"],"names":[],"mappings":"AAYA,MAAM,MAAM,aAAa,GAAG;IAM1B,OAAO,CAAC,EAAE,MAAM,CAAA;IAKhB,WAAW,EAAE,MAAM,EAAE,CAAA;IAOrB,eAAe,CAAC,EAAE,MAAM,CAAA;IAQxB,SAAS,CAAC,EAAE,MAAM,CAAA;IAOlB,cAAc,CAAC,EAAE,MAAM,CAAA;IAOvB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAA;CAClB,CAAA;AAED,MAAM,MAAM,iBAAiB,GAAG,QAAQ,CAAC,aAAa,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|