@stacksjs/stx 0.2.124 → 0.2.126

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.
@@ -12,6 +12,19 @@ export declare function extractVariables(scriptContent: string, context: Record<
12
12
  * @param scriptContent - ES module style script content
13
13
  * @returns CommonJS compatible script
14
14
  */
15
+ /**
16
+ * Whether an import statement is finished, or continues on the next line.
17
+ *
18
+ * Finished means the module specifier has been reached and its quotes are
19
+ * closed, with no unclosed brace left over. A side-effect import
20
+ * (`import './setup'`) is complete as soon as its string closes; a named
21
+ * import is not complete until both the brace list and the specifier are.
22
+ *
23
+ * Exported for the tests, because the failure it prevents is invisible from
24
+ * the outside: an unjoined import produces a syntax error in generated code,
25
+ * and the page then renders with every server variable undefined.
26
+ */
27
+ export declare function isCompleteImport(statement: string): boolean;
15
28
  export declare function convertToCommonJS(scriptContent: string, filePath?: string): string;
16
29
  /**
17
30
  * Extract script content from an HTML template
@@ -484,6 +484,31 @@ export async function extractVariables(scriptContent, context, filePath, options
484
484
  }
485
485
  }
486
486
  }
487
+ export function isCompleteImport(statement) {
488
+ let braces = 0, quote = null, sawFrom = !1, closedSpecifier = !1;
489
+ for (let index = 0;index < statement.length; index++) {
490
+ const character = statement[index];
491
+ if (quote) {
492
+ if (character === quote) {
493
+ quote = null;
494
+ if (sawFrom || braces === 0)
495
+ closedSpecifier = !0;
496
+ }
497
+ continue;
498
+ }
499
+ if (character === "'" || character === '"' || character === "`") {
500
+ quote = character;
501
+ continue;
502
+ }
503
+ if (character === "{")
504
+ braces++;
505
+ else if (character === "}")
506
+ braces--;
507
+ else if (statement.startsWith("from", index) && braces === 0)
508
+ sawFrom = !0;
509
+ }
510
+ return quote === null && braces === 0 && closedSpecifier;
511
+ }
487
512
  export function convertToCommonJS(scriptContent, filePath) {
488
513
  const templateDir = filePath ? path.dirname(filePath) : process.cwd(), lines = scriptContent.split(`
489
514
  `), convertedLines = [];
@@ -546,8 +571,14 @@ export function convertToCommonJS(scriptContent, filePath) {
546
571
  i = lines.length;
547
572
  continue;
548
573
  }
549
- if (line.startsWith("import ")) {
550
- const defaultImportMatch = line.match(/^import\s+(\w+)\s+from\s+['"](.+)['"]/), namedImportMatch = line.match(/^import\s+\{([^}]+)\}\s+from\s+['"](.+)['"]/), sideEffectMatch = line.match(/^import\s+['"](.+)['"]/), resolveSource = (source) => {
574
+ if (line.startsWith("import ") || line === "import" || line.startsWith("import{")) {
575
+ let statement = line, consumed = 0;
576
+ while (!isCompleteImport(statement) && i + consumed + 1 < lines.length) {
577
+ consumed++;
578
+ statement = `${statement} ${lines[i + consumed].trim()}`;
579
+ }
580
+ i += consumed;
581
+ const defaultImportMatch = statement.match(/^import\s+(\w+)\s+from\s+['"](.+?)['"]/), namedImportMatch = statement.match(/^import\s+\{([^}]*)\}\s+from\s+['"](.+?)['"]/), sideEffectMatch = statement.match(/^import\s+['"](.+?)['"]/), line_ = statement, resolveSource = (source) => {
551
582
  if (source.startsWith("."))
552
583
  return path.resolve(templateDir, source);
553
584
  return source;
@@ -568,7 +599,7 @@ export function convertToCommonJS(scriptContent, filePath) {
568
599
  const resolved = resolveSource(sideEffectMatch[1]);
569
600
  convertedLines.push(`await import('${resolved}')`);
570
601
  } else
571
- convertedLines.push(line);
602
+ convertedLines.push(line_);
572
603
  i++;
573
604
  continue;
574
605
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/stx",
3
3
  "type": "module",
4
- "version": "0.2.124",
4
+ "version": "0.2.126",
5
5
  "description": "A performant UI Framework. Powered by Bun.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",
@@ -117,12 +117,12 @@
117
117
  "dependencies": {
118
118
  "@cwcss/crosswind": "^0.2.12",
119
119
  "@stacksjs/clapp": "^0.2.0",
120
- "@stacksjs/desktop": "0.2.124",
121
- "@stacksjs/sanitizer": "0.2.124",
120
+ "@stacksjs/desktop": "0.2.126",
121
+ "@stacksjs/sanitizer": "0.2.126",
122
122
  "@stacksjs/ts-i18n": "^0.1.11",
123
- "bun-plugin-stx": "0.2.124",
123
+ "bun-plugin-stx": "0.2.126",
124
124
  "bunfig": "^0.15.15",
125
- "stx-router": "0.2.124",
125
+ "stx-router": "0.2.126",
126
126
  "ts-broadcasting": "^0.0.9",
127
127
  "ts-images": "^0.1.9",
128
128
  "ts-syntax-highlighter": "^0.2.1"