@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.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
@@ -1509,26 +1515,94 @@ var SEMANTIC_BUNDLE_SIZES = {
1509
1515
  function getSemanticBundleSize(bundleType) {
1510
1516
  return SEMANTIC_BUNDLE_SIZES[bundleType] ?? SEMANTIC_BUNDLE_SIZES.all;
1511
1517
  }
1512
- function getSemanticBundleImport(_bundleType) {
1513
- return "@lokascript/semantic";
1518
+ var LANGUAGES_WITH_ESM_EXPORTS = /* @__PURE__ */ new Set([
1519
+ "en",
1520
+ "es",
1521
+ "ja",
1522
+ "ar",
1523
+ "ko",
1524
+ "zh",
1525
+ "tr",
1526
+ "pt",
1527
+ "fr",
1528
+ "de",
1529
+ "id",
1530
+ "qu",
1531
+ "sw"
1532
+ ]);
1533
+ function getLanguagesForBundleType(bundleType, configLanguages) {
1534
+ const languages = new Set(configLanguages);
1535
+ switch (bundleType) {
1536
+ case "western":
1537
+ REGIONS.western.forEach((l) => languages.add(l));
1538
+ break;
1539
+ case "east-asian":
1540
+ REGIONS["east-asian"].forEach((l) => languages.add(l));
1541
+ break;
1542
+ case "priority":
1543
+ REGIONS.priority.forEach((l) => languages.add(l));
1544
+ break;
1545
+ case "all":
1546
+ REGIONS.all.forEach((l) => languages.add(l));
1547
+ break;
1548
+ case "es-en":
1549
+ languages.add("en");
1550
+ languages.add("es");
1551
+ break;
1552
+ default:
1553
+ if (typeof bundleType === "string" && bundleType.length <= 3) {
1554
+ languages.add(bundleType);
1555
+ }
1556
+ break;
1557
+ }
1558
+ languages.add("en");
1559
+ return languages;
1560
+ }
1561
+ function canUseCorePlusLanguages(languages) {
1562
+ for (const lang of languages) {
1563
+ if (!LANGUAGES_WITH_ESM_EXPORTS.has(lang)) {
1564
+ return false;
1565
+ }
1566
+ }
1567
+ return true;
1514
1568
  }
1515
1569
  function generateSemanticIntegrationCode(config) {
1516
1570
  if (!config.enabled || !config.bundleType) {
1517
1571
  return "";
1518
1572
  }
1519
- const bundleImport = getSemanticBundleImport(config.bundleType);
1573
+ const resolvedLanguages = getLanguagesForBundleType(config.bundleType, config.languages);
1574
+ const useCorePlusLanguages = canUseCorePlusLanguages(resolvedLanguages);
1520
1575
  const languages = [...config.languages].join("', '");
1521
1576
  let code = `
1522
1577
  // =============================================================================
1523
1578
  // SEMANTIC PARSER INTEGRATION
1524
1579
  // =============================================================================
1580
+ `;
1581
+ if (useCorePlusLanguages) {
1582
+ code += `
1583
+ // Core infrastructure (no language data bundled)
1584
+ import {
1585
+ createSemanticAnalyzer,
1586
+ buildAST,
1587
+ isLanguageSupported,
1588
+ } from '@lokascript/semantic/core';
1525
1589
 
1590
+ // Register required languages (self-registering side-effect imports)
1591
+ `;
1592
+ const sortedLangs = [...resolvedLanguages].sort();
1593
+ for (const lang of sortedLangs) {
1594
+ code += `import '@lokascript/semantic/languages/${lang}';
1595
+ `;
1596
+ }
1597
+ } else {
1598
+ code += `
1526
1599
  import {
1527
1600
  createSemanticAnalyzer,
1528
1601
  buildAST,
1529
1602
  isLanguageSupported,
1530
- } from '${bundleImport}';
1603
+ } from '@lokascript/semantic';
1531
1604
  `;
1605
+ }
1532
1606
  if (config.grammarEnabled) {
1533
1607
  code += `
1534
1608
  import { GrammarTransformer, translate } from '@lokascript/i18n';
@@ -1642,28 +1716,34 @@ function generateBundleCode(config) {
1642
1716
  htmxIntegration = false,
1643
1717
  globalName = "hyperfixi",
1644
1718
  positionalExpressions = false,
1645
- parserImportPath = "../parser/hybrid",
1646
1719
  autoInit = true,
1647
- esModule = true
1720
+ esModule = true,
1721
+ parserType = "hybrid"
1648
1722
  } = config;
1649
1723
  const needsStyleHelpers = commands.some((cmd) => STYLE_COMMANDS.includes(cmd));
1650
1724
  const needsElementArrayHelper = commands.some((cmd) => ELEMENT_ARRAY_COMMANDS.includes(cmd));
1725
+ const needsMorphlex = commands.some((cmd) => MORPH_COMMANDS.includes(cmd));
1651
1726
  const hasBlocks = blocks.length > 0;
1652
1727
  const hasReturn = commands.includes("return");
1653
1728
  const commandCases = commands.filter((cmd) => COMMAND_IMPLEMENTATIONS[cmd]).map((cmd) => COMMAND_IMPLEMENTATIONS[cmd]).join("\n");
1654
1729
  const blockCases = blocks.filter((block) => BLOCK_IMPLEMENTATIONS[block]).map((block) => BLOCK_IMPLEMENTATIONS[block]).join("\n");
1730
+ const parserTemplate = parserType === "lite" ? LITE_PARSER_TEMPLATE : HYBRID_PARSER_TEMPLATE;
1731
+ const morphlexImport = needsMorphlex ? `import { morph as morphlexMorph, morphInner as morphlexMorphInner } from 'morphlex';
1732
+
1733
+ ` : "";
1655
1734
  return `/**
1656
1735
  * LokaScript ${name} Bundle (Auto-Generated)
1657
1736
  *
1658
1737
  * Generated by: @lokascript/vite-plugin
1738
+ * Parser: ${parserType}
1659
1739
  * Commands: ${commands.join(", ")}${blocks.length > 0 ? `
1660
- * Blocks: ${blocks.join(", ")}` : ""}${positionalExpressions ? "\n * Positional expressions: enabled" : ""}
1740
+ * Blocks: ${blocks.join(", ")}` : ""}${positionalExpressions ? "\n * Positional expressions: enabled" : ""}${needsMorphlex ? "\n * Morphing: morphlex (~2.5KB)" : ""}
1661
1741
  *
1662
1742
  * DO NOT EDIT - This file is automatically regenerated.
1663
1743
  */
1664
1744
 
1665
- // Parser imports
1666
- import { HybridParser, addCommandAliases } from '${parserImportPath}';
1745
+ ${morphlexImport}// Embedded ${parserType} parser (no external dependencies)
1746
+ ${parserTemplate}
1667
1747
 
1668
1748
  // Runtime state
1669
1749
  const globalVars = new Map();
@@ -1962,8 +2042,7 @@ function processElement(el) {
1962
2042
  if (!code) return;
1963
2043
 
1964
2044
  try {
1965
- const parser = new HybridParser(code);
1966
- const ast = parser.parse();
2045
+ const ast = ${parserType === "lite" ? "parseLite(code)" : "new HybridParser(code).parse()"};
1967
2046
  executeAST(ast, el);
1968
2047
  } catch (err) {
1969
2048
  console.error('LokaScript ${name} error:', err, 'Code:', code);
@@ -1979,14 +2058,12 @@ const api = {
1979
2058
  version: '1.0.0-${name.toLowerCase().replace(/\s+/g, "-")}',
1980
2059
 
1981
2060
  parse(code) {
1982
- const parser = new HybridParser(code);
1983
- return parser.parse();
2061
+ return ${parserType === "lite" ? "parseLite(code)" : "new HybridParser(code).parse()"};
1984
2062
  },
1985
2063
 
1986
2064
  async execute(code, element) {
1987
2065
  const me = element || document.body;
1988
- const parser = new HybridParser(code);
1989
- const ast = parser.parse();
2066
+ const ast = ${parserType === "lite" ? "parseLite(code)" : "new HybridParser(code).parse()"};
1990
2067
  return executeAST(ast, me);
1991
2068
  },
1992
2069
 
@@ -1998,7 +2075,7 @@ const api = {
1998
2075
 
1999
2076
  commands: ${JSON.stringify(commands)},
2000
2077
  ${blocks.length > 0 ? `blocks: ${JSON.stringify(blocks)},` : ""}
2001
- parserName: 'hybrid',
2078
+ parserName: '${parserType}',
2002
2079
  };
2003
2080
  ${autoInit ? `
2004
2081
  if (typeof window !== 'undefined') {
@@ -2038,6 +2115,17 @@ var Generator = class {
2038
2115
  if (commands.length === 0 && blocks.length === 0 && !positional && !semanticConfig.enabled) {
2039
2116
  return this.generateEmptyBundle(options);
2040
2117
  }
2118
+ const unsupportedCommands = this.getUnsupportedCommands(commands);
2119
+ if (unsupportedCommands.length > 0) {
2120
+ if (this.debug) {
2121
+ console.log(
2122
+ `[hyperfixi] Commands detected that require full runtime: ${unsupportedCommands.join(", ")}
2123
+ Falling back to full browser bundle for complete functionality.`
2124
+ );
2125
+ }
2126
+ return this.generateDevFallback("full");
2127
+ }
2128
+ const parserType = canUseLiteParser(commands, blocks, positional) ? "lite" : "hybrid";
2041
2129
  const config = {
2042
2130
  name: options.bundleName ?? "ViteAutoGenerated",
2043
2131
  commands,
@@ -2045,8 +2133,7 @@ var Generator = class {
2045
2133
  positionalExpressions: positional,
2046
2134
  htmxIntegration: options.htmx ?? usage.htmx?.hasHtmxAttributes ?? false,
2047
2135
  globalName: options.globalName ?? "hyperfixi",
2048
- // Use @lokascript/core package path for virtual module
2049
- parserImportPath: "@lokascript/core/parser/hybrid",
2136
+ parserType,
2050
2137
  autoInit: true,
2051
2138
  esModule: true
2052
2139
  };
@@ -2056,7 +2143,8 @@ var Generator = class {
2056
2143
  blocks,
2057
2144
  positional,
2058
2145
  htmx: config.htmxIntegration,
2059
- semantic: semanticConfig.enabled
2146
+ semantic: semanticConfig.enabled,
2147
+ parserType
2060
2148
  };
2061
2149
  if (semanticConfig.enabled && semanticConfig.bundleType) {
2062
2150
  const sizeInfo = getSemanticBundleSize(semanticConfig.bundleType);
@@ -2091,7 +2179,7 @@ var Generator = class {
2091
2179
  bundleCode = bundleCode.slice(0, parserImportEnd) + semanticCode + "\n\n" + bundleCode.slice(parserImportEnd);
2092
2180
  }
2093
2181
  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,
2182
+ /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
2183
  `function processElement(el) {
2096
2184
  const code = el.getAttribute('_');
2097
2185
  if (!code) return;
@@ -2100,27 +2188,47 @@ var Generator = class {
2100
2188
  const ast = parseWithSemantic(code);`
2101
2189
  );
2102
2190
  bundleCode = bundleCode.replace(
2103
- /parse\(code\) \{\s*const parser = new HybridParser\(code\);\s*return parser\.parse\(\);/g,
2191
+ /parse\(code\) \{\s*return (?:new HybridParser\(code\)\.parse\(\)|parseLite\(code\));/g,
2104
2192
  `parse(code, lang = null) {
2105
2193
  return parseWithSemantic(code, lang);`
2106
2194
  );
2107
2195
  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,
2196
+ /async execute\(code, element\) \{\s*const me = element \|\| document\.body;\s*const ast = (?:new HybridParser\(code\)\.parse\(\)|parseLite\(code\));/g,
2109
2197
  `async execute(code, element, lang = null) {
2110
2198
  const me = element || document.body;
2111
2199
  const ast = parseWithSemantic(code, lang);`
2112
2200
  );
2113
2201
  if (semanticExports.length > 0) {
2114
- const apiExportsMarker = "parserName: 'hybrid',";
2202
+ const apiExportsMarkerRegex = /parserName: '(?:lite|hybrid)',/;
2115
2203
  const additionalApiProps = semanticExports.map((exp) => ` ${exp},`).join("\n");
2116
2204
  bundleCode = bundleCode.replace(
2117
- apiExportsMarker,
2118
- `parserName: 'hybrid',
2205
+ apiExportsMarkerRegex,
2206
+ `parserName: 'semantic',
2119
2207
  ${additionalApiProps}`
2120
2208
  );
2121
2209
  }
2122
2210
  return bundleCode;
2123
2211
  }
2212
+ /**
2213
+ * Get commands that require the full runtime and are not available in generated bundles
2214
+ */
2215
+ getUnsupportedCommands(commands) {
2216
+ const unsupported = [];
2217
+ for (const cmd of commands) {
2218
+ if (FULL_RUNTIME_ONLY_COMMANDS.includes(cmd)) {
2219
+ unsupported.push(cmd);
2220
+ } else if (!isAvailableCommand(cmd) && !COMMAND_IMPLEMENTATIONS[cmd]) {
2221
+ unsupported.push(cmd);
2222
+ }
2223
+ }
2224
+ return unsupported;
2225
+ }
2226
+ /**
2227
+ * Check if any commands require full runtime and should trigger fallback
2228
+ */
2229
+ requiresFullRuntime(commands) {
2230
+ return this.getUnsupportedCommands(commands).length > 0;
2231
+ }
2124
2232
  /**
2125
2233
  * Generate an empty bundle when no hyperscript is detected
2126
2234
  */