@lokascript/vite-plugin 1.1.3 → 1.2.0

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/dist/index.js CHANGED
@@ -1170,7 +1170,13 @@ import {
1170
1170
  COMMAND_IMPLEMENTATIONS as COMMAND_IMPL_TS,
1171
1171
  BLOCK_IMPLEMENTATIONS as BLOCK_IMPL_TS,
1172
1172
  STYLE_COMMANDS,
1173
- ELEMENT_ARRAY_COMMANDS
1173
+ ELEMENT_ARRAY_COMMANDS,
1174
+ MORPH_COMMANDS,
1175
+ LITE_PARSER_TEMPLATE,
1176
+ HYBRID_PARSER_TEMPLATE,
1177
+ canUseLiteParser,
1178
+ FULL_RUNTIME_ONLY_COMMANDS,
1179
+ isAvailableCommand
1174
1180
  } from "@lokascript/core/bundle-generator";
1175
1181
 
1176
1182
  // src/semantic-integration.ts
@@ -1624,7 +1630,7 @@ function stripTypes(code) {
1624
1630
  ).replace(/:\s*[A-Z]\w*(?:\[\])?(?=\s*[=;,)\]])/g, "").replace(/\((\w+):\s*(?:\w+|unknown|any)\)/g, "($1)").replace(/Promise<[^>]+>/g, "Promise").replace(/Array<[^>]+>/g, "Array").replace(/\):\s*\w+(?:<[^>]*>)?(?:\[\])?\s*(?=[{=])/g, ") ").replace(/\):\s*\w+(?:<[^>]*>)?\s*=>/g, ") =>").replace(
1625
1631
  /:\s*(?:TransitionEvent|MouseEvent|KeyboardEvent|Event|Element|HTMLElement|Node)(?=\s*[=;,)\]])/g,
1626
1632
  ""
1627
- );
1633
+ ).replace(/(\w)!(?=[,;)\].\s])/g, "$1");
1628
1634
  }
1629
1635
  var COMMAND_IMPLEMENTATIONS = {};
1630
1636
  var BLOCK_IMPLEMENTATIONS = {};
@@ -1642,28 +1648,34 @@ function generateBundleCode(config) {
1642
1648
  htmxIntegration = false,
1643
1649
  globalName = "hyperfixi",
1644
1650
  positionalExpressions = false,
1645
- parserImportPath = "../parser/hybrid",
1646
1651
  autoInit = true,
1647
- esModule = true
1652
+ esModule = true,
1653
+ parserType = "hybrid"
1648
1654
  } = config;
1649
1655
  const needsStyleHelpers = commands.some((cmd) => STYLE_COMMANDS.includes(cmd));
1650
1656
  const needsElementArrayHelper = commands.some((cmd) => ELEMENT_ARRAY_COMMANDS.includes(cmd));
1657
+ const needsMorphlex = commands.some((cmd) => MORPH_COMMANDS.includes(cmd));
1651
1658
  const hasBlocks = blocks.length > 0;
1652
1659
  const hasReturn = commands.includes("return");
1653
1660
  const commandCases = commands.filter((cmd) => COMMAND_IMPLEMENTATIONS[cmd]).map((cmd) => COMMAND_IMPLEMENTATIONS[cmd]).join("\n");
1654
1661
  const blockCases = blocks.filter((block) => BLOCK_IMPLEMENTATIONS[block]).map((block) => BLOCK_IMPLEMENTATIONS[block]).join("\n");
1662
+ const parserTemplate = parserType === "lite" ? LITE_PARSER_TEMPLATE : HYBRID_PARSER_TEMPLATE;
1663
+ const morphlexImport = needsMorphlex ? `import { morph as morphlexMorph, morphInner as morphlexMorphInner } from 'morphlex';
1664
+
1665
+ ` : "";
1655
1666
  return `/**
1656
1667
  * LokaScript ${name} Bundle (Auto-Generated)
1657
1668
  *
1658
1669
  * Generated by: @lokascript/vite-plugin
1670
+ * Parser: ${parserType}
1659
1671
  * Commands: ${commands.join(", ")}${blocks.length > 0 ? `
