@adonisjs/vite 5.1.0-next.4 → 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.
@@ -1,69 +1,87 @@
1
- // src/plugins/edge.ts
2
1
  import { EdgeError } from "edge-error";
3
- var edgePluginVite = (vite) => {
4
- const edgeVite = (edge) => {
5
- edge.global("vite", vite);
6
- edge.global("asset", vite.assetPath.bind(vite));
7
- edge.registerTag({
8
- tagName: "viteReactRefresh",
9
- seekable: true,
10
- block: false,
11
- compile(parser, buffer, token) {
12
- let attributes = "";
13
- if (token.properties.jsArg.trim()) {
14
- const jsArg = `a,${token.properties.jsArg}`;
15
- const parsed = parser.utils.transformAst(
16
- parser.utils.generateAST(jsArg, token.loc, token.filename),
17
- token.filename,
18
- parser
19
- );
20
- attributes = parser.utils.stringify(parsed.expressions[1]);
21
- }
22
- buffer.writeExpression(
23
- `const __vite_hmr_script = state.vite.getReactHmrScript(${attributes})`,
24
- token.filename,
25
- token.loc.start.line
26
- );
27
- buffer.writeStatement("if(__vite_hmr_script) {", token.filename, token.loc.start.line);
28
- buffer.outputExpression(
29
- `__vite_hmr_script.toString()`,
30
- token.filename,
31
- token.loc.start.line,
32
- false
33
- );
34
- buffer.writeStatement("}", token.filename, token.loc.start.line);
35
- }
36
- });
37
- edge.registerTag({
38
- tagName: "vite",
39
- seekable: true,
40
- block: false,
41
- compile(parser, buffer, token) {
42
- if (!token.properties.jsArg.trim()) {
43
- throw new EdgeError("Missing entrypoint name", "E_RUNTIME_EXCEPTION", {
44
- filename: token.filename,
45
- line: token.loc.start.line,
46
- col: token.loc.start.col
47
- });
48
- }
49
- const parsed = parser.utils.transformAst(
50
- parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename),
51
- token.filename,
52
- parser
53
- );
54
- const entrypoints = parser.utils.stringify(parsed);
55
- const methodCall = parsed.type === "SequenceExpression" ? `generateEntryPointsTags${entrypoints}` : `generateEntryPointsTags(${entrypoints})`;
56
- buffer.outputExpression(
57
- `(await state.vite.${methodCall}).join('\\n')`,
58
- token.filename,
59
- token.loc.start.line,
60
- false
61
- );
62
- }
63
- });
64
- };
65
- return edgeVite;
66
- };
67
- export {
68
- edgePluginVite
2
+ //#region src/plugins/edge.ts
3
+ /**
4
+ * Creates an Edge.js plugin that integrates Vite functionality into templates
5
+ *
6
+ * Registers global helpers and custom tags (@vite, @viteReactRefresh) for use in Edge templates.
7
+ * Provides access to asset paths and HMR functionality within template rendering.
8
+ *
9
+ * @param vite - The Vite instance to integrate with Edge templates
10
+ *
11
+ * @example
12
+ * const plugin = edgePluginVite(viteInstance)
13
+ * edge.use(plugin)
14
+ *
15
+ * // In templates:
16
+ * // @vite('app.js')
17
+ * // @viteReactRefresh({ nonce: 'abc123' })
18
+ */
19
+ const edgePluginVite = (vite) => {
20
+ const edgeVite = (edge) => {
21
+ edge.global("vite", vite);
22
+ edge.global("asset", vite.assetPath.bind(vite));
23
+ edge.registerTag({
24
+ tagName: "viteReactRefresh",
25
+ seekable: true,
26
+ block: false,
27
+ compile(parser, buffer, token) {
28
+ let attributes = "";
29
+ if (token.properties.jsArg.trim()) {
30
+ /**
31
+ * Converting a single argument to a SequenceExpression so that we
32
+ * work around the following edge cases.
33
+ *
34
+ * - If someone passes an object literal to the tag, ie { nonce: 'foo' }
35
+ * it will be parsed as a LabeledStatement and not an object.
36
+ * - If we wrap the object literal inside parenthesis, ie ({nonce: 'foo'})
37
+ * then we will end up messing other expressions like a variable reference
38
+ * , or a member expression and so on.
39
+ * - So the best bet is to convert user supplied argument to a sequence expression
40
+ * and hence ignore it during stringification.
41
+ */
42
+ const jsArg = `a,${token.properties.jsArg}`;
43
+ const parsed = parser.utils.transformAst(parser.utils.generateAST(jsArg, token.loc, token.filename), token.filename, parser);
44
+ attributes = parser.utils.stringify(parsed.expressions[1]);
45
+ }
46
+ /**
47
+ * Get HMR script
48
+ */
49
+ buffer.writeExpression(`const __vite_hmr_script = state.vite.getReactHmrScript(${attributes})`, token.filename, token.loc.start.line);
50
+ /**
51
+ * Check if the script exists (only in hot mode)
52
+ */
53
+ buffer.writeStatement("if(__vite_hmr_script) {", token.filename, token.loc.start.line);
54
+ /**
55
+ * Write output
56
+ */
57
+ buffer.outputExpression(`__vite_hmr_script.toString()`, token.filename, token.loc.start.line, false);
58
+ /**
59
+ * Close if block
60
+ */
61
+ buffer.writeStatement("}", token.filename, token.loc.start.line);
62
+ }
63
+ });
64
+ edge.registerTag({
65
+ tagName: "vite",
66
+ seekable: true,
67
+ block: false,
68
+ compile(parser, buffer, token) {
69
+ /**
70
+ * Ensure an argument is defined
71
+ */
72
+ if (!token.properties.jsArg.trim()) throw new EdgeError("Missing entrypoint name", "E_RUNTIME_EXCEPTION", {
73
+ filename: token.filename,
74
+ line: token.loc.start.line,
75
+ col: token.loc.start.col
76
+ });
77
+ const parsed = parser.utils.transformAst(parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename), token.filename, parser);
78
+ const entrypoints = parser.utils.stringify(parsed);
79
+ const methodCall = parsed.type === "SequenceExpression" ? `generateEntryPointsTags${entrypoints}` : `generateEntryPointsTags(${entrypoints})`;
80
+ buffer.outputExpression(`(await state.vite.${methodCall}).join('\\n')`, token.filename, token.loc.start.line, false);
81
+ }
82
+ });
83
+ };
84
+ return edgeVite;
69
85
  };
86
+ //#endregion
87
+ export { edgePluginVite };
@@ -0,0 +1 @@
1
+ export {};
@@ -12,7 +12,6 @@ import type { Vite } from './vite.ts';
12
12
  * AdonisJS server.
13
13
  */
14
14
  export default class ViteMiddleware {
15
- #private;
16
15
  protected vite: Vite;
17
16
  /**
18
17
  * Creates a new ViteMiddleware instance
@@ -1,6 +1,2 @@
1
- import {
2
- ViteMiddleware
3
- } from "../chunk-AF6PV64J.js";
4
- export {
5
- ViteMiddleware as default
6
- };
1
+ import { t as ViteMiddleware } from "../vite_middleware-Bszg_DDr.js";
2
+ export { ViteMiddleware as default };
@@ -0,0 +1,50 @@
1
+ //#region src/utils.ts
2
+ /**
3
+ * Returns a new array with unique items filtered by the specified key
4
+ *
5
+ * @param array - The array to filter for unique items
6
+ * @param key - The key to use for uniqueness comparison
7
+ *
8
+ * @example
9
+ * const users = [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 1, name: 'John' }]
10
+ * const unique = uniqBy(users, 'id')
11
+ * // Returns: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
12
+ */
13
+ function uniqBy(array, key) {
14
+ const seen = /* @__PURE__ */ new Set();
15
+ return array.filter((item) => {
16
+ const k = item[key];
17
+ return seen.has(k) ? false : seen.add(k);
18
+ });
19
+ }
20
+ /**
21
+ * Converts an object of HTML attributes to a valid HTML attribute string
22
+ *
23
+ * @param attributes - Object containing HTML attributes where values can be strings or booleans
24
+ *
25
+ * @example
26
+ * const attrs = makeAttributes({ class: 'btn', disabled: true, hidden: false })
27
+ * // Returns: 'class="btn" disabled'
28
+ */
29
+ function makeAttributes(attributes) {
30
+ return Object.keys(attributes).map((key) => {
31
+ const value = attributes[key];
32
+ if (value === true) return key;
33
+ if (!value) return null;
34
+ return `${key}="${value}"`;
35
+ }).filter((attr) => attr !== null).join(" ");
36
+ }
37
+ /**
38
+ * Adds a trailing slash to a URL if it doesn't already have one
39
+ *
40
+ * @param url - The URL string to process
41
+ *
42
+ * @example
43
+ * addTrailingSlash('/api') // Returns: '/api/'
44
+ * addTrailingSlash('/api/') // Returns: '/api/'
45
+ */
46
+ const addTrailingSlash = (url) => {
47
+ return url.endsWith("/") ? url : `${url}/`;
48
+ };
49
+ //#endregion
50
+ export { makeAttributes as n, uniqBy as r, addTrailingSlash as t };