diorama-js 0.1.1 → 0.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/react.cjs CHANGED
@@ -626,6 +626,7 @@ function analyzeProject(project, overrideType, overrideEntry) {
626
626
  hasBareImports: hasBareImports(files),
627
627
  isVite
628
628
  });
629
+ const usesTailwind = usesTailwindCSS(files);
629
630
  return {
630
631
  type,
631
632
  entryPoint: htmlEntry,
@@ -634,7 +635,8 @@ function analyzeProject(project, overrideType, overrideEntry) {
634
635
  hasJSX,
635
636
  hasTypeScript,
636
637
  jsEntryPoint: jsEntry,
637
- isVite
638
+ isVite,
639
+ usesTailwind
638
640
  };
639
641
  }
640
642
  function detectProjectType(input) {
@@ -656,6 +658,17 @@ function hasBareImports(files) {
656
658
  }
657
659
  return false;
658
660
  }
661
+ function usesTailwindCSS(files) {
662
+ for (const [path, content] of files) {
663
+ if (/(?:^|\/)tailwind\.config\.(?:js|cjs|mjs|ts)$/.test(path)) {
664
+ return true;
665
+ }
666
+ if (path.endsWith(".css") && /@tailwind\b|@apply\b/.test(content)) {
667
+ return true;
668
+ }
669
+ }
670
+ return false;
671
+ }
659
672
 
660
673
  // src/core/sandbox.ts
