@agnos-ui/svelte-preprocess 0.7.0-next.0 → 0.7.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.
Files changed (3) hide show
  1. package/index.cjs +37 -16
  2. package/index.js +37 -16
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -2,6 +2,12 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const MagicString = require("magic-string");
4
4
  const compiler = require("svelte/compiler");
5
+ const isElement = (node) => "attributes" in node;
6
+ const isEachBlock = (node) => node.type === "EachBlock";
7
+ const isIfBlock = (node) => node.type === "IfBlock";
8
+ const isAwaitBlock = (node) => node.type === "AwaitBlock";
9
+ const isKeyBlock = (node) => node.type === "KeyBlock";
10
+ const isSnippetBlock = (node) => node.type === "SnippetBlock";
5
11
  const directivesPreprocess = () => {
6
12
  return {
7
13
  name: "AgnosUI",
@@ -11,29 +17,33 @@ const directivesPreprocess = () => {
11
17
  return;
12
18
  }
13
19
  const str = new MagicString(content, { filename });
14
- const parsedCode = compiler.parse(content, { filename });
20
+ const parsedCode = compiler.parse(content, { filename, modern: true });
15
21
  const requiredImports = /* @__PURE__ */ new Set();
16
22
  const extractValue = (attribute) => {
17
23
  const res = [];
18
- const value = attribute.value;
24
+ if (attribute.value === true) {
25
+ return "true";
26
+ }
27
+ const value = Array.isArray(attribute.value) ? attribute.value : [attribute.value];
19
28
  for (const part of value) {
20
29
  if (part.type === "Text") {
21
30
  res.push(JSON.stringify(part.data));
22
- } else if (part.type === "MustacheTag") {
23
- res.push(`(${content.substring(part.expression.start, part.expression.end)})`);
31
+ } else if (part.type === "ExpressionTag") {
32
+ const expression = part.expression;
33
+ res.push(`(${content.substring(expression.start, expression.end)})`);
24
34
  } else {
25
- throw new Error(`Unexpected part type: ${part.type}`);
35
+ throw new Error(`Assert failed, unexpected part`);
26
36
  }
27
37
  }
28
38
  return res.join("+");
29
39
  };
40
+ const processFragment = (items) => items == null ? void 0 : items.nodes.forEach(processItem);
30
41
  const processItem = (item) => {
31
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
32
42
  const actionAttributes = [];
33
43
  const classAttributes = [];
34
- if (item.attributes) {
44
+ if (isElement(item)) {
35
45
  for (const attribute of item.attributes) {
36
- if (attribute.type === "Action") {
46
+ if (attribute.type === "UseDirective") {
37
47
  actionAttributes.push(attribute);
38
48
  } else if (attribute.type === "Attribute" && attribute.name === "class") {
39
49
  classAttributes.push(attribute);
@@ -50,8 +60,9 @@ const directivesPreprocess = () => {
50
60
  } else {
51
61
  str.appendRight(end, `, `);
52
62
  }
53
- if (attribute.expression) {
54
- str.appendRight(end, `[${attribute.name}, ${content.substring(attribute.expression.start, attribute.expression.end)}]`);
63
+ const expression = attribute.expression;
64
+ if (expression) {
65
+ str.appendRight(end, `[${attribute.name}, ${content.substring(expression.start, expression.end)}]`);
55
66
  } else {
56
67
  str.appendRight(end, attribute.name);
57
68
  }
@@ -64,14 +75,24 @@ const directivesPreprocess = () => {
64
75
  }
65
76
  str.appendRight(end, `)}`);
66
77
  }
78
+ processFragment(item.fragment);
79
+ } else if (isEachBlock(item)) {
80
+ processFragment(item.body);
81
+ processFragment(item.fallback);
82
+ } else if (isIfBlock(item)) {
83
+ processFragment(item.consequent);
84
+ processFragment(item.alternate);
85
+ } else if (isAwaitBlock(item)) {
86
+ processFragment(item.pending);
87
+ processFragment(item.then);
88
+ processFragment(item.catch);
89
+ } else if (isKeyBlock(item)) {
90
+ processFragment(item.fragment);
91
+ } else if (isSnippetBlock(item)) {
92
+ processFragment(item.body);
67
93
  }
68
- (_a = item.children) == null ? void 0 : _a.forEach(processItem);
69
- (_c = (_b = item.else) == null ? void 0 : _b.children) == null ? void 0 : _c.forEach(processItem);
70
- (_e = (_d = item.pending) == null ? void 0 : _d.children) == null ? void 0 : _e.forEach(processItem);
71
- (_g = (_f = item.then) == null ? void 0 : _f.children) == null ? void 0 : _g.forEach(processItem);
72
- (_i = (_h = item.catch) == null ? void 0 : _h.children) == null ? void 0 : _i.forEach(processItem);
73
94
  };
74
- processItem(parsedCode.html);
95
+ processFragment(parsedCode.fragment);
75
96
  if (requiredImports.size > 0) {
76
97
  const importStatement = `
77
98
  import {${[...requiredImports].map((importName) => `${importName} as ${varPrefix}${importName}`).join(", ")}} from '@agnos-ui/svelte-headless/utils/directive';
package/index.js CHANGED
@@ -1,5 +1,11 @@
1
1
  import MagicString from "magic-string";
2
2
  import { parse } from "svelte/compiler";
3
+ const isElement = (node) => "attributes" in node;
4
+ const isEachBlock = (node) => node.type === "EachBlock";
5
+ const isIfBlock = (node) => node.type === "IfBlock";
6
+ const isAwaitBlock = (node) => node.type === "AwaitBlock";
7
+ const isKeyBlock = (node) => node.type === "KeyBlock";
8
+ const isSnippetBlock = (node) => node.type === "SnippetBlock";
3
9
  const directivesPreprocess = () => {
4
10
  return {
5
11
  name: "AgnosUI",
@@ -9,29 +15,33 @@ const directivesPreprocess = () => {
9
15
  return;
10
16
  }
11
17
  const str = new MagicString(content, { filename });
12
- const parsedCode = parse(content, { filename });
18
+ const parsedCode = parse(content, { filename, modern: true });
13
19
  const requiredImports = /* @__PURE__ */ new Set();
14
20
  const extractValue = (attribute) => {
15
21
  const res = [];
16
- const value = attribute.value;
22
+ if (attribute.value === true) {
23
+ return "true";
24
+ }
25
+ const value = Array.isArray(attribute.value) ? attribute.value : [attribute.value];
17
26
  for (const part of value) {
18
27
  if (part.type === "Text") {
19
28
  res.push(JSON.stringify(part.data));
20
- } else if (part.type === "MustacheTag") {
21
- res.push(`(${content.substring(part.expression.start, part.expression.end)})`);
29
+ } else if (part.type === "ExpressionTag") {
30
+ const expression = part.expression;
31
+ res.push(`(${content.substring(expression.start, expression.end)})`);
22
32
  } else {
23
- throw new Error(`Unexpected part type: ${part.type}`);
33
+ throw new Error(`Assert failed, unexpected part`);
24
34
  }
25
35
  }
26
36
  return res.join("+");
27
37
  };
38
+ const processFragment = (items) => items == null ? void 0 : items.nodes.forEach(processItem);
28
39
  const processItem = (item) => {
29
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
30
40
  const actionAttributes = [];
31
41
  const classAttributes = [];
32
- if (item.attributes) {
42
+ if (isElement(item)) {
33
43
  for (const attribute of item.attributes) {
34
- if (attribute.type === "Action") {
44
+ if (attribute.type === "UseDirective") {
35
45
  actionAttributes.push(attribute);
36
46
  } else if (attribute.type === "Attribute" && attribute.name === "class") {
37
47
  classAttributes.push(attribute);
@@ -48,8 +58,9 @@ const directivesPreprocess = () => {
48
58
  } else {
49
59
  str.appendRight(end, `, `);
50
60
  }
51
- if (attribute.expression) {
52
- str.appendRight(end, `[${attribute.name}, ${content.substring(attribute.expression.start, attribute.expression.end)}]`);
61
+ const expression = attribute.expression;
62
+ if (expression) {
63
+ str.appendRight(end, `[${attribute.name}, ${content.substring(expression.start, expression.end)}]`);
53
64
  } else {
54
65
  str.appendRight(end, attribute.name);
55
66
  }
@@ -62,14 +73,24 @@ const directivesPreprocess = () => {
62
73
  }
63
74
  str.appendRight(end, `)}`);
64
75
  }
76
+ processFragment(item.fragment);
77
+ } else if (isEachBlock(item)) {
78
+ processFragment(item.body);
79
+ processFragment(item.fallback);
80
+ } else if (isIfBlock(item)) {
81
+ processFragment(item.consequent);
82
+ processFragment(item.alternate);
83
+ } else if (isAwaitBlock(item)) {
84
+ processFragment(item.pending);
85
+ processFragment(item.then);
86
+ processFragment(item.catch);
87
+ } else if (isKeyBlock(item)) {
88
+ processFragment(item.fragment);
89
+ } else if (isSnippetBlock(item)) {
90
+ processFragment(item.body);
65
91
  }
66
- (_a = item.children) == null ? void 0 : _a.forEach(processItem);
67
- (_c = (_b = item.else) == null ? void 0 : _b.children) == null ? void 0 : _c.forEach(processItem);
68
- (_e = (_d = item.pending) == null ? void 0 : _d.children) == null ? void 0 : _e.forEach(processItem);
69
- (_g = (_f = item.then) == null ? void 0 : _f.children) == null ? void 0 : _g.forEach(processItem);
70
- (_i = (_h = item.catch) == null ? void 0 : _h.children) == null ? void 0 : _i.forEach(processItem);
71
92
  };
72
- processItem(parsedCode.html);
93
+ processFragment(parsedCode.fragment);
73
94
  if (requiredImports.size > 0) {
74
95
  const importStatement = `
75
96
  import {${[...requiredImports].map((importName) => `${importName} as ${varPrefix}${importName}`).join(", ")}} from '@agnos-ui/svelte-headless/utils/directive';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@agnos-ui/svelte-preprocess",
3
3
  "description": "Preprocessor to run Svelte directives server-side.",
4
- "version": "0.7.0-next.0",
4
+ "version": "0.7.1",
5
5
  "type": "module",
6
6
  "main": "./index.cjs",
7
7
  "module": "./index.js",
@@ -17,7 +17,7 @@
17
17
  "magic-string": "^0.30.14"
18
18
  },
19
19
  "peerDependencies": {
20
- "svelte": "^5.9.0"
20
+ "svelte": "^5.0.0"
21
21
  },
22
22
  "sideEffects": false,
23
23
  "homepage": "https://www.agnosui.dev/latest/",