akhaliq HF Staff commited on
Commit
180e81b
·
1 Parent(s): 000b511

update svelte

Browse files
Files changed (1) hide show
  1. app.py +112 -0
app.py CHANGED
@@ -354,6 +354,9 @@ Output format (CRITICAL):
354
  (repeat for all files you decide to create)
355
  - Do NOT wrap files in Markdown code fences.
356
 
 
 
 
357
  Requirements:
358
  1. Create a modern, responsive Svelte application based on the user's specific request
359
  2. Prefer TypeScript where applicable for better type safety
@@ -385,6 +388,9 @@ Output format (CRITICAL):
385
  (repeat for all files you decide to create)
386
  - Do NOT wrap files in Markdown code fences.
387
 
 
 
 
388
  Requirements:
389
  1. Create a modern, responsive Svelte application
390
  2. Prefer TypeScript where applicable
@@ -1650,6 +1656,100 @@ def format_svelte_output(files):
1650
  """Format Svelte files into === filename === sections (generic)."""
1651
  return format_multipage_output(files)
1652
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1653
  def history_render(history: History):
1654
  return gr.update(visible=True), history
1655
 
@@ -7603,6 +7703,18 @@ with gr.Blocks(
7603
  if not isinstance(files, dict) or 'src/App.svelte' not in files or not files['src/App.svelte'].strip():
7604
  return gr.update(value="Error: Could not parse Svelte output (missing src/App.svelte). Please regenerate the code.", visible=True)
7605
 
 
 
 
 
 
 
 
 
 
 
 
 
7606
  # Write all files to a temp directory and upload folder in one commit
7607
  import tempfile, os
7608
  with tempfile.TemporaryDirectory() as tmpdir:
 
354
  (repeat for all files you decide to create)
355
  - Do NOT wrap files in Markdown code fences.
356
 
357
+ Dependency policy:
358
+ - If you import any third-party npm packages (e.g., "@gradio/dataframe"), include a package.json at the project root with a "dependencies" section listing them. Keep scripts and devDependencies compatible with the default Svelte + Vite template.
359
+
360
  Requirements:
361
  1. Create a modern, responsive Svelte application based on the user's specific request
362
  2. Prefer TypeScript where applicable for better type safety
 
388
  (repeat for all files you decide to create)
389
  - Do NOT wrap files in Markdown code fences.
390
 
391
+ Dependency policy:
392
+ - If you import any third-party npm packages, include a package.json at the project root with a "dependencies" section listing them. Keep scripts and devDependencies compatible with the default Svelte + Vite template.
393
+
394
  Requirements:
395
  1. Create a modern, responsive Svelte application
396
  2. Prefer TypeScript where applicable
 
1656
  """Format Svelte files into === filename === sections (generic)."""
1657
  return format_multipage_output(files)
1658
 
1659
+ def infer_svelte_dependencies(files: Dict[str, str]) -> Dict[str, str]:
1660
+ """Infer npm dependencies from Svelte/TS imports across generated files.
1661
+
1662
+ Returns mapping of package name -> semver (string). Uses conservative defaults
1663
+ when versions aren't known. Adds special-cased versions when known.
1664
+ """
1665
+ import re as _re
1666
+ deps: Dict[str, str] = {}
1667
+ import_from = _re.compile(r"import\s+[^;]*?from\s+['\"]([^'\"]+)['\"]", _re.IGNORECASE)
1668
+ bare_import = _re.compile(r"import\s+['\"]([^'\"]+)['\"]", _re.IGNORECASE)
1669
+
1670
+ def maybe_add(pkg: str):
1671
+ if not pkg or pkg.startswith('.') or pkg.startswith('/') or pkg.startswith('http'):
1672
+ return
1673
+ if pkg.startswith('svelte'):
1674
+ return
1675
+ if pkg not in deps:
1676
+ # Default to wildcard; adjust known packages below
1677
+ deps[pkg] = "*"
1678
+
1679
+ for path, content in (files or {}).items():
1680
+ if not isinstance(content, str):
1681
+ continue
1682
+ for m in import_from.finditer(content):
1683
+ maybe_add(m.group(1))
1684
+ for m in bare_import.finditer(content):
1685
+ maybe_add(m.group(1))
1686
+
1687
+ # Pin known versions when sensible
1688
+ if '@gradio/dataframe' in deps:
1689
+ deps['@gradio/dataframe'] = '^0.19.1'
1690
+
1691
+ return deps
1692
+
1693
+ def build_svelte_package_json(existing_json_text: Optional[str], detected_dependencies: Dict[str, str]) -> str:
1694
+ """Create or merge a package.json for Svelte spaces.
1695
+
1696
+ - If existing_json_text is provided, merge detected deps into its dependencies.
1697
+ - Otherwise, start from the template defaults provided by the user and add deps.
1698
+ - Always preserve template scripts and devDependencies.
1699
+ """
1700
+ import json as _json
1701
+ # Template from the user's Svelte space scaffold
1702
+ template = {
1703
+ "name": "svelte",
1704
+ "private": True,
1705
+ "version": "0.0.0",
1706
+ "type": "module",
1707
+ "scripts": {
1708
+ "dev": "vite",
1709
+ "build": "vite build",
1710
+ "preview": "vite preview",
1711
+ "check": "svelte-check --tsconfig ./tsconfig.app.json && tsc -p tsconfig.node.json"
1712
+ },
1713
+ "devDependencies": {
1714
+ "@sveltejs/vite-plugin-svelte": "^5.0.3",
1715
+ "@tsconfig/svelte": "^5.0.4",
1716
+ "svelte": "^5.28.1",
1717
+ "svelte-check": "^4.1.6",
1718
+ "typescript": "~5.8.3",
1719
+ "vite": "^6.3.5"
1720
+ }
1721
+ }
1722
+
1723
+ result = template
1724
+ if existing_json_text:
1725
+ try:
1726
+ parsed = _json.loads(existing_json_text)
1727
+ # Merge with template as base, keeping template scripts/devDependencies if missing in parsed
1728
+ result = {
1729
+ **template,
1730
+ **{k: v for k, v in parsed.items() if k not in ("scripts", "devDependencies")},
1731
+ }
1732
+ # If parsed contains its own scripts/devDependencies, prefer parsed to respect user's file
1733
+ if isinstance(parsed.get("scripts"), dict):
1734
+ result["scripts"] = parsed["scripts"]
1735
+ if isinstance(parsed.get("devDependencies"), dict):
1736
+ result["devDependencies"] = parsed["devDependencies"]
1737
+ except Exception:
1738
+ # Fallback to template if parse fails
1739
+ result = template
1740
+
1741
+ # Merge dependencies
1742
+ existing_deps = result.get("dependencies", {})
1743
+ if not isinstance(existing_deps, dict):
1744
+ existing_deps = {}
1745
+ merged = {**existing_deps, **(detected_dependencies or {})}
1746
+ if merged:
1747
+ result["dependencies"] = merged
1748
+ else:
1749
+ result.pop("dependencies", None)
1750
+
1751
+ return _json.dumps(result, indent=2, ensure_ascii=False) + "\n"
1752
+
1753
  def history_render(history: History):
1754
  return gr.update(visible=True), history
1755
 
 
7703
  if not isinstance(files, dict) or 'src/App.svelte' not in files or not files['src/App.svelte'].strip():
7704
  return gr.update(value="Error: Could not parse Svelte output (missing src/App.svelte). Please regenerate the code.", visible=True)
7705
 
7706
+ # Ensure package.json includes any external npm deps used; overwrite template's package.json
7707
+ try:
7708
+ detected = infer_svelte_dependencies(files)
7709
+ existing_pkg_text = files.get('package.json')
7710
+ pkg_text = build_svelte_package_json(existing_pkg_text, detected)
7711
+ # Only write if we have either detected deps or user provided a package.json
7712
+ if pkg_text and (detected or existing_pkg_text is not None):
7713
+ files['package.json'] = pkg_text
7714
+ except Exception as e:
7715
+ # Non-fatal: proceed without generating package.json
7716
+ print(f"[Svelte Deploy] package.json synthesis skipped: {e}")
7717
+
7718
  # Write all files to a temp directory and upload folder in one commit
7719
  import tempfile, os
7720
  with tempfile.TemporaryDirectory() as tmpdir: