@adonisjs/vite 5.1.0 → 5.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.js CHANGED
@@ -1,49 +1,73 @@
1
- import {
2
- Vite
3
- } from "./chunk-ZCIGOANY.js";
4
- import "./chunk-ZCQTQOMI.js";
5
-
6
- // stubs/main.ts
7
- var stubsRoot = import.meta.dirname;
8
-
9
- // configure.ts
1
+ import { t as Vite } from "./vite-DDpuL-Eo.js";
2
+ import { join } from "node:path";
3
+ //#region stubs/main.ts
4
+ const stubsRoot = import.meta.dirname;
5
+ //#endregion
6
+ //#region configure.ts
7
+ /**
8
+ * Configures the package
9
+ */
10
10
  async function configure(command) {
11
- const codemods = await command.createCodemods();
12
- let shouldInstallPackages = command.parsedFlags.install;
13
- await codemods.makeUsingStub(stubsRoot, "config/vite.stub", {});
14
- await codemods.makeUsingStub(stubsRoot, "vite.config.stub", {});
15
- await codemods.makeUsingStub(stubsRoot, "js_entrypoint.stub", {});
16
- await codemods.updateRcFile((rcFile) => {
17
- rcFile.addProvider("@adonisjs/vite/vite_provider");
18
- rcFile.addMetaFile("public/**", false);
19
- rcFile.addAssemblerHook("buildStarting", "@adonisjs/vite/build_hook");
20
- });
21
- await codemods.registerMiddleware("server", [
22
- { path: "@adonisjs/vite/vite_middleware", position: "after" }
23
- ]);
24
- if (shouldInstallPackages === void 0) {
25
- shouldInstallPackages = await command.prompt.confirm('Do you want to install "vite"?');
26
- }
27
- if (shouldInstallPackages) {
28
- await codemods.installPackages([{ name: "vite", isDevDependency: true }]);
29
- } else {
30
- await codemods.listPackagesToInstall([{ name: "vite", isDevDependency: true }]);
31
- }
11
+ const codemods = await command.createCodemods();
12
+ let shouldInstallPackages = command.parsedFlags.install;
13
+ /**
14
+ * Publish stubs
15
+ */
16
+ await codemods.makeUsingStub(stubsRoot, "config/vite.stub", {});
17
+ await codemods.makeUsingStub(stubsRoot, "vite.config.stub", {});
18
+ await codemods.makeUsingStub(stubsRoot, "js_entrypoint.stub", {});
19
+ /**
20
+ * Update RC file
21
+ */
22
+ await codemods.updateRcFile((rcFile) => {
23
+ rcFile.addProvider("@adonisjs/vite/vite_provider");
24
+ rcFile.addMetaFile("public/**", false);
25
+ rcFile.addAssemblerHook("buildStarting", "@adonisjs/vite/build_hook");
26
+ });
27
+ /**
28
+ * Add server middleware
29
+ */
30
+ await codemods.registerMiddleware("server", [{
31
+ path: "@adonisjs/vite/vite_middleware",
32
+ position: "after"
33
+ }]);
34
+ /**
35
+ * Prompt when `install` or `--no-install` flags are
36
+ * not used
37
+ */
38
+ if (shouldInstallPackages === void 0) shouldInstallPackages = await command.prompt.confirm("Do you want to install \"vite\"?");
39
+ /**
40
+ * Install dependency or list the command to install it
41
+ */
42
+ if (shouldInstallPackages) await codemods.installPackages([{
43
+ name: "vite",
44
+ isDevDependency: true
45
+ }]);
46
+ else await codemods.listPackagesToInstall([{
47
+ name: "vite",
48
+ isDevDependency: true
49
+ }]);
32
50
  }
33
-
34
- // src/define_config.ts
35
- import { join } from "path";
51
+ //#endregion
52
+ //#region src/define_config.ts
53
+ /**
54
+ * Creates a complete Vite configuration by merging provided options with sensible defaults
55
+ *
56
+ * @param config - Partial Vite configuration options to override defaults
57
+ *
58
+ * @example
59
+ * const viteConfig = defineConfig({
60
+ * assetsUrl: '/static',
61
+ * buildDirectory: 'dist'
62
+ * })
63
+ */
36
64
  function defineConfig(config) {
37
- return {
38
- assetsUrl: "/assets",
39
- buildDirectory: "public/assets",
40
- manifestFile: config.buildDirectory ? join(config.buildDirectory, ".vite/manifest.json") : "public/assets/.vite/manifest.json",
41
- ...config
42
- };
65
+ return {
66
+ assetsUrl: "/assets",
67
+ buildDirectory: "public/assets",
68
+ manifestFile: config.buildDirectory ? join(config.buildDirectory, ".vite/manifest.json") : "public/assets/.vite/manifest.json",
69
+ ...config
70
+ };
43
71
  }
