diorama-js 0.1.1 → 0.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
@@ -640,6 +640,7 @@ function analyzeProject(project, overrideType, overrideEntry) {
640
640
  hasBareImports: hasBareImports(files),
641
641
  isVite
642
642
  });
643
+ const usesTailwind = usesTailwindCSS(files);
643
644
  return {
644
645
  type,
645
646
  entryPoint: htmlEntry,
@@ -648,7 +649,8 @@ function analyzeProject(project, overrideType, overrideEntry) {
648
649
  hasJSX,
649
650
  hasTypeScript,
650
651
  jsEntryPoint: jsEntry,
651
- isVite
652
+ isVite,
653
+ usesTailwind
652
654
  };
653
655
  }
654
656
  function detectProjectType(input) {
@@ -670,6 +672,17 @@ function hasBareImports(files) {
670
672
  }
671
673
  return false;
672
674
  }
675
+ function usesTailwindCSS(files) {
676
+ for (const [path, content] of files) {
677
+ if (/(?:^|\/)tailwind\.config\.(?:js|cjs|mjs|ts)$/.test(path)) {
678
+ return true;
679
+ }
680
+ if (path.endsWith(".css") && /@tailwind\b|@apply\b/.test(content)) {
681
+ return true;
682
+ }
683
+ }
684
+ return false;
685
+ }
673
686
 
674
687
  // src/core/sandbox.ts
675
688
  function buildErrorHTML(message) {
@@ -1754,20 +1767,25 @@ var ASSET_IMPORT_EXTENSIONS = {
1754
1767
  ".otf": "font/otf"
1755
1768
  };
1756
1769
  function assembleHTML(options) {
1757
- const { project, config, transformedFiles, cdnProvider } = options;
1770
+ const { project, config, transformedFiles, cdnProvider, tailwind = "auto" } = options;
1758
1771
  const files = transformedFiles ?? project.files;
1772
+ let result;
1759
1773
  switch (config.type) {
1760
1774
  case "static":
1761
- return assembleStatic(files, project, config);
1775
+ result = assembleStatic(files, project, config);
1776
+ break;
1762
1777
  case "static-esm":
1763
1778
  case "jsx":
1764
1779
  case "typescript":
1765
1780
  case "jsx-typescript":
1766
1781
  case "vite":
1767
- return assembleESM(files, project, config, cdnProvider);
1782
+ result = assembleESM(files, project, config, cdnProvider);
1783
+ break;
1768
1784
  default:
1769
1785
  throw new AssemblyError(`Unsupported project type: ${config.type}`);
1770
1786
  }
1787
+ result.html = applyTailwindRuntime(result.html, project, config, tailwind);
1788
+ return result;
1771
1789
  }
1772
1790
  function assembleStatic(files, project, config) {
1773
1791
  let html = files.get(config.entryPoint);
@@ -1837,6 +1855,7 @@ ${importMapJSON}
1837
1855
  );
1838
1856
  }
1839
1857
  }
1858
+ html = injectIntoHead(html, `<script>${HISTORY_GUARD_SCRIPT}</script>`);
1840
1859
  return { html, usesESM: true };
1841
1860
  }
