@lokascript/vite-plugin 1.1.4 → 1.2.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/dist/index.cjs CHANGED
@@ -1553,26 +1553,94 @@ var SEMANTIC_BUNDLE_SIZES = {
1553
1553
  function getSemanticBundleSize(bundleType) {
1554
1554
  return SEMANTIC_BUNDLE_SIZES[bundleType] ?? SEMANTIC_BUNDLE_SIZES.all;
1555
1555
  }
1556
- function getSemanticBundleImport(_bundleType) {
1557
- return "@lokascript/semantic";
1556
+ var LANGUAGES_WITH_ESM_EXPORTS = /* @__PURE__ */ new Set([
1557
+ "en",
1558
+ "es",
1559
+ "ja",
1560
+ "ar",
1561
+ "ko",
1562
+ "zh",
1563
+ "tr",
1564
+ "pt",
1565
+ "fr",
1566
+ "de",
1567
+ "id",
1568
+ "qu",
1569
+ "sw"
1570
+ ]);
1571
+ function getLanguagesForBundleType(bundleType, configLanguages) {
1572
+ const languages = new Set(configLanguages);
1573
+ switch (bundleType) {
1574
+ case "western":
1575
+ REGIONS.western.forEach((l) => languages.add(l));
1576
+ break;
1577
+ case "east-asian":
1578
+ REGIONS["east-asian"].forEach((l) => languages.add(l));
1579
+ break;
1580
+ case "priority":
1581
+ REGIONS.priority.forEach((l) => languages.add(l));
1582
+ break;
1583
+ case "all":
1584
+ REGIONS.all.forEach((l) => languages.add(l));
1585
+ break;
1586
+ case "es-en":
1587
+ languages.add("en");
1588
+ languages.add("es");
1589
+ break;
1590
+ default:
1591
+ if (typeof bundleType === "string" && bundleType.length <= 3) {
1592
+ languages.add(bundleType);
1593
+ }
1594
+ break;
1595
+ }
1596
+ languages.add("en");
1597
+ return languages;
1598
+ }
1599
+ function canUseCorePlusLanguages(languages) {
1600
+ for (const lang of languages) {
1601
+ if (!LANGUAGES_WITH_ESM_EXPORTS.has(lang)) {
1602
+ return false;
1603
+ }
1604
+ }
1605
+ return true;
1558
1606
  }
1559
1607
  function generateSemanticIntegrationCode(config) {
1560
1608
  if (!config.enabled || !config.bundleType) {
1561
1609
  return "";
1562
1610
  }
1563
- const bundleImport = getSemanticBundleImport(config.bundleType);
1611
+ const resolvedLanguages = getLanguagesForBundleType(config.bundleType, config.languages);
1612
+ const useCorePlusLanguages = canUseCorePlusLanguages(resolvedLanguages);
1564
1613
  const languages = [...config.languages].join("', '");
1565
1614
  let code = `
1566
1615
  // =============================================================================
1567
1616
  // SEMANTIC PARSER INTEGRATION
1568
1617
  // =============================================================================
1618
+ `;
1619
+ if (useCorePlusLanguages) {
1620
+ code += `
1621
+ // Core infrastructure (no language data bundled)
1622
+ import {
1623
+ createSemanticAnalyzer,
1624
+ buildAST,
1625
+ isLanguageSupported,
1626
+ } from '@lokascript/semantic/core';
1569
1627
 
1628
+ // Register required languages (self-registering side-effect imports)
1629
+ `;
1630
+ const sortedLangs = [...resolvedLanguages].sort();
1631
+ for (const lang of sortedLangs) {
1632
+ code += `import '@lokascript/semantic/languages/${lang}';
1633
+ `;
1634
+ }
1635
+ } else {
1636
+ code += `
1570
1637
  import {
1571
1638
  createSemanticAnalyzer,
1572
1639
  buildAST,
1573
1640
  isLanguageSupported,
1574
- } from '${bundleImport}';
1641
+ } from '@lokascript/semantic';
1575
1642
  `;
1643
+ }
1576
1644
  if (config.grammarEnabled) {
1577
1645
  code += `
1578
1646
  import { GrammarTransformer, translate } from '@lokascript/i18n';
@@ -1686,28 +1754,34 @@ function generateBundleCode(config) {
1686
1754
  htmxIntegration = false,
1687
1755
  globalName = "hyperfixi",
1688
1756
  positionalExpressions = false,
1689
- parserImportPath = "../parser/hybrid",
1690
1757
  autoInit = true,
1691
- esModule = true
1758
+ esModule = true,
1759
+ parserType = "hybrid"
1692
1760
  } = config;
1693
1761
  const needsStyleHelpers = commands.some((cmd) => import_bundle_generator.STYLE_COMMANDS.includes(cmd));
1694
1762
  const needsElementArrayHelper = commands.some((cmd) => import_bundle_generator.ELEMENT_ARRAY_COMMANDS.includes(cmd));
1763
+ const needsMorphlex = commands.some((cmd) => import_bundle_generator.MORPH_COMMANDS.includes(cmd));
1695
1764
  const hasBlocks = blocks.length > 0;
1696
1765
  const hasReturn = commands.includes("return");
1697
1766
  const commandCases = commands.filter((cmd) => COMMAND_IMPLEMENTATIONS[cmd]).map((cmd) => COMMAND_IMPLEMENTATIONS[cmd]).join("\n");
1698
1767
  const blockCases = blocks.filter((block) => BLOCK_IMPLEMENTATIONS[block]).map((block) => BLOCK_IMPLEMENTATIONS[block]).join("\n");
1768
+ const parserTemplate = parserType === "lite" ? import_bundle_generator.LITE_PARSER_TEMPLATE : import_bundle_generator.HYBRID_PARSER_TEMPLATE;
1769
+ const morphlexImport = needsMorphlex ? `import { morph as morphlexMorph, morphInner as morphlexMorphInner } from 'morphlex';
1770
+
1771
+ ` : "";
1699
1772
  return `/**
1700
1773
  * LokaScript ${name} Bundle (Auto-Generated)
1701
1774
  *
1702
1775
  * Generated by: @lokascript/vite-plugin
1776
+ * Parser: ${parserType}
1703
1777
  * Commands: ${commands.join(", ")}${blocks.length > 0 ? `
1704
- * Blocks: ${blocks.join(", ")}` : ""}${positionalExpressions ? "\n * Positional expressions: enabled" : ""}
1778
+ * Blocks: ${blocks.join(", ")}` : ""}${positionalExpressions ? "\n * Positional expressions: enabled" : ""}${needsMorphlex ? "\n * Morphing: morphlex (~2.5KB)" : ""}
1705
1779
  *
1706
1780
  * DO NOT EDIT - This file is automatically regenerated.
1707
1781
  */
1708
1782
 
1709
- // Parser imports
1710
- import { HybridParser, addCommandAliases } from '${parserImportPath}';
1783
+ ${morphlexImport}// Embedded ${parserType} parser (no external dependencies)
1784
+ ${parserTemplate}
1711
1785
 
1712
1786
  // Runtime state
1713
1787
  const globalVars = new Map();
@@ -2006,8 +2080,7 @@ function processElement(el) {
2006
2080
  if (!code) return;
2007
2081
 
2008
2082
  try {
2009
- const parser = new HybridParser(code);
2010
- const ast = parser.parse();
2083
+ const ast = ${parserType === "lite" ? "parseLite(code)" : "new HybridParser(code).parse()"};
2011
2084
  executeAST(ast, el);
2012
2085
  } catch (err) {
2013
2086
  console.error('LokaScript ${name} error:', err, 'Code:', code);
@@ -2023,14 +2096,12 @@ const api = {
2023
2096
  version: '1.0.0-${name.toLowerCase().replace(/\s+/g, "-")}',
2024
2097
 
2025
2098
  parse(code) {
2026
- const parser = new HybridParser(code);
2027
- return parser.parse();
2099
+ return ${parserType === "lite" ? "parseLite(code)" : "new HybridParser(code).parse()"};
2028
2100
  },
2029
2101
 
2030
2102
  async execute(code, element) {
2031
2103
  const me = element || document.body;
2032
- const parser = new HybridParser(code);
2033
- const ast = parser.parse();
2104
+ const ast = ${parserType === "lite" ? "parseLite(code)" : "new HybridParser(code).parse()"};
2034
2105
  return executeAST(ast, me);
2035
2106
  },
2036
2107
 
@@ -2042,7 +2113,7 @@ const api = {
2042
2113
 
2043
2114
  commands: ${JSON.stringify(commands)},
2044
2115
  ${blocks.length > 0 ? `blocks: ${JSON.stringify(blocks)},` : ""}
2045
- parserName: 'hybrid',
2116
+ parserName: '${parserType}',
2046
2117
  };
2047
2118
  ${autoInit ? `
2048
2119
  if (typeof window !== 'undefined') {
@@ -2082,6 +2153,17 @@ var Generator = class {
2082
2153
  if (commands.length === 0 && blocks.length === 0 && !positional && !semanticConfig.enabled) {
2083
2154
  return this.generateEmptyBundle(options);
2084
2155
  }
2156
+ const unsupportedCommands = this.getUnsupportedCommands(commands);
2157
+ if (unsupportedCommands.length > 0) {
2158
+ if (this.debug) {
2159
+ console.log(
2160
+ `[hyperfixi] Commands detected that require full runtime: ${unsupportedCommands.join(", ")}
2161
+ Falling back to full browser bundle for complete functionality.`
2162
+ );
2163
+ }
2164
+ return this.generateDevFallback("full");
2165
+ }
2166
+ const parserType = (0, import_bundle_generator.canUseLiteParser)(commands, blocks, positional) ? "lite" : "hybrid";
2085
2167
  const config = {
2086
2168
  name: options.bundleName ?? "ViteAutoGenerated",
2087
2169
  commands,
@@ -2089,8 +2171,7 @@ var Generator = class {
2089
2171
  positionalExpressions: positional,
2090
2172
  htmxIntegration: options.htmx ?? usage.htmx?.hasHtmxAttributes ?? false,
2091
2173
  globalName: options.globalName ?? "hyperfixi",
2092
- // Use @lokascript/core package path for virtual module
2093
- parserImportPath: "@lokascript/core/parser/hybrid",
2174
+ parserType,
2094
2175
  autoInit: true,
2095
2176
  esModule: true
2096
2177
  };
@@ -2100,7 +2181,8 @@ var Generator = class {
2100
2181
  blocks,
2101
2182
  positional,
2102
2183
  htmx: config.htmxIntegration,
2103
- semantic: semanticConfig.enabled
2184
+ semantic: semanticConfig.enabled,
2185
+ parserType
2104
2186
  };
2105
2187
  if (semanticConfig.enabled && semanticConfig.bundleType) {
2106
2188
  const sizeInfo = getSemanticBundleSize(semanticConfig.bundleType);
@@ -2135,7 +2217,7 @@ var Generator = class {
2135
2217
  bundleCode = bundleCode.slice(0, parserImportEnd) + semanticCode + "\n\n" + bundleCode.slice(parserImportEnd);
2136
2218
  }
2137
2219
  bundleCode = bundleCode.replace(
2138
- /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,
2220
+ /function processElement\(el\) \{\s*const code = el\.getAttribute\('_'\);\s*if \(!code\) return;\s*try \{\s*const ast = (?:new HybridParser\(code\)\.parse\(\)|parseLite\(code\));/g,
2139
2221
  `function processElement(el) {
2140
2222
  const code = el.getAttribute('_');
2141
2223
  if (!code) return;
@@ -2144,27 +2226,47 @@ var Generator = class {
2144
2226
  const ast = parseWithSemantic(code);`
2145
2227
  );
2146
2228
  bundleCode = bundleCode.replace(
2147
- /parse\(code\) \{\s*const parser = new HybridParser\(code\);\s*return parser\.parse\(\);/g,
2229
+ /parse\(code\) \{\s*return (?:new HybridParser\(code\)\.parse\(\)|parseLite\(code\));/g,
2148
2230
  `parse(code, lang = null) {
2149
2231
  return parseWithSemantic(code, lang);`
2150
2232
  );
2151
2233
  bundleCode = bundleCode.replace(
2152
- /async execute\(code, element\) \{\s*const me = element \|\| document\.body;\s*const parser = new HybridParser\(code\);\s*const ast = parser\.parse\(\);/g,
2234
+ /async execute\(code, element\) \{\s*const me = element \|\| document\.body;\s*const ast = (?:new HybridParser\(code\)\.parse\(\)|parseLite\(code\));/g,
2153
2235
  `async execute(code, element, lang = null) {
2154
2236
  const me = element || document.body;
2155
2237
  const ast = parseWithSemantic(code, lang);`
2156
2238
  );
2157
2239
  if (semanticExports.length > 0) {
2158
- const apiExportsMarker = "parserName: 'hybrid',";
2240
+ const apiExportsMarkerRegex = /parserName: '(?:lite|hybrid)',/;
2159
2241
  const additionalApiProps = semanticExports.map((exp) => ` ${exp},`).join("\n");
2160
2242
  bundleCode = bundleCode.replace(
2161
- apiExportsMarker,
2162
- `parserName: 'hybrid',
2243
+ apiExportsMarkerRegex,
2244
+ `parserName: 'semantic',
2163
2245
  ${additionalApiProps}`
2164
2246
  );
2165
2247
  }
2166
2248
  return bundleCode;
2167
2249
  }
2250
+ /**
2251
+ * Get commands that require the full runtime and are not available in generated bundles
2252
+ */
2253
+ getUnsupportedCommands(commands) {
2254
+ const unsupported = [];
2255
+ for (const cmd of commands) {
2256
+ if (import_bundle_generator.FULL_RUNTIME_ONLY_COMMANDS.includes(cmd)) {
2257
+ unsupported.push(cmd);
2258
+ } else if (!(0, import_bundle_generator.isAvailableCommand)(cmd) && !COMMAND_IMPLEMENTATIONS[cmd]) {
2259
+ unsupported.push(cmd);
2260
+ }
2261
+ }
2262
+ return unsupported;
2263
+ }
2264
+ /**
2265
+ * Check if any commands require full runtime and should trigger fallback
2266
+ */
2267
+ requiresFullRuntime(commands) {
2268
+ return this.getUnsupportedCommands(commands).length > 0;
2269
+ }
2168
2270
  /**
2169
2271
  * Generate an empty bundle when no hyperscript is detected
2170
2272
  */