661
674
  function buildErrorHTML(message) {
@@ -1740,20 +1753,25 @@ var ASSET_IMPORT_EXTENSIONS = {
1740
1753
  ".otf": "font/otf"
1741
1754
  };
1742
1755
  function assembleHTML(options) {
1743
- const { project, config, transformedFiles, cdnProvider } = options;
1756
+ const { project, config, transformedFiles, cdnProvider, tailwind = "auto" } = options;
1744
1757
  const files = transformedFiles ?? project.files;
1758
+ let result;
1745
1759
  switch (config.type) {
1746
1760
  case "static":
1747
- return assembleStatic(files, project, config);
1761
+ result = assembleStatic(files, project, config);
1762
+ break;
1748
1763
  case "static-esm":
1749
1764
  case "jsx":
1750
1765
  case "typescript":
1751
1766
  case "jsx-typescript":
1752
1767
  case "vite":
1753
- return assembleESM(files, project, config, cdnProvider);
1768
+ result = assembleESM(files, project, config, cdnProvider);
1769
+ break;
1754
1770
  default:
1755
1771
  throw new AssemblyError(`Unsupported project type: ${config.type}`);
1756
1772
  }
1773
+ result.html = applyTailwindRuntime(result.html, project, config, tailwind);
1774
+ return result;
1757
1775
  }
1758
1776
  function assembleStatic(files, project, config) {
1759
1777
  let html = files.get(config.entryPoint);
@@ -2079,6 +2097,94 @@ ${css}`);
2079
2097
  }
2080
2098
  return { css: cssChunks.join("\n\n"), links };
2081
2099
  }
2100
+ var TAILWIND_DIRECTIVE_RE = /@tailwind\b|@apply\b/;
2101
+ var TAILWIND_CDN_URL = "https://cdn.tailwindcss.com";
2102
+ function hasTailwindDirectives(css) {
2103
+ return TAILWIND_DIRECTIVE_RE.test(css);
2104
+ }
2105
+ function applyTailwindRuntime(html, project, config, tailwind) {
2106
+ const enabled = tailwind === true || tailwind === "auto" && (config.usesTailwind ?? usesTailwindCSS(project.files));
2107
+ if (!enabled) return html;
2108
+ html = html.replace(
2109
+ /<style>([\s\S]*?)<\/style>/g,
2110
+ (match, css) => hasTailwindDirectives(css) ? `<style type="text/tailwindcss">${css}</style>` : match
2111
+ );
2112
+ let injection = `<script src="${TAILWIND_CDN_URL}"></script>`;
2113
+ const configObject = extractTailwindConfig(project.files);
2114
+ if (configObject) {
2115
+ injection += `
2116
+ <script>tailwind.config = ${configObject};</script>`;
2117
+ }
2118
+ return injectIntoHead(html, injection);
2119
+ }
2120
+ function extractTailwindConfig(files) {
2121
+ let source;
2122
+ for (const [path, content] of files) {
2123
+ if (/(?:^|\/)tailwind\.config\.(?:js|cjs|mjs|ts)$/.test(path)) {
2124
+ source = content;
2125
+ break;
2126
+ }
2127
+ }
2128
+ if (!source) return null;
2129
+ const object = extractBalancedObject(source);
2130
+ if (!object || !isSafeConfigObject(object)) return null;
2131
+ return object;
2132
+ }
2133
+ function extractBalancedObject(source) {
2134
+ const opener = source.match(/(?:export\s+default|module\.exports\s*=)\s*\{/);
2135
+ if (!opener || opener.index === void 0) return null;
2136
+ const start = opener.index + opener[0].length - 1;
2137
+ let depth = 0;
2138
+ let str = null;
2139
+ let lineComment = false;
2140
+ let blockComment = false;
2141
+ for (let i = start; i < source.length; i++) {
2142
+ const c = source[i];
2143
+ const n = source[i + 1];
2144
+ if (lineComment) {
2145
+ if (c === "\n") lineComment = false;
2146
+ continue;
2147
+ }
2148
+ if (blockComment) {
2149
+ if (c === "*" && n === "/") {
2150
+ blockComment = false;
2151
+ i++;
2152
+ }
2153
+ continue;
2154
+ }
2155
+ if (str) {
2156
+ if (c === "\\") {
2157
+ i++;
2158
+ continue;
2159
+ }
2160
+ if (c === str) str = null;
2161
+ continue;
2162
+ }
2163
+ if (c === "/" && n === "/") {
2164
+ lineComment = true;
2165
+ i++;
2166
+ continue;
2167
+ }
2168
+ if (c === "/" && n === "*") {
2169
+ blockComment = true;
2170
+ i++;
2171
+ continue;
2172
+ }
2173
+ if (c === '"' || c === "'" || c === "`") {
2174
+ str = c;
2175
+ continue;
2176
+ }
2177
+ if (c === "{") depth++;
2178
+ else if (c === "}") {
2179
+ depth--;
2180
+ if (depth === 0) return source.slice(start, i + 1);
2181
+ }
2182
+ }
2183
+ return null;
2184
+ }
2185
+ function isSafeConfigObject(object) {
2186
+ return !(/\brequire\s*\(/.test(object) || /\bimport\b/.test(object) || /=>/.test(object) || /\bfunction\b/.test(object) || /`/.test(object) || /\bprocess\b/.test(object) || /\b__dirname\b|\b__filename\b/.test(object));
2187
+ }
2082
2188
  function injectImportMetaEnv(files) {
2083
2189
  const shim = `if(!import.meta.env){Object.defineProperty(import.meta,'env',{value:{MODE:'production',BASE_URL:'/',PROD:true,DEV:false,SSR:false}});}`;
2084
2190
  for (const [path, content] of files) {
@@ -2189,7 +2295,8 @@ var Diorama = class {
2189
2295
  project,
2190
2296
  config,
2191
2297
  transformedFiles: files,
2192
- cdnProvider: this.options.cdnProvider
2298
+ cdnProvider: this.options.cdnProvider,
2299
+ tailwind: options.tailwind ?? "auto"
2193
2300
  });
2194
2301
  return { html: html2, usesESM: usesESM2, repoName: repoName2 };
2195
2302
  };
@@ -2302,6 +2409,7 @@ var DioramaPreview = react.forwardRef(
2302
2409
  height = "500px",
2303
2410
  frame,
2304
2411
  expand,
2412
+ tailwind,
2305
2413
  onLoad,
2306
2414
  onError,
2307
2415
  options,
@@ -2328,6 +2436,7 @@ var DioramaPreview = react.forwardRef(
2328
2436
  height,
2329
2437
  frame,
2330
2438
  expand,
2439
+ tailwind,
2331
2440
  onLoad,
2332
2441
  onError: (err) => {
2333
2442
  onError?.(err);
@@ -2348,7 +2457,7 @@ var DioramaPreview = react.forwardRef(
2348
2457
  instanceRef.current?.destroy();
2349
2458
  instanceRef.current = null;
2350
2459
  };
2351
- }, [repo, branch, subdirectory, loading, placeholder, height, frame, expand]);
2460
+ }, [repo, branch, subdirectory, loading, placeholder, height, frame, expand, tailwind]);
2352
2461
  const containerStyle = {
2353
2462
  width: "100%",
2354
2463
  minHeight: height,