44
- export {
45
- Vite,
46
- configure,
47
- defineConfig,
48
- stubsRoot
49
- };
72
+ //#endregion
73
+ export { Vite, configure, defineConfig, stubsRoot };
@@ -1,141 +1,140 @@
1
- import {
2
- Vite
3
- } from "../chunk-ZCIGOANY.js";
4
- import {
5
- ViteMiddleware
6
- } from "../chunk-AF6PV64J.js";
7
- import "../chunk-ZCQTQOMI.js";
8
-
9
- // providers/vite_provider.ts
1
+ import { t as Vite } from "../vite-DDpuL-Eo.js";
2
+ import { t as ViteMiddleware } from "../vite_middleware-Bszg_DDr.js";
3
+ //#region providers/vite_provider.ts
4
+ /**
5
+ * AdonisJS service provider for integrating Vite build tool and development server
6
+ *
7
+ * Handles lifecycle management of Vite integration including:
8
+ * - Registering Vite service in the container
9
+ * - Setting up Edge.js plugin for template integration
10
+ * - Managing development server startup/shutdown
11
+ * - Configuring CSP keywords for @adonisjs/shield
12
+ */
10
13
  var ViteProvider = class {
11
- /**
12
- * Creates a new ViteProvider instance
13
- *
14
- * @param app - The AdonisJS application service instance
15
- *
16
- * @example
17
- * const provider = new ViteProvider(app)
18
- */
19
- constructor(app) {
20
- this.app = app;
21
- }
22
- /**
23
- * Flag indicating whether the Vite development server should be started
24
- * Set to true when manifest file doesn't exist and running in web/test environment
25
- */
26
- #shouldRunViteDevServer = false;
27
- /**
28
- * Registers the Vite Edge.js plugin if Edge.js is available in the application
29
- *
30
- * Adds global helpers and custom tags (@vite, @viteReactRefresh) to Edge templates.
31
- * Only registers the plugin if the application is using Edge.js.
32
- *
33
- * @example
34
- * await provider.registerEdgePlugin()
35
- * // Enables @vite('app.js') in Edge templates
36
- */
37
- async registerEdgePlugin() {
38
- if (this.app.usingEdgeJS) {
39
- const edge = await import("edge.js");
40
- const vite = await this.app.container.make("vite");
41
- const { edgePluginVite } = await import("../src/plugins/edge.js");
42
- edge.default.use(edgePluginVite(vite));
43
- }
44
- }
45
- /**
46
- * Registers Content Security Policy keywords when @adonisjs/shield is installed
47
- *
48
- * Adds the '@viteUrl' keyword for CSP directives that returns the Vite assets URL
49
- * when it's an HTTP/HTTPS URL, or an empty string otherwise.
50
- *
51
- * @example
52
- * await provider.registerShieldKeywords()
53
- * // Enables @viteUrl in CSP directives
54
- */
55
- async registerShieldKeywords() {
56
- let cspKeywords = null;
57
- try {
58
- const shieldExports = await import("@adonisjs/shield");
59
- cspKeywords = shieldExports.cspKeywords;
60
- } catch {
61
- }
62
- if (!cspKeywords) {
63
- return;
64
- }
65
- const vite = await this.app.container.make("vite");
66
- cspKeywords.register("@viteUrl", function() {
67
- const assetsURL = vite.assetsUrl();
68
- if (!assetsURL || !assetsURL.startsWith("http://") || assetsURL.startsWith("https://")) {
69
- return "";
70
- }
71
- return assetsURL;
72
- });
73
- }
74
- /**
75
- * Registers Vite service and middleware bindings in the IoC container
76
- *
77
- * Creates a Vite instance using configuration and determines whether
78
- * the development server should run based on environment and manifest file presence.
79
- *
80
- * @example
81
- * provider.register()
82
- * // Vite service is now available via container.make('vite')
83
- */
84
- register() {
85
- const appEnvironment = this.app.getEnvironment();
86
- const vite = new Vite(this.app.config.get("vite"));
87
- this.#shouldRunViteDevServer = appEnvironment === "test" || !!process.env.DEV_MODE;
88
- this.app.container.singleton(Vite, () => vite);
89
- this.app.container.singleton(ViteMiddleware, () => new ViteMiddleware(vite));
90
- this.app.container.alias("vite", Vite);
91
- }
92
- /**
93
- * Boots the Vite provider by registering plugins and integrations
94
- *
95
- * Registers Edge.js plugin and Shield CSP keywords if the respective
96
- * packages are available in the application.
97
- *
98
- * @example
99
- * await provider.boot()
100
- */
101
- async boot() {
102
- await this.registerEdgePlugin();
103
- await this.registerShieldKeywords();
104
- }
105
- /**
106
- * Starts the Vite development server when the application is ready
107
- *
108
- * Only starts the server if running in development/test mode and
109
- * no manifest file exists (indicating development mode).
110
- *
111
- * @example
112
- * await provider.ready()
113
- * // Dev server starts on configured port
114
- */
115
- async ready() {
116
- if (!this.#shouldRunViteDevServer) {
117
- return;
118
- }
119
- const vite = await this.app.container.make("vite");
120
- await vite.createDevServer();
121
- }
122
- /**
123
- * Gracefully stops the Vite development server during application shutdown
124
- *
125
- * Only attempts to stop the server if it was started during the ready phase.
126
- * Ensures clean shutdown of the development server and its resources.
127
- *
128
- * @example
129
- * await provider.shutdown()
130
- */
131
- async shutdown() {
132
- if (!this.#shouldRunViteDevServer) {
133
- return;
134
- }
135
- const vite = await this.app.container.make("vite");
136
- await vite.stopDevServer();
137
- }
138
- };
139
- export {
140
- ViteProvider as default
14
+ /**
15
+ * Flag indicating whether the Vite development server should be started
16
+ * Set to true when manifest file doesn't exist and running in web/test environment
17
+ */
18
+ #shouldRunViteDevServer = false;
19
+ /**
20
+ * Creates a new ViteProvider instance
21
+ *
22
+ * @param app - The AdonisJS application service instance
23
+ *
24
+ * @example
25
+ * const provider = new ViteProvider(app)
26
+ */
27
+ constructor(app) {
28
+ this.app = app;
29
+ }
30
+ /**
31
+ * Registers the Vite Edge.js plugin if Edge.js is available in the application
32
+ *
33
+ * Adds global helpers and custom tags (@vite, @viteReactRefresh) to Edge templates.
34
+ * Only registers the plugin if the application is using Edge.js.
35
+ *
36
+ * @example
37
+ * await provider.registerEdgePlugin()
38
+ * // Enables @vite('app.js') in Edge templates
39
+ */
40
+ async registerEdgePlugin() {
41
+ if (this.app.usingEdgeJS) {
42
+ const edge = await import("edge.js");
43
+ const vite = await this.app.container.make("vite");
44
+ const { edgePluginVite } = await import("../src/plugins/edge.js");
45
+ edge.default.use(edgePluginVite(vite));
46
+ }
47
+ }
48
+ /**
49
+ * Registers Content Security Policy keywords when @adonisjs/shield is installed
50
+ *
51
+ * Adds the '@viteUrl' keyword for CSP directives that returns the Vite assets URL
52
+ * when it's an HTTP/HTTPS URL, or an empty string otherwise.
53
+ *
54
+ * @example
55
+ * await provider.registerShieldKeywords()
56
+ * // Enables @viteUrl in CSP directives
57
+ */
58
+ async registerShieldKeywords() {
59
+ let cspKeywords = null;
60
+ try {
61
+ cspKeywords = (await import("@adonisjs/shield")).cspKeywords;
62
+ } catch {}
63
+ if (!cspKeywords) return;
64
+ const vite = await this.app.container.make("vite");
65
+ /**
66
+ * Registering the @viteUrl keyword for CSP directives.
67
+ * Returns http URL to the dev or the CDN server, otherwise
68
+ * an empty string
69
+ */
70
+ cspKeywords.register("@viteUrl", function() {
71
+ const assetsURL = vite.assetsUrl();
72
+ if (!assetsURL || !assetsURL.startsWith("http://") || assetsURL.startsWith("https://")) return "";
73
+ return assetsURL;
74
+ });
75
+ }
76
+ /**
77
+ * Registers Vite service and middleware bindings in the IoC container
78
+ *
79
+ * Creates a Vite instance using configuration and determines whether
80
+ * the development server should run based on environment and manifest file presence.
81
+ *
82
+ * @example
83
+ * provider.register()
84
+ * // Vite service is now available via container.make('vite')
85
+ */
86
+ register() {
87
+ const appEnvironment = this.app.getEnvironment();
88
+ const vite = new Vite(this.app.config.get("vite"));
89
+ /**
90
+ * DEV_MODE is injected when the dev server is started
91
+ * by assembler
92
+ */
93
+ this.#shouldRunViteDevServer = appEnvironment === "test" || !!process.env.DEV_MODE;
94
+ this.app.container.singleton(Vite, () => vite);
95
+ this.app.container.singleton(ViteMiddleware, () => new ViteMiddleware(vite));
96
+ this.app.container.alias("vite", Vite);
97
+ }
98
+ /**
99
+ * Boots the Vite provider by registering plugins and integrations
100
+ *
101
+ * Registers Edge.js plugin and Shield CSP keywords if the respective
102
+ * packages are available in the application.
103
+ *
104
+ * @example
105
+ * await provider.boot()
106
+ */
107
+ async boot() {
108
+ await this.registerEdgePlugin();
109
+ await this.registerShieldKeywords();
110
+ }
111
+ /**
112
+ * Starts the Vite development server when the application is ready
113
+ *
114
+ * Only starts the server if running in development/test mode and
115
+ * no manifest file exists (indicating development mode).
116
+ *
117
+ * @example
118
+ * await provider.ready()
119
+ * // Dev server starts on configured port
120
+ */
121
+ async ready() {
122
+ if (!this.#shouldRunViteDevServer) return;
123
+ await (await this.app.container.make("vite")).createDevServer();
124
+ }
125
+ /**
126
+ * Gracefully stops the Vite development server during application shutdown
127
+ *
128
+ * Only attempts to stop the server if it was started during the ready phase.
129
+ * Ensures clean shutdown of the development server and its resources.
130
+ *
131
+ * @example
132
+ * await provider.shutdown()
133
+ */
134
+ async shutdown() {
135
+ if (!this.#shouldRunViteDevServer) return;
136
+ await (await this.app.container.make("vite")).stopDevServer();
137
+ }
141
138
  };
139
+ //#endregion
140
+ export { ViteProvider as default };
@@ -1,9 +1,12 @@
1
- // services/vite.ts
2
1
  import app from "@adonisjs/core/services/app";
3
- var vite;
2
+ //#region services/vite.ts
3
+ let vite;
4
+ /**
5
+ * Returns a singleton instance of Vite class
6
+ * from the container
7
+ */
4
8
  await app.booted(async () => {
5
- vite = await app.container.make("vite");
9
+ vite = await app.container.make("vite");
6
10
  });
7
- export {
8
- vite as default
9
- };
11
+ //#endregion
12
+ export { vite as default };
@@ -1,62 +1,75 @@
1
- import {
2
- addTrailingSlash
3
- } from "../../chunk-ZCQTQOMI.js";
4
-
5
- // src/client/main.ts
1
+ import { t as addTrailingSlash } from "../../utils-CdZpa_tV.js";
2
+ import { join } from "node:path";
6
3
  import PluginRestart from "vite-plugin-restart";
7
-
8
- // src/client/config.ts
9
- import { join } from "path";
10
- function resolveBase(config2, options, command) {
11
- if (config2.base) return config2.base;
12
- if (command === "build") {
13
- return addTrailingSlash(options.assetsUrl);
14
- }
15
- return "/";
4
+ //#region src/client/config.ts
5
+ /**
6
+ * Resolves the base URL for Vite configuration based on command and options
7
+ *
8
+ * @param config - User-provided Vite configuration
9
+ * @param options - Plugin options containing assetsUrl
10
+ * @param command - Vite command being executed ('build' or 'serve')
11
+ *
12
+ * @example
13
+ * const base = resolveBase(userConfig, { assetsUrl: '/assets' }, 'build')
14
+ * // Returns: '/assets/' for build, '/' for serve
15
+ */
16
+ function resolveBase(config, options, command) {
17
+ if (config.base) return config.base;
18
+ if (command === "build") return addTrailingSlash(options.assetsUrl);
19
+ return "/";
16
20
  }
21
+ /**
22
+ * Merges user Vite configuration with AdonisJS-specific defaults and requirements
23
+ *
24
+ * @param options - Plugin options containing build directory and entry points
25
+ * @param userConfig - User-provided Vite configuration
26
+ * @param command - Vite environment command
27
+ *
28
+ * @example
29
+ * const mergedConfig = configHook(pluginOptions, userViteConfig, { command: 'build' })
30
+ */
17
31
  function configHook(options, userConfig, { command }) {
18
- const config2 = {
19
- publicDir: userConfig.publicDir ?? false,
20
- base: resolveBase(userConfig, options, command),
21
- /**
22
- * Disable the vite dev server cors handling. Otherwise, it will
23
- * override the cors settings defined by @adonisjs/cors
24
- * https://github.com/adonisjs/vite/issues/13
25
- */
26
- server: { cors: userConfig.server?.cors ?? false },
27
- build: {
28
- assetsDir: "",
29
- emptyOutDir: true,
30
- manifest: userConfig.build?.manifest ?? true,
31
- outDir: userConfig.build?.outDir ?? options.buildDirectory,
32
- assetsInlineLimit: userConfig.build?.assetsInlineLimit ?? 0,
33
- rollupOptions: {
34
- input: options.entrypoints.map((entrypoint) => join(userConfig.root || "", entrypoint))
35
- }
36
- }
37
- };
38
- return config2;
32
+ return {
33
+ publicDir: userConfig.publicDir ?? false,
34
+ base: resolveBase(userConfig, options, command),
35
+ /**
36
+ * Disable the vite dev server cors handling. Otherwise, it will
37
+ * override the cors settings defined by @adonisjs/cors
38
+ * https://github.com/adonisjs/vite/issues/13
39
+ */
40
+ server: { cors: userConfig.server?.cors ?? false },
41
+ build: {
42
+ assetsDir: "",
43
+ emptyOutDir: true,
44
+ manifest: userConfig.build?.manifest ?? true,
45
+ outDir: userConfig.build?.outDir ?? options.buildDirectory,
46
+ assetsInlineLimit: userConfig.build?.assetsInlineLimit ?? 0,
47
+ rollupOptions: { input: options.entrypoints.map((entrypoint) => join(userConfig.root || "", entrypoint)) }
48
+ }
49
+ };
39
50
  }
40
- var config = (options) => {
41
- return {
42
- name: "vite-plugin-adonis:config",
43
- enforce: "post",
44
- config: configHook.bind(null, options)
45
- };
51
+ /**
52
+ * Update the user vite config to match the Adonis requirements
53
+ */
54
+ const config = (options) => {
55
+ return {
56
+ name: "vite-plugin-adonis:config",
57
+ enforce: "post",
58
+ config: configHook.bind(null, options)
59
+ };
46
60
  };
47
-
48
- // src/client/main.ts
61
+ //#endregion
62
+ //#region src/client/main.ts
63
+ /**
64
+ * Vite plugin for AdonisJS
65
+ */
49
66
  function adonisjs(options) {
50
- const fullOptions = Object.assign(
51
- {
52
- assetsUrl: "/assets",
53
- buildDirectory: "public/assets",
54
- reload: ["./resources/views/**/*.edge"]
55
- },
56
- options
57
- );
58
- return [PluginRestart({ reload: fullOptions.reload }), config(fullOptions)];
67
+ const fullOptions = Object.assign({
68
+ assetsUrl: "/assets",
69
+ buildDirectory: "public/assets",
70
+ reload: ["./resources/views/**/*.edge"]
71
+ }, options);
72
+ return [PluginRestart({ reload: fullOptions.reload }), config(fullOptions)];
59
73
  }
60
- export {
61
- adonisjs as default
62
- };
74
+ //#endregion
75
+ export { adonisjs as default };
@@ -1,11 +1,15 @@
1
- // src/hooks/build_hook.ts
2
1
  import { hooks } from "@adonisjs/core/app";
3
2
  import { createBuilder } from "vite";
3
+ //#region src/hooks/build_hook.ts
4
+ /**
5
+ * This is an Assembler hook that should be executed when the application is
6
+ * builded using the `node ace build` command.
7
+ *
8
+ * The hook is responsible for launching a Vite multi-build process.
9
+ */
4
10
  var build_hook_default = hooks.buildStarting(async (parent) => {
5
- parent.ui.logger.info("building assets with vite");
6
- const builder = await createBuilder({}, null);
7
- await builder.buildApp();
11
+ parent.ui.logger.info("building assets with vite");
12
+ await (await createBuilder({}, null)).buildApp();
8
13
  });
9
- export {
10
- build_hook_default as default
11
- };
14
+ //#endregion
15
+ export { build_hook_default as default };