@adonisjs/vite 3.0.0-0 → 3.0.0-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.
- package/build/index.js +47 -12
- package/build/providers/vite_provider.js +49 -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/main.js +67 -20
- package/build/src/client/main.js.map +1 -0
- package/build/src/hooks/build_hook.js +15 -24
- package/build/src/hooks/build_hook.js.map +1 -0
- package/build/src/types.js +1 -9
- package/build/src/types.js.map +1 -0
- package/package.json +24 -7
- 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/plugins/edge.js +0 -84
- package/build/src/utils.js +0 -42
- package/build/src/vite.js +0 -261
- package/build/stubs/main.js +0 -10
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-J6JUW6YH.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
|
|
@@ -1,49 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
app;
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import {
|
|
2
|
+
Vite
|
|
3
|
+
} from "../chunk-J6JUW6YH.js";
|
|
4
|
+
import "../chunk-CFRBPZ4N.js";
|
|
5
|
+
import {
|
|
6
|
+
ViteMiddleware
|
|
7
|
+
} from "../chunk-YCD5RGKQ.js";
|
|
8
|
+
|
|
9
|
+
// providers/vite_provider.ts
|
|
10
|
+
var ViteProvider = class {
|
|
11
|
+
constructor(app) {
|
|
12
|
+
this.app = app;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Registers edge plugin when edge is installed
|
|
16
|
+
*/
|
|
17
|
+
async registerEdgePlugin() {
|
|
18
|
+
if (this.app.usingEdgeJS) {
|
|
19
|
+
const edge = await import("edge.js");
|
|
20
|
+
const vite = await this.app.container.make("vite");
|
|
21
|
+
const { edgePluginVite } = await import("../edge-SDXKV2NF.js");
|
|
22
|
+
edge.default.use(edgePluginVite(vite));
|
|
15
23
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
24
|
+
}
|
|
25
|
+
async register() {
|
|
26
|
+
const config = this.app.config.get("vite");
|
|
27
|
+
const vite = new Vite(this.app.inDev, config);
|
|
28
|
+
this.app.container.bind("vite", () => vite);
|
|
29
|
+
this.app.container.bind(ViteMiddleware, () => new ViteMiddleware(vite));
|
|
30
|
+
if (this.app.inDev) {
|
|
31
|
+
const server = await this.app.container.make("server");
|
|
32
|
+
server.use([() => import("../vite_middleware-HYLIJP2B.js")]);
|
|
26
33
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return;
|
|
46
|
-
const vite = await this.app.container.make('vite');
|
|
47
|
-
await vite.stopDevServer();
|
|
48
|
-
}
|
|
49
|
-
}
|
|
34
|
+
}
|
|
35
|
+
async boot() {
|
|
36
|
+
if (!this.app.inDev)
|
|
37
|
+
return;
|
|
38
|
+
const vite = await this.app.container.make("vite");
|
|
39
|
+
await Promise.all([vite.createDevServer(), this.registerEdgePlugin()]);
|
|
40
|
+
}
|
|
41
|
+
async shutdown() {
|
|
42
|
+
if (!this.app.inDev)
|
|
43
|
+
return;
|
|
44
|
+
const vite = await this.app.container.make("vite");
|
|
45
|
+
await vite.stopDevServer();
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
export {
|
|
49
|
+
ViteProvider as default
|
|
50
|
+
};
|
|
51
|
+
//# sourceMappingURL=vite_provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../providers/vite_provider.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 { ApplicationService } from '@adonisjs/core/types'\n\nimport { Vite } from '../src/vite.js'\nimport type { ViteOptions } from '../src/types.js'\nimport ViteMiddleware from '../src/middlewares/vite_middleware.js'\n\ndeclare module '@adonisjs/core/types' {\n interface ContainerBindings {\n vite: Vite\n }\n}\n\nexport default class ViteProvider {\n constructor(protected app: ApplicationService) {}\n\n /**\n * Registers edge plugin when edge is installed\n */\n protected async registerEdgePlugin() {\n if (this.app.usingEdgeJS) {\n const edge = await import('edge.js')\n const vite = await this.app.container.make('vite')\n const { edgePluginVite } = await import('../src/plugins/edge.js')\n edge.default.use(edgePluginVite(vite))\n }\n }\n\n async register() {\n const config = this.app.config.get<ViteOptions>('vite')\n\n const vite = new Vite(this.app.inDev, config)\n this.app.container.bind('vite', () => vite)\n this.app.container.bind(ViteMiddleware, () => new ViteMiddleware(vite))\n\n if (this.app.inDev) {\n const server = await this.app.container.make('server')\n server.use([() => import('../src/middlewares/vite_middleware.js')])\n }\n }\n\n async boot() {\n if (!this.app.inDev) return\n\n const vite = await this.app.container.make('vite')\n await Promise.all([vite.createDevServer(), this.registerEdgePlugin()])\n }\n\n async shutdown() {\n if (!this.app.inDev) return\n\n const vite = await this.app.container.make('vite')\n await vite.stopDevServer()\n }\n}\n"],"mappings":";;;;;;;;;AAqBA,IAAqB,eAArB,MAAkC;AAAA,EAChC,YAAsB,KAAyB;AAAzB;AAAA,EAA0B;AAAA;AAAA;AAAA;AAAA,EAKhD,MAAgB,qBAAqB;AACnC,QAAI,KAAK,IAAI,aAAa;AACxB,YAAM,OAAO,MAAM,OAAO,SAAS;AACnC,YAAM,OAAO,MAAM,KAAK,IAAI,UAAU,KAAK,MAAM;AACjD,YAAM,EAAE,eAAe,IAAI,MAAM,OAAO,qBAAwB;AAChE,WAAK,QAAQ,IAAI,eAAe,IAAI,CAAC;AAAA,IACvC;AAAA,EACF;AAAA,EAEA,MAAM,WAAW;AACf,UAAM,SAAS,KAAK,IAAI,OAAO,IAAiB,MAAM;AAEtD,UAAM,OAAO,IAAI,KAAK,KAAK,IAAI,OAAO,MAAM;AAC5C,SAAK,IAAI,UAAU,KAAK,QAAQ,MAAM,IAAI;AAC1C,SAAK,IAAI,UAAU,KAAK,gBAAgB,MAAM,IAAI,eAAe,IAAI,CAAC;AAEtE,QAAI,KAAK,IAAI,OAAO;AAClB,YAAM,SAAS,MAAM,KAAK,IAAI,UAAU,KAAK,QAAQ;AACrD,aAAO,IAAI,CAAC,MAAM,OAAO,gCAAuC,CAAC,CAAC;AAAA,IACpE;AAAA,EACF;AAAA,EAEA,MAAM,OAAO;AACX,QAAI,CAAC,KAAK,IAAI;AAAO;AAErB,UAAM,OAAO,MAAM,KAAK,IAAI,UAAU,KAAK,MAAM;AACjD,UAAM,QAAQ,IAAI,CAAC,KAAK,gBAAgB,GAAG,KAAK,mBAAmB,CAAC,CAAC;AAAA,EACvE;AAAA,EAEA,MAAM,WAAW;AACf,QAAI,CAAC,KAAK,IAAI;AAAO;AAErB,UAAM,OAAO,MAAM,KAAK,IAAI,UAAU,KAAK,MAAM;AACjD,UAAM,KAAK,cAAc;AAAA,EAC3B;AACF;","names":[]}
|
package/build/services/vite.js
CHANGED
|
@@ -1,18 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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 app from '@adonisjs/core/services/app';
|
|
10
|
-
let vite;
|
|
11
|
-
/**
|
|
12
|
-
* Returns a singleton instance of Vite class
|
|
13
|
-
* from the container
|
|
14
|
-
*/
|
|
1
|
+
// services/vite.ts
|
|
2
|
+
import app from "@adonisjs/core/services/app";
|
|
3
|
+
var vite;
|
|
15
4
|
await app.booted(async () => {
|
|
16
|
-
|
|
5
|
+
vite = await app.container.make("vite");
|
|
17
6
|
});
|
|
18
|
-
export {
|
|
7
|
+
export {
|
|
8
|
+
vite as default
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=vite.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../services/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 app from '@adonisjs/core/services/app'\nimport type { Vite } from '../src/vite.js'\n\nlet vite: Vite\n\n/**\n * Returns a singleton instance of Vite class\n * from the container\n */\nawait app.booted(async () => {\n vite = await app.container.make('vite')\n})\n\nexport { vite as default }\n"],"mappings":";AASA,OAAO,SAAS;AAGhB,IAAI;AAMJ,MAAM,IAAI,OAAO,YAAY;AAC3B,SAAO,MAAM,IAAI,UAAU,KAAK,MAAM;AACxC,CAAC;","names":[]}
|
package/build/src/client/main.js
CHANGED
|
@@ -1,21 +1,68 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}, options);
|
|
20
|
-
return [PluginRestart({ reload: fullOptions.reload }), config(fullOptions)];
|
|
1
|
+
import {
|
|
2
|
+
addTrailingSlash
|
|
3
|
+
} from "../../chunk-CFRBPZ4N.js";
|
|
4
|
+
|
|
5
|
+
// src/client/main.ts
|
|
6
|
+
import PluginRestart from "vite-plugin-restart";
|
|
7
|
+
|
|
8
|
+
// src/client/config.ts
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
function resolveAlias(config2) {
|
|
11
|
+
const defaultAlias = { "@/": `/resources/js/` };
|
|
12
|
+
if (Array.isArray(config2.resolve?.alias)) {
|
|
13
|
+
return [
|
|
14
|
+
...config2.resolve?.alias ?? [],
|
|
15
|
+
Object.entries(defaultAlias).map(([find, replacement]) => ({ find, replacement }))
|
|
16
|
+
];
|
|
17
|
+
}
|
|
18
|
+
return { ...defaultAlias, ...config2.resolve?.alias };
|
|
21
19
|
}
|
|
20
|
+
function resolveBase(config2, options, command) {
|
|
21
|
+
if (config2.base)
|
|
22
|
+
return config2.base;
|
|
23
|
+
if (command === "build") {
|
|
24
|
+
return addTrailingSlash(options.assetsUrl);
|
|
25
|
+
}
|
|
26
|
+
return "/";
|
|
27
|
+
}
|
|
28
|
+
function configHook(options, userConfig, { command }) {
|
|
29
|
+
const config2 = {
|
|
30
|
+
publicDir: userConfig.publicDir ?? false,
|
|
31
|
+
resolve: { alias: resolveAlias(userConfig) },
|
|
32
|
+
base: resolveBase(userConfig, options, command),
|
|
33
|
+
build: {
|
|
34
|
+
assetsDir: "",
|
|
35
|
+
emptyOutDir: true,
|
|
36
|
+
manifest: userConfig.build?.manifest ?? true,
|
|
37
|
+
outDir: userConfig.build?.outDir ?? options.buildDirectory,
|
|
38
|
+
assetsInlineLimit: userConfig.build?.assetsInlineLimit ?? 0,
|
|
39
|
+
rollupOptions: {
|
|
40
|
+
input: options.entrypoints.map((entrypoint) => join(userConfig.root || "", entrypoint))
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
return config2;
|
|
45
|
+
}
|
|
46
|
+
var config = (options) => {
|
|
47
|
+
return {
|
|
48
|
+
name: "vite-plugin-adonis:config",
|
|
49
|
+
config: configHook.bind(null, options)
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// src/client/main.ts
|
|
54
|
+
function adonisjs(options) {
|
|
55
|
+
const fullOptions = Object.assign(
|
|
56
|
+
{
|
|
57
|
+
assetsUrl: "/assets",
|
|
58
|
+
buildDirectory: "public/assets",
|
|
59
|
+
reload: ["./resources/views/**/*.edge"]
|
|
60
|
+
},
|
|
61
|
+
options
|
|
62
|
+
);
|
|
63
|
+
return [PluginRestart({ reload: fullOptions.reload }), config(fullOptions)];
|
|
64
|
+
}
|
|
65
|
+
export {
|
|
66
|
+
adonisjs as default
|
|
67
|
+
};
|
|
68
|
+
//# sourceMappingURL=main.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/client/main.ts","../../../src/client/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\n/// <reference types=\"@vavite/multibuild\" />\n\nimport { PluginOption } from 'vite'\nimport PluginRestart from 'vite-plugin-restart'\n\nimport { config } from './config.js'\nimport type { PluginOptions } from './types.js'\n\ndeclare module 'vite' {\n interface ManifestChunk {\n integrity: string\n }\n}\n\n/**\n * Vite plugin for AdonisJS\n */\nexport default function adonisjs(options: PluginOptions): PluginOption[] {\n const fullOptions = Object.assign(\n {\n assetsUrl: '/assets',\n buildDirectory: 'public/assets',\n reload: ['./resources/views/**/*.edge'],\n },\n options\n )\n\n return [PluginRestart({ reload: fullOptions.reload }), config(fullOptions)]\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'\nimport type { AliasOptions, ConfigEnv, Plugin, UserConfig } from 'vite'\n\nimport { addTrailingSlash } from '../utils.js'\nimport type { PluginFullOptions } from './types.js'\n\n/**\n * Resolve the `config.resolve.alias` value\n *\n * Basically we are merging the user defined alias with the\n * default alias.\n */\nexport function resolveAlias(config: UserConfig): AliasOptions {\n const defaultAlias = { '@/': `/resources/js/` }\n\n if (Array.isArray(config.resolve?.alias)) {\n return [\n ...(config.resolve?.alias ?? []),\n Object.entries(defaultAlias).map(([find, replacement]) => ({ find, replacement })),\n ]\n }\n\n return { ...defaultAlias, ...config.resolve?.alias }\n}\n\n/**\n * Resolve the `config.base` value\n */\nexport function resolveBase(\n config: UserConfig,\n options: PluginFullOptions,\n command: 'build' | 'serve'\n): string {\n if (config.base) return config.base\n if (command === 'build') {\n return addTrailingSlash(options.assetsUrl)\n }\n\n return '/'\n}\n\n/**\n * Vite config hook\n */\nexport function configHook(\n options: PluginFullOptions,\n userConfig: UserConfig,\n { command }: ConfigEnv\n): UserConfig {\n const config: UserConfig = {\n publicDir: userConfig.publicDir ?? false,\n resolve: { alias: resolveAlias(userConfig) },\n base: resolveBase(userConfig, options, command),\n\n build: {\n assetsDir: '',\n emptyOutDir: true,\n manifest: userConfig.build?.manifest ?? true,\n outDir: userConfig.build?.outDir ?? options.buildDirectory,\n assetsInlineLimit: userConfig.build?.assetsInlineLimit ?? 0,\n\n rollupOptions: {\n input: options.entrypoints.map((entrypoint) => join(userConfig.root || '', entrypoint)),\n },\n },\n }\n\n return config\n}\n\n/**\n * Update the user vite config to match the Adonis requirements\n */\nexport const config = (options: PluginFullOptions): Plugin => {\n return {\n name: 'vite-plugin-adonis:config',\n config: configHook.bind(null, options),\n }\n}\n"],"mappings":";;;;;AAYA,OAAO,mBAAmB;;;ACH1B,SAAS,YAAY;AAYd,SAAS,aAAaA,SAAkC;AAC7D,QAAM,eAAe,EAAE,MAAM,iBAAiB;AAE9C,MAAI,MAAM,QAAQA,QAAO,SAAS,KAAK,GAAG;AACxC,WAAO;AAAA,MACL,GAAIA,QAAO,SAAS,SAAS,CAAC;AAAA,MAC9B,OAAO,QAAQ,YAAY,EAAE,IAAI,CAAC,CAAC,MAAM,WAAW,OAAO,EAAE,MAAM,YAAY,EAAE;AAAA,IACnF;AAAA,EACF;AAEA,SAAO,EAAE,GAAG,cAAc,GAAGA,QAAO,SAAS,MAAM;AACrD;AAKO,SAAS,YACdA,SACA,SACA,SACQ;AACR,MAAIA,QAAO;AAAM,WAAOA,QAAO;AAC/B,MAAI,YAAY,SAAS;AACvB,WAAO,iBAAiB,QAAQ,SAAS;AAAA,EAC3C;AAEA,SAAO;AACT;AAKO,SAAS,WACd,SACA,YACA,EAAE,QAAQ,GACE;AACZ,QAAMA,UAAqB;AAAA,IACzB,WAAW,WAAW,aAAa;AAAA,IACnC,SAAS,EAAE,OAAO,aAAa,UAAU,EAAE;AAAA,IAC3C,MAAM,YAAY,YAAY,SAAS,OAAO;AAAA,IAE9C,OAAO;AAAA,MACL,WAAW;AAAA,MACX,aAAa;AAAA,MACb,UAAU,WAAW,OAAO,YAAY;AAAA,MACxC,QAAQ,WAAW,OAAO,UAAU,QAAQ;AAAA,MAC5C,mBAAmB,WAAW,OAAO,qBAAqB;AAAA,MAE1D,eAAe;AAAA,QACb,OAAO,QAAQ,YAAY,IAAI,CAAC,eAAe,KAAK,WAAW,QAAQ,IAAI,UAAU,CAAC;AAAA,MACxF;AAAA,IACF;AAAA,EACF;AAEA,SAAOA;AACT;AAKO,IAAM,SAAS,CAAC,YAAuC;AAC5D,SAAO;AAAA,IACL,MAAM;AAAA,IACN,QAAQ,WAAW,KAAK,MAAM,OAAO;AAAA,EACvC;AACF;;;AD7De,SAAR,SAA0B,SAAwC;AACvE,QAAM,cAAc,OAAO;AAAA,IACzB;AAAA,MACE,WAAW;AAAA,MACX,gBAAgB;AAAA,MAChB,QAAQ,CAAC,6BAA6B;AAAA,IACxC;AAAA,IACA;AAAA,EACF;AAEA,SAAO,CAAC,cAAc,EAAE,QAAQ,YAAY,OAAO,CAAC,GAAG,OAAO,WAAW,CAAC;AAC5E;","names":["config"]}
|
|
@@ -1,25 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* builded using the `node ace build` command.
|
|
13
|
-
*
|
|
14
|
-
* The hook is responsible for launching a Vite multi-build process.
|
|
15
|
-
*/
|
|
16
|
-
export default async function viteBuildHook({ logger }) {
|
|
17
|
-
logger.info('building assets with vite');
|
|
18
|
-
await multibuild(undefined, {
|
|
19
|
-
onStartBuildStep: (step) => {
|
|
20
|
-
if (!step.currentStep.description)
|
|
21
|
-
return;
|
|
22
|
-
logger.info(step.currentStep.description);
|
|
23
|
-
},
|
|
24
|
-
});
|
|
1
|
+
// src/hooks/build_hook.ts
|
|
2
|
+
import { multibuild } from "@vavite/multibuild";
|
|
3
|
+
async function viteBuildHook({ logger }) {
|
|
4
|
+
logger.info("building assets with vite");
|
|
5
|
+
await multibuild(void 0, {
|
|
6
|
+
onStartBuildStep: (step) => {
|
|
7
|
+
if (!step.currentStep.description)
|
|
8
|
+
return;
|
|
9
|
+
logger.info(step.currentStep.description);
|
|
10
|
+
}
|
|
11
|
+
});
|
|
25
12
|
}
|
|
13
|
+
export {
|
|
14
|
+
viteBuildHook as default
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=build_hook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/hooks/build_hook.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 { multibuild } from '@vavite/multibuild'\nimport type { AssemblerHookHandler } from '@adonisjs/application/types'\n\n/**\n * This is an Assembler hook that should be executed when the application is\n * builded using the `node ace build` command.\n *\n * The hook is responsible for launching a Vite multi-build process.\n */\nexport default async function viteBuildHook({ logger }: Parameters<AssemblerHookHandler>[0]) {\n logger.info('building assets with vite')\n\n await multibuild(undefined, {\n onStartBuildStep: (step) => {\n if (!step.currentStep.description) return\n\n logger.info(step.currentStep.description)\n },\n })\n}\n"],"mappings":";AASA,SAAS,kBAAkB;AAS3B,eAAO,cAAqC,EAAE,OAAO,GAAwC;AAC3F,SAAO,KAAK,2BAA2B;AAEvC,QAAM,WAAW,QAAW;AAAA,IAC1B,kBAAkB,CAAC,SAAS;AAC1B,UAAI,CAAC,KAAK,YAAY;AAAa;AAEnC,aAAO,KAAK,KAAK,YAAY,WAAW;AAAA,IAC1C;AAAA,EACF,CAAC;AACH;","names":[]}
|
package/build/src/types.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonisjs/vite",
|
|
3
3
|
"description": "Vite plugin for AdonisJS",
|
|
4
|
-
"version": "3.0.0-
|
|
4
|
+
"version": "3.0.0-1",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=20.6.0"
|
|
7
7
|
},
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"./services/main": "./build/services/vite.js",
|
|
24
24
|
"./types": "./build/src/types.js",
|
|
25
25
|
"./client": "./build/src/client/main.js",
|
|
26
|
-
"./build_hook": "./build/src/build_hook.js"
|
|
26
|
+
"./build_hook": "./build/src/hooks/build_hook.js"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
29
|
"clean": "del-cli build",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"pretest": "npm run lint",
|
|
36
36
|
"test": "c8 npm run quick:test",
|
|
37
37
|
"prebuild": "npm run lint && npm run clean",
|
|
38
|
-
"build": "tsc",
|
|
38
|
+
"build": "tsup-node && tsc --emitDeclarationOnly --declaration",
|
|
39
39
|
"postbuild": "npm run copy:templates",
|
|
40
40
|
"release": "np",
|
|
41
41
|
"version": "npm run build",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@adonisjs/application": "8.1.0",
|
|
46
|
-
"@adonisjs/assembler": "^7.2.
|
|
47
|
-
"@adonisjs/core": "6.3.
|
|
46
|
+
"@adonisjs/assembler": "^7.2.2",
|
|
47
|
+
"@adonisjs/core": "6.3.1",
|
|
48
48
|
"@adonisjs/eslint-config": "^1.2.1",
|
|
49
49
|
"@adonisjs/prettier-config": "^1.2.1",
|
|
50
50
|
"@adonisjs/shield": "^8.1.1",
|
|
@@ -59,9 +59,10 @@
|
|
|
59
59
|
"del-cli": "^5.1.0",
|
|
60
60
|
"edge.js": "^6.0.1",
|
|
61
61
|
"eslint": "^8.57.0",
|
|
62
|
-
"np": "^
|
|
62
|
+
"np": "^10.0.0",
|
|
63
63
|
"prettier": "^3.2.5",
|
|
64
64
|
"ts-node": "^10.9.2",
|
|
65
|
+
"tsup": "^8.0.2",
|
|
65
66
|
"typescript": "~5.3.3",
|
|
66
67
|
"vite": "^5.1.4"
|
|
67
68
|
},
|
|
@@ -73,7 +74,7 @@
|
|
|
73
74
|
},
|
|
74
75
|
"peerDependencies": {
|
|
75
76
|
"@adonisjs/core": "^6.3.0",
|
|
76
|
-
"@adonisjs/shield": "^8.
|
|
77
|
+
"@adonisjs/shield": "^8.0.0",
|
|
77
78
|
"edge.js": "^6.0.1",
|
|
78
79
|
"vite": "^5.1.4"
|
|
79
80
|
},
|
|
@@ -122,5 +123,21 @@
|
|
|
122
123
|
"tests/**",
|
|
123
124
|
"tests_helpers/**"
|
|
124
125
|
]
|
|
126
|
+
},
|
|
127
|
+
"tsup": {
|
|
128
|
+
"entry": [
|
|
129
|
+
"./index.ts",
|
|
130
|
+
"./providers/vite_provider.ts",
|
|
131
|
+
"./services/vite.ts",
|
|
132
|
+
"./src/types.ts",
|
|
133
|
+
"./src/client/main.ts",
|
|
134
|
+
"./src/hooks/build_hook.ts"
|
|
135
|
+
],
|
|
136
|
+
"outDir": "./build",
|
|
137
|
+
"clean": true,
|
|
138
|
+
"format": "esm",
|
|
139
|
+
"dts": false,
|
|
140
|
+
"sourcemap": true,
|
|
141
|
+
"target": "esnext"
|
|
125
142
|
}
|
|
126
143
|
}
|
package/build/configure.js
DELETED
|
@@ -1,42 +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 { stubsRoot } from './stubs/main.js';
|
|
10
|
-
/**
|
|
11
|
-
* Configures the package
|
|
12
|
-
*/
|
|
13
|
-
export async function configure(command) {
|
|
14
|
-
const codemods = await command.createCodemods();
|
|
15
|
-
let shouldInstallPackages = command.parsedFlags.install;
|
|
16
|
-
/**
|
|
17
|
-
* Publish stubs
|
|
18
|
-
*/
|
|
19
|
-
await codemods.makeUsingStub(stubsRoot, 'config/vite.stub', {});
|
|
20
|
-
await codemods.makeUsingStub(stubsRoot, 'vite.config.stub', {});
|
|
21
|
-
await codemods.makeUsingStub(stubsRoot, 'js_entrypoint.stub', {});
|
|
22
|
-
await codemods.updateRcFile((rcFile) => {
|
|
23
|
-
rcFile.addProvider('@adonisjs/vite/vite_provider');
|
|
24
|
-
rcFile.addMetaFile('public/**', false);
|
|
25
|
-
});
|
|
26
|
-
/**
|
|
27
|
-
* Prompt when `install` or `--no-install` flags are
|
|
28
|
-
* not used
|
|
29
|
-
*/
|
|
30
|
-
if (shouldInstallPackages === undefined) {
|
|
31
|
-
shouldInstallPackages = await command.prompt.confirm('Do you want to install "vite"?');
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Install dependency or list the command to install it
|
|
35
|
-
*/
|
|
36
|
-
if (shouldInstallPackages) {
|
|
37
|
-
await codemods.installPackages([{ name: 'vite', isDevDependency: true }]);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
await codemods.listPackagesToInstall([{ name: 'vite', isDevDependency: true }]);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
@@ -1,67 +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 { join } from 'node:path';
|
|
10
|
-
import { addTrailingSlash } from '../utils.js';
|
|
11
|
-
/**
|
|
12
|
-
* Resolve the `config.resolve.alias` value
|
|
13
|
-
*
|
|
14
|
-
* Basically we are merging the user defined alias with the
|
|
15
|
-
* default alias.
|
|
16
|
-
*/
|
|
17
|
-
export function resolveAlias(config) {
|
|
18
|
-
const defaultAlias = { '@/': `/resources/js/` };
|
|
19
|
-
if (Array.isArray(config.resolve?.alias)) {
|
|
20
|
-
return [
|
|
21
|
-
...(config.resolve?.alias ?? []),
|
|
22
|
-
Object.entries(defaultAlias).map(([find, replacement]) => ({ find, replacement })),
|
|
23
|
-
];
|
|
24
|
-
}
|
|
25
|
-
return { ...defaultAlias, ...config.resolve?.alias };
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Resolve the `config.base` value
|
|
29
|
-
*/
|
|
30
|
-
export function resolveBase(config, options, command) {
|
|
31
|
-
if (config.base)
|
|
32
|
-
return config.base;
|
|
33
|
-
if (command === 'build') {
|
|
34
|
-
return addTrailingSlash(options.assetsUrl);
|
|
35
|
-
}
|
|
36
|
-
return '/';
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Vite config hook
|
|
40
|
-
*/
|
|
41
|
-
export function configHook(options, userConfig, { command }) {
|
|
42
|
-
const config = {
|
|
43
|
-
publicDir: userConfig.publicDir ?? false,
|
|
44
|
-
resolve: { alias: resolveAlias(userConfig) },
|
|
45
|
-
base: resolveBase(userConfig, options, command),
|
|
46
|
-
build: {
|
|
47
|
-
assetsDir: '',
|
|
48
|
-
emptyOutDir: true,
|
|
49
|
-
manifest: userConfig.build?.manifest ?? true,
|
|
50
|
-
outDir: userConfig.build?.outDir ?? options.buildDirectory,
|
|
51
|
-
assetsInlineLimit: userConfig.build?.assetsInlineLimit ?? 0,
|
|
52
|
-
rollupOptions: {
|
|
53
|
-
input: options.entrypoints.map((entrypoint) => join(userConfig.root || '', entrypoint)),
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
};
|
|
57
|
-
return config;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Update the user vite config to match the Adonis requirements
|
|
61
|
-
*/
|
|
62
|
-
export const config = (options) => {
|
|
63
|
-
return {
|
|
64
|
-
name: 'vite-plugin-adonis:config',
|
|
65
|
-
config: configHook.bind(null, options),
|
|
66
|
-
};
|
|
67
|
-
};
|
|
@@ -1,22 +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 { join } from 'node:path';
|
|
10
|
-
/**
|
|
11
|
-
* Define the backend config for Vite
|
|
12
|
-
*/
|
|
13
|
-
export function defineConfig(config) {
|
|
14
|
-
return {
|
|
15
|
-
buildDirectory: 'public/assets',
|
|
16
|
-
assetsUrl: '/assets',
|
|
17
|
-
manifestFile: config.buildDirectory
|
|
18
|
-
? join(config.buildDirectory, '.vite/manifest.json')
|
|
19
|
-
: 'public/assets/.vite/manifest.json',
|
|
20
|
-
...config,
|
|
21
|
-
};
|
|
22
|
-
}
|
|
@@ -1,31 +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
|
-
/**
|
|
10
|
-
* Since Vite dev server is integrated within the AdonisJS process, this
|
|
11
|
-
* middleware is used to proxy the requests to it.
|
|
12
|
-
*
|
|
13
|
-
* Some of the requests are directly handled by the Vite dev server,
|
|
14
|
-
* like the one for the assets, while others are passed down to the
|
|
15
|
-
* AdonisJS server.
|
|
16
|
-
*/
|
|
17
|
-
export default class ViteMiddleware {
|
|
18
|
-
vite;
|
|
19
|
-
#devServer;
|
|
20
|
-
constructor(vite) {
|
|
21
|
-
this.vite = vite;
|
|
22
|
-
this.#devServer = this.vite.getDevServer();
|
|
23
|
-
}
|
|
24
|
-
async handle({ request, response }, next) {
|
|
25
|
-
return await new Promise((resolve) => {
|
|
26
|
-
this.#devServer.middlewares.handle(request.request, response.response, () => {
|
|
27
|
-
return resolve(next());
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,84 +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 { EdgeError } from 'edge-error';
|
|
10
|
-
/**
|
|
11
|
-
* The edge plugin for vite to share vite service with edge
|
|
12
|
-
* and register custom tags
|
|
13
|
-
*/
|
|
14
|
-
export const edgePluginVite = (vite) => {
|
|
15
|
-
const edgeVite = (edge) => {
|
|
16
|
-
edge.global('vite', vite);
|
|
17
|
-
edge.global('asset', vite.assetPath.bind(vite));
|
|
18
|
-
edge.registerTag({
|
|
19
|
-
tagName: 'viteReactRefresh',
|
|
20
|
-
seekable: true,
|
|
21
|
-
block: false,
|
|
22
|
-
compile(parser, buffer, token) {
|
|
23
|
-
let attributes = '';
|
|
24
|
-
if (token.properties.jsArg.trim()) {
|
|
25
|
-
/**
|
|
26
|
-
* Converting a single argument to a SequenceExpression so that we
|
|
27
|
-
* work around the following edge cases.
|
|
28
|
-
*
|
|
29
|
-
* - If someone passes an object literal to the tag, ie { nonce: 'foo' }
|
|
30
|
-
* it will be parsed as a LabeledStatement and not an object.
|
|
31
|
-
* - If we wrap the object literal inside parenthesis, ie ({nonce: 'foo'})
|
|
32
|
-
* then we will end up messing other expressions like a variable reference
|
|
33
|
-
* , or a member expression and so on.
|
|
34
|
-
* - So the best bet is to convert user supplied argument to a sequence expression
|
|
35
|
-
* and hence ignore it during stringification.
|
|
36
|
-
*/
|
|
37
|
-
const jsArg = `a,${token.properties.jsArg}`;
|
|
38
|
-
const parsed = parser.utils.transformAst(parser.utils.generateAST(jsArg, token.loc, token.filename), token.filename, parser);
|
|
39
|
-
attributes = parser.utils.stringify(parsed.expressions[1]);
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Get HMR script
|
|
43
|
-
*/
|
|
44
|
-
buffer.writeExpression(`const __vite_hmr_script = state.vite.getReactHmrScript(${attributes})`, token.filename, token.loc.start.line);
|
|
45
|
-
/**
|
|
46
|
-
* Check if the script exists (only in hot mode)
|
|
47
|
-
*/
|
|
48
|
-
buffer.writeStatement('if(__vite_hmr_script) {', token.filename, token.loc.start.line);
|
|
49
|
-
/**
|
|
50
|
-
* Write output
|
|
51
|
-
*/
|
|
52
|
-
buffer.outputExpression(`__vite_hmr_script.toString()`, token.filename, token.loc.start.line, false);
|
|
53
|
-
/**
|
|
54
|
-
* Close if block
|
|
55
|
-
*/
|
|
56
|
-
buffer.writeStatement('}', token.filename, token.loc.start.line);
|
|
57
|
-
},
|
|
58
|
-
});
|
|
59
|
-
edge.registerTag({
|
|
60
|
-
tagName: 'vite',
|
|
61
|
-
seekable: true,
|
|
62
|
-
block: false,
|
|
63
|
-
compile(parser, buffer, token) {
|
|
64
|
-
/**
|
|
65
|
-
* Ensure an argument is defined
|
|
66
|
-
*/
|
|
67
|
-
if (!token.properties.jsArg.trim()) {
|
|
68
|
-
throw new EdgeError('Missing entrypoint name', 'E_RUNTIME_EXCEPTION', {
|
|
69
|
-
filename: token.filename,
|
|
70
|
-
line: token.loc.start.line,
|
|
71
|
-
col: token.loc.start.col,
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
const parsed = parser.utils.transformAst(parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename), token.filename, parser);
|
|
75
|
-
const entrypoints = parser.utils.stringify(parsed);
|
|
76
|
-
const methodCall = parsed.type === 'SequenceExpression'
|
|
77
|
-
? `generateEntryPointsTags${entrypoints}`
|
|
78
|
-
: `generateEntryPointsTags(${entrypoints})`;
|
|
79
|
-
buffer.outputExpression(`state.vite.${methodCall}.join('\\n')`, token.filename, token.loc.start.line, false);
|
|
80
|
-
},
|
|
81
|
-
});
|
|
82
|
-
};
|
|
83
|
-
return edgeVite;
|
|
84
|
-
};
|
package/build/src/utils.js
DELETED
|
@@ -1,42 +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
|
-
/**
|
|
10
|
-
* Returns a new array with unique items by the given key
|
|
11
|
-
*/
|
|
12
|
-
export function uniqBy(array, key) {
|
|
13
|
-
const seen = new Set();
|
|
14
|
-
return array.filter((item) => {
|
|
15
|
-
const k = item[key];
|
|
16
|
-
return seen.has(k) ? false : seen.add(k);
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Convert Record of attributes to a valid HTML string
|
|
21
|
-
*/
|
|
22
|
-
export function makeAttributes(attributes) {
|
|
23
|
-
return Object.keys(attributes)
|
|
24
|
-
.map((key) => {
|
|
25
|
-
const value = attributes[key];
|
|
26
|
-
if (value === true) {
|
|
27
|
-
return key;
|
|
28
|
-
}
|
|
29
|
-
if (!value) {
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
return `${key}="${value}"`;
|
|
33
|
-
})
|
|
34
|
-
.filter((attr) => attr !== null)
|
|
35
|
-
.join(' ');
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Add a trailing slash if missing
|
|
39
|
-
*/
|
|
40
|
-
export const addTrailingSlash = (url) => {
|
|
41
|
-
return url.endsWith('/') ? url : url + '/';
|
|
42
|
-
};
|
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
|
-
}
|
package/build/stubs/main.js
DELETED
|
@@ -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);
|