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/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);
@@ -2093,6 +2111,94 @@ ${css}`);
2093
2111
  }
2094
2112
  return { css: cssChunks.join("\n\n"), links };
2095
2113
  }
2114
+ var TAILWIND_DIRECTIVE_RE = /@tailwind\b|@apply\b/;
2115
+ var TAILWIND_CDN_URL = "https://cdn.tailwindcss.com";
2116
+ function hasTailwindDirectives(css) {
2117
+ return TAILWIND_DIRECTIVE_RE.test(css);
2118
+ }
2119
+ function applyTailwindRuntime(html, project, config, tailwind) {
2120
+ const enabled = tailwind === true || tailwind === "auto" && (config.usesTailwind ?? usesTailwindCSS(project.files));
2121
+ if (!enabled) return html;
2122
+ html = html.replace(
2123
+ /<style>([\s\S]*?)<\/style>/g,
2124
+ (match, css) => hasTailwindDirectives(css) ? `<style type="text/tailwindcss">${css}</style>` : match
2125
+ );
2126
+ let injection = `<script src="${TAILWIND_CDN_URL}"></script>`;
2127
+ const configObject = extractTailwindConfig(project.files);
2128
+ if (configObject) {
2129
+ injection += `
2130
+ <script>tailwind.config = ${configObject};</script>`;
2131
+ }
2132
+ return injectIntoHead(html, injection);
2133
+ }
2134
+ function extractTailwindConfig(files) {
2135
+ let source;
2136
+ for (const [path, content] of files) {
2137
+ if (/(?:^|\/)tailwind\.config\.(?:js|cjs|mjs|ts)$/.test(path)) {
2138
+ source = content;
2139
+ break;
2140
+ }
2141
+ }
2142
+ if (!source) return null;
2143
+ const object = extractBalancedObject(source);
2144
+ if (!object || !isSafeConfigObject(object)) return null;
2145
+ return object;
2146
+ }
2147
+ function extractBalancedObject(source) {
2148
+ const opener = source.match(/(?:export\s+default|module\.exports\s*=)\s*\{/);
2149
+ if (!opener || opener.index === void 0) return null;
2150
+ const start = opener.index + opener[0].length - 1;
2151
+ let depth = 0;
2152
+ let str = null;
2153
+ let lineComment = false;
2154
+ let blockComment = false;
2155
+ for (let i = start; i < source.length; i++) {
2156
+ const c = source[i];
2157
+ const n = source[i + 1];
2158
+ if (lineComment) {
2159
+ if (c === "\n") lineComment = false;
2160
+ continue;
2161
+ }
2162
+ if (blockComment) {
2163
+ if (c === "*" && n === "/") {
2164
+ blockComment = false;
2165
+ i++;
2166
+ }
2167
+ continue;
2168
+ }
2169
+ if (str) {
2170
+ if (c === "\\") {
2171
+ i++;
2172
+ continue;
2173
+ }
2174
+ if (c === str) str = null;
2175
+ continue;
2176
+ }
2177
+ if (c === "/" && n === "/") {
2178
+ lineComment = true;
2179
+ i++;
2180
+ continue;
2181
+ }
2182
+ if (c === "/" && n === "*") {
2183
+ blockComment = true;
2184
+ i++;
2185
+ continue;
2186
+ }
2187
+ if (c === '"' || c === "'" || c === "`") {
2188
+ str = c;
2189
+ continue;
2190
+ }
2191
+ if (c === "{") depth++;
2192
+ else if (c === "}") {
2193
+ depth--;
2194
+ if (depth === 0) return source.slice(start, i + 1);
2195
+ }
2196
+ }
2197
+ return null;
2198
+ }
2199
+ function isSafeConfigObject(object) {
2200
+ 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));
2201
+ }
2096
2202
  function injectImportMetaEnv(files) {
2097
2203
  const shim = `if(!import.meta.env){Object.defineProperty(import.meta,'env',{value:{MODE:'production',BASE_URL:'/',PROD:true,DEV:false,SSR:false}});}`;
2098
2204
  for (const [path, content] of files) {
@@ -2203,7 +2309,8 @@ var Diorama = class {
2203
2309
  project,
2204
2310
  config,
2205
2311
  transformedFiles: files,
2206
- cdnProvider: this.options.cdnProvider
2312
+ cdnProvider: this.options.cdnProvider,
2313
+ tailwind: options.tailwind ?? "auto"
2207
2314
  });
2208
2315
  return { html: html2, usesESM: usesESM2, repoName: repoName2 };
2209
2316
  };