1842
1861
  function generateHTMLShell(files, config) {
@@ -2093,6 +2112,96 @@ ${css}`);
2093
2112
  }
2094
2113
  return { css: cssChunks.join("\n\n"), links };
2095
2114
  }
2115
+ var TAILWIND_DIRECTIVE_RE = /@tailwind\b|@apply\b/;
2116
+ var TAILWIND_CDN_URL = "https://cdn.tailwindcss.com";
2117
+ function hasTailwindDirectives(css) {
2118
+ return TAILWIND_DIRECTIVE_RE.test(css);
2119
+ }
2120
+ function applyTailwindRuntime(html, project, config, tailwind) {
2121
+ const enabled = tailwind === true || tailwind === "auto" && (config.usesTailwind ?? usesTailwindCSS(project.files));
2122
+ if (!enabled) return html;
2123
+ html = html.replace(
2124
+ /<style>([\s\S]*?)<\/style>/g,
2125
+ (match, css) => hasTailwindDirectives(css) ? `<style type="text/tailwindcss">${css}</style>` : match
2126
+ );
2127
+ let injection = "";
2128
+ const configObject = extractTailwindConfig(project.files);
2129
+ if (configObject) {
2130
+ injection += `<script>window.tailwind = { config: ${configObject} };</script>
2131
+ `;
2132
+ }
2133
+ injection += `<script src="${TAILWIND_CDN_URL}" defer></script>`;
2134
+ return injectIntoHead(html, injection);
2135
+ }
2136
+ function extractTailwindConfig(files) {
2137
+ let source;
2138
+ for (const [path, content] of files) {
2139
+ if (/(?:^|\/)tailwind\.config\.(?:js|cjs|mjs|ts)$/.test(path)) {
2140
+ source = content;
2141
+ break;
2142
+ }
2143
+ }
2144
+ if (!source) return null;
2145
+ const object = extractBalancedObject(source);
2146
+ if (!object || !isSafeConfigObject(object)) return null;
2147
+ return object;
2148
+ }
2149
+ function extractBalancedObject(source) {
2150
+ const opener = source.match(/(?:export\s+default|module\.exports\s*=)\s*\{/);
2151
+ if (!opener || opener.index === void 0) return null;
2152
+ const start = opener.index + opener[0].length - 1;
2153
+ let depth = 0;
2154
+ let str = null;
2155
+ let lineComment = false;
2156
+ let blockComment = false;
2157
+ for (let i = start; i < source.length; i++) {
2158
+ const c = source[i];
2159
+ const n = source[i + 1];
2160
+ if (lineComment) {
2161
+ if (c === "\n") lineComment = false;
2162
+ continue;
2163
+ }
2164
+ if (blockComment) {
2165
+ if (c === "*" && n === "/") {
2166
+ blockComment = false;
2167
+ i++;
2168
+ }
2169
+ continue;
2170
+ }
2171
+ if (str) {
2172
+ if (c === "\\") {
2173
+ i++;
2174
+ continue;
2175
+ }
2176
+ if (c === str) str = null;
2177
+ continue;
2178
+ }
2179
+ if (c === "/" && n === "/") {
2180
+ lineComment = true;
2181
+ i++;
2182
+ continue;
2183
+ }
2184
+ if (c === "/" && n === "*") {
2185
+ blockComment = true;
2186
+ i++;
2187
+ continue;
2188
+ }
2189
+ if (c === '"' || c === "'" || c === "`") {
2190
+ str = c;
2191
+ continue;
2192
+ }
2193
+ if (c === "{") depth++;
2194
+ else if (c === "}") {
2195
+ depth--;
2196
+ if (depth === 0) return source.slice(start, i + 1);
2197
+ }
2198
+ }
2199
+ return null;
2200
+ }
2201
+ function isSafeConfigObject(object) {
2202
+ 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));
2203
+ }
2204
+ var HISTORY_GUARD_SCRIPT = "try{for(const m of ['pushState','replaceState']){const orig=history[m].bind(history);history[m]=function(state,title,url){try{return orig(state,title,url);}catch(e){try{return orig(state,title);}catch(e2){}}};}}catch(e){}";
2096
2205
  function injectImportMetaEnv(files) {
2097
2206
  const shim = `if(!import.meta.env){Object.defineProperty(import.meta,'env',{value:{MODE:'production',BASE_URL:'/',PROD:true,DEV:false,SSR:false}});}`;
2098
2207
  for (const [path, content] of files) {
@@ -2203,7 +2312,8 @@ var Diorama = class {
2203
2312
  project,
2204
2313
  config,
2205
2314
  transformedFiles: files,
2206
- cdnProvider: this.options.cdnProvider
2315
+ cdnProvider: this.options.cdnProvider,
2316
+ tailwind: options.tailwind ?? "auto"
2207
2317
  });
2208
2318
  return { html: html2, usesESM: usesESM2, repoName: repoName2 };
2209
2319
  };