1660
- * Blocks: ${blocks.join(", ")}` : ""}${positionalExpressions ? "\n * Positional expressions: enabled" : ""}
1672
+ * Blocks: ${blocks.join(", ")}` : ""}${positionalExpressions ? "\n * Positional expressions: enabled" : ""}${needsMorphlex ? "\n * Morphing: morphlex (~2.5KB)" : ""}
1661
1673
  *
1662
1674
  * DO NOT EDIT - This file is automatically regenerated.
1663
1675
  */
1664
1676
 
1665
- // Parser imports
1666
- import { HybridParser, addCommandAliases } from '${parserImportPath}';
1677
+ ${morphlexImport}// Embedded ${parserType} parser (no external dependencies)
1678
+ ${parserTemplate}
1667
1679
 
1668
1680
  // Runtime state
1669
1681
  const globalVars = new Map();
@@ -1962,8 +1974,7 @@ function processElement(el) {
1962
1974
  if (!code) return;
1963
1975
 
1964
1976
  try {
1965
- const parser = new HybridParser(code);
1966
- const ast = parser.parse();
1977
+ const ast = ${parserType === "lite" ? "parseLite(code)" : "new HybridParser(code).parse()"};
1967
1978
  executeAST(ast, el);
1968
1979
  } catch (err) {
1969
1980
  console.error('LokaScript ${name} error:', err, 'Code:', code);
@@ -1979,14 +1990,12 @@ const api = {
1979
1990
  version: '1.0.0-${name.toLowerCase().replace(/\s+/g, "-")}',
1980
1991
 
1981
1992
  parse(code) {
1982
- const parser = new HybridParser(code);
1983
- return parser.parse();
1993
+ return ${parserType === "lite" ? "parseLite(code)" : "new HybridParser(code).parse()"};
1984
1994
  },
1985
1995
 
1986
1996
  async execute(code, element) {
1987
1997
  const me = element || document.body;
1988
- const parser = new HybridParser(code);
1989
- const ast = parser.parse();
1998
+ const ast = ${parserType === "lite" ? "parseLite(code)" : "new HybridParser(code).parse()"};
1990
1999
  return executeAST(ast, me);
1991
2000
  },
1992
2001
 
@@ -1998,7 +2007,7 @@ const api = {
1998
2007
 
1999
2008
  commands: ${JSON.stringify(commands)},
2000
2009
  ${blocks.length > 0 ? `blocks: ${JSON.stringify(blocks)},` : ""}
2001
- parserName: 'hybrid',
2010
+ parserName: '${parserType}',
2002
2011
  };
2003
2012
  ${autoInit ? `
2004
2013
  if (typeof window !== 'undefined') {
@@ -2038,6 +2047,17 @@ var Generator = class {
2038
2047
  if (commands.length === 0 && blocks.length === 0 && !positional && !semanticConfig.enabled) {
2039
2048
  return this.generateEmptyBundle(options);
2040
2049
  }
2050
+ const unsupportedCommands = this.getUnsupportedCommands(commands);
2051
+ if (unsupportedCommands.length > 0) {
2052
+ if (this.debug) {
2053
+ console.log(
2054
+ `[hyperfixi] Commands detected that require full runtime: ${unsupportedCommands.join(", ")}
2055
+ Falling back to full browser bundle for complete functionality.`
2056
+ );
2057
+ }
2058
+ return this.generateDevFallback("full");
2059
+ }
2060
+ const parserType = canUseLiteParser(commands, blocks, positional) ? "lite" : "hybrid";
2041
2061
  const config = {
2042
2062
  name: options.bundleName ?? "ViteAutoGenerated",
2043
2063
  commands,
@@ -2045,8 +2065,7 @@ var Generator = class {
2045
2065
  positionalExpressions: positional,
2046
2066
  htmxIntegration: options.htmx ?? usage.htmx?.hasHtmxAttributes ?? false,
2047
2067
  globalName: options.globalName ?? "hyperfixi",
2048
- // Use @lokascript/core package path for virtual module
2049
- parserImportPath: "@lokascript/core/parser/hybrid",
2068
+ parserType,
2050
2069
  autoInit: true,
2051
2070
  esModule: true
2052
2071
  };
@@ -2056,7 +2075,8 @@ var Generator = class {
2056
2075
  blocks,
2057
2076
  positional,
2058
2077
  htmx: config.htmxIntegration,
2059
- semantic: semanticConfig.enabled
2078
+ semantic: semanticConfig.enabled,
2079
+ parserType
2060
2080
  };
2061
2081
  if (semanticConfig.enabled && semanticConfig.bundleType) {
2062
2082
  const sizeInfo = getSemanticBundleSize(semanticConfig.bundleType);
@@ -2091,7 +2111,7 @@ var Generator = class {
2091
2111
  bundleCode = bundleCode.slice(0, parserImportEnd) + semanticCode + "\n\n" + bundleCode.slice(parserImportEnd);
2092
2112
  }
2093
2113
  bundleCode = bundleCode.replace(
2094
- /function processElement\(el\) \{\s*const code = el\.getAttribute\('_'\);\s*if \(!code\) return;\s*try \{\s*const parser = new HybridParser\(code\);\s*const ast = parser\.parse\(\);/g,
2114
+ /function processElement\(el\) \{\s*const code = el\.getAttribute\('_'\);\s*if \(!code\) return;\s*try \{\s*const ast = (?:new HybridParser\(code\)\.parse\(\)|parseLite\(code\));/g,
2095
2115
  `function processElement(el) {
2096
2116
  const code = el.getAttribute('_');
2097
2117
  if (!code) return;
@@ -2100,27 +2120,47 @@ var Generator = class {
2100
2120
  const ast = parseWithSemantic(code);`
2101
2121
  );
2102
2122
  bundleCode = bundleCode.replace(
2103
- /parse\(code\) \{\s*const parser = new HybridParser\(code\);\s*return parser\.parse\(\);/g,
2123
+ /parse\(code\) \{\s*return (?:new HybridParser\(code\)\.parse\(\)|parseLite\(code\));/g,
2104
2124
  `parse(code, lang = null) {
2105
2125
  return parseWithSemantic(code, lang);`
2106
2126
  );
2107
2127
  bundleCode = bundleCode.replace(
2108
- /async execute\(code, element\) \{\s*const me = element \|\| document\.body;\s*const parser = new HybridParser\(code\);\s*const ast = parser\.parse\(\);/g,
2128
+ /async execute\(code, element\) \{\s*const me = element \|\| document\.body;\s*const ast = (?:new HybridParser\(code\)\.parse\(\)|parseLite\(code\));/g,
2109
2129
  `async execute(code, element, lang = null) {
2110
2130
  const me = element || document.body;
2111
2131
  const ast = parseWithSemantic(code, lang);`
2112
2132
  );
2113
2133
  if (semanticExports.length > 0) {
2114
- const apiExportsMarker = "parserName: 'hybrid',";
2134
+ const apiExportsMarkerRegex = /parserName: '(?:lite|hybrid)',/;
2115
2135
  const additionalApiProps = semanticExports.map((exp) => ` ${exp},`).join("\n");
2116
2136
  bundleCode = bundleCode.replace(
2117
- apiExportsMarker,
2118
- `parserName: 'hybrid',
2137
+ apiExportsMarkerRegex,
2138
+ `parserName: 'semantic',
2119
2139
  ${additionalApiProps}`
2120
2140
  );
2121
2141
  }
2122
2142
  return bundleCode;
2123
2143
  }
2144
+ /**
2145
+ * Get commands that require the full runtime and are not available in generated bundles
2146
+ */
2147
+ getUnsupportedCommands(commands) {
2148
+ const unsupported = [];
2149
+ for (const cmd of commands) {
2150
+ if (FULL_RUNTIME_ONLY_COMMANDS.includes(cmd)) {
2151
+ unsupported.push(cmd);
2152
+ } else if (!isAvailableCommand(cmd) && !COMMAND_IMPLEMENTATIONS[cmd]) {
2153
+ unsupported.push(cmd);
2154
+ }
2155
+ }
2156
+ return unsupported;
2157
+ }
2158
+ /**
2159
+ * Check if any commands require full runtime and should trigger fallback
2160
+ */
2161
+ requiresFullRuntime(commands) {
2162
+ return this.getUnsupportedCommands(commands).length > 0;
2163
+ }
2124
2164
  /**
2125
2165
  * Generate an empty bundle when no hyperscript is detected
2126
2166
  */