lapikit 0.3.4 → 0.3.5

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,4 +1,4 @@
1
1
  import type { TextfieldProps } from './types.js';
2
- declare const Textfield: import("svelte").Component<TextfieldProps, {}, "value" | "ref">;
2
+ declare const Textfield: import("svelte").Component<TextfieldProps, {}, "ref" | "value">;
3
3
  type Textfield = ReturnType<typeof Textfield>;
4
4
  export default Textfield;
@@ -1,4 +1,7 @@
1
- declare function lapikitPreprocess(): {
1
+ type LapikitPreprocessOptions = {
2
+ plugins?: string[];
3
+ };
4
+ declare function lapikitPreprocess(options?: LapikitPreprocessOptions): {
2
5
  markup({ content }: {
3
6
  content: string;
4
7
  filename?: string;
@@ -2,23 +2,58 @@ const components = ['btn'];
2
2
  function componentName(shortName) {
3
3
  return 'Kit' + shortName.charAt(0).toUpperCase() + shortName.slice(1);
4
4
  }
5
- function lapikitPreprocess() {
5
+ // plugins lapikit
6
+ const lapikitPlugins = {
7
+ repl: {
8
+ components: ['code'],
9
+ ref: '@lapikit/repl'
10
+ }
11
+ };
12
+ function lapikitPreprocess(options) {
6
13
  return {
7
14
  markup({ content }) {
8
- const hasComponent = components.some((comp) => content.includes(`<lpk:${comp}`));
15
+ const allComponents = [...components];
16
+ const componentToRef = new Map();
17
+ components.forEach((comp) => {
18
+ componentToRef.set(comp, 'lapikit/labs/components');
19
+ });
20
+ // plugins
21
+ if (options?.plugins) {
22
+ options.plugins.forEach((pluginKey) => {
23
+ const plugin = lapikitPlugins[pluginKey];
24
+ if (plugin) {
25
+ plugin.components.forEach((comp) => {
26
+ if (!allComponents.includes(comp)) {
27
+ allComponents.push(comp);
28
+ }
29
+ componentToRef.set(comp, plugin.ref);
30
+ });
31
+ }
32
+ });
33
+ }
34
+ const hasComponent = allComponents.some((comp) => content.includes(`<lpk:${comp}`));
9
35
  if (!hasComponent)
10
36
  return;
11
37
  let processedContent = content;
12
- const importedComponents = new Set();
38
+ const importedComponents = new Map();
13
39
  let hasChanges = true;
14
40
  while (hasChanges) {
15
41
  hasChanges = false;
16
- for (const shortName of components) {
42
+ for (const shortName of allComponents) {
17
43
  const componentNameStr = componentName(shortName);
18
- const regex = new RegExp(`<lpk:${shortName}([\\s\\S]*?)>([\\s\\S]*?)<\\/lpk:${shortName}\\s*>`, 'g');
19
- const newContent = processedContent.replace(regex, (fullMatch, attrs, children) => {
44
+ const attrPattern = `(?:[^>"']|"[^"]*"|'[^']*')*?`;
45
+ const selfClosingRegex = new RegExp(`<lpk:${shortName}(${attrPattern})\\s*/>`, 'g');
46
+ const pairRegex = new RegExp(`<lpk:${shortName}(${attrPattern})>([\\s\\S]*?)<\\/lpk:${shortName}\\s*>`, 'g');
47
+ let newContent = processedContent.replace(selfClosingRegex, (fullMatch, attrs) => {
48
+ hasChanges = true;
49
+ const ref = componentToRef.get(shortName) || 'lapikit/labs/components';
50
+ importedComponents.set(componentNameStr, ref);
51
+ return `<${componentNameStr}${attrs} />`;
52
+ });
53
+ newContent = newContent.replace(pairRegex, (fullMatch, attrs, children) => {
20
54
  hasChanges = true;
21
- importedComponents.add(componentNameStr);
55
+ const ref = componentToRef.get(shortName) || 'lapikit/labs/components';
56
+ importedComponents.set(componentNameStr, ref);
22
57
  return `<${componentNameStr}${attrs}>${children}</${componentNameStr}>`;
23
58
  });
24
59
  if (newContent !== processedContent) {
@@ -29,15 +64,25 @@ function lapikitPreprocess() {
29
64
  if (processedContent === content)
30
65
  return;
31
66
  if (importedComponents.size > 0) {
32
- const imports = Array.from(importedComponents).join(', ');
67
+ const importsByRef = new Map();
68
+ importedComponents.forEach((ref, component) => {
69
+ if (!importsByRef.has(ref)) {
70
+ importsByRef.set(ref, []);
71
+ }
72
+ importsByRef.get(ref).push(component);
73
+ });
74
+ const importLines = Array.from(importsByRef.entries())
75
+ .map(([ref, components]) => {
76
+ const imports = components.join(', ');
77
+ return `\n\timport { ${imports} } from '${ref}';`;
78
+ })
79
+ .join('');
33
80
  const scriptRegex = /<script(?![^>]*\bmodule\b)([^>]*)>/;
34
81
  const scriptMatch = processedContent.match(scriptRegex);
35
82
  if (scriptMatch && scriptMatch.index !== undefined) {
36
83
  const insertPos = scriptMatch.index + scriptMatch[0].length;
37
84
  processedContent =
38
- processedContent.slice(0, insertPos) +
39
- `\n\timport { ${imports} } from 'lapikit/labs/components';` +
40
- processedContent.slice(insertPos);
85
+ processedContent.slice(0, insertPos) + importLines + processedContent.slice(insertPos);
41
86
  }
42
87
  else {
43
88
  const moduleScriptMatch = processedContent.match(/<script[^>]*\bmodule\b[^>]*>/);
@@ -45,13 +90,11 @@ function lapikitPreprocess() {
45
90
  const moduleScriptEnd = processedContent.indexOf('</script>', moduleScriptMatch.index) + '</script>'.length;
46
91
  processedContent =
47
92
  processedContent.slice(0, moduleScriptEnd) +
48
- `\n\n<script>\n\timport { ${imports} } from 'lapikit/labs/components';\n</script>` +
93
+ `\n\n<script>${importLines}\n</script>` +
49
94
  processedContent.slice(moduleScriptEnd);
50
95
  }
51
96
  else {
52
- processedContent =
53
- `<script>\n\timport { ${imports} } from 'lapikit/labs/components';\n</script>\n\n` +
54
- processedContent;
97
+ processedContent = `<script>${importLines}\n</script>\n\n` + processedContent;
55
98
  }
56
99
  }
57
100
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lapikit",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"