diorama-js 0.1.0 → 0.1.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
@@ -1524,6 +1524,18 @@ function getCDNProvider(name) {
1524
1524
  return esmShProvider;
1525
1525
  }
1526
1526
  }
1527
+ function buildCSSURL(specifier, options = {}) {
1528
+ const { dependencies = {}, cdnProvider = "esm.sh" } = options;
1529
+ const clean = specifier.replace(/[?#].*$/, "");
1530
+ const { packageName, subpath } = parseSpecifier(clean);
1531
+ const version = dependencies[packageName];
1532
+ const ver = version ? `@${version}` : "";
1533
+ const sub = subpath ? `/${subpath}` : "";
1534
+ if (cdnProvider === "unpkg") {
1535
+ return `https://unpkg.com/${packageName}${ver}${sub}`;
1536
+ }
1537
+ return `https://esm.sh/${packageName}${ver}${sub}`;
1538
+ }
1527
1539
  function rewriteImports(source, options = {}) {
1528
1540
  const {
1529
1541
  dependencies = {},
@@ -1537,6 +1549,9 @@ function rewriteImports(source, options = {}) {
1537
1549
  if (raw.startsWith(".") || raw.startsWith("/") || /^https?:\/\//.test(raw)) {
1538
1550
  return raw;
1539
1551
  }
1552
+ if (/\.css(?:[?#]|$)/.test(raw)) {
1553
+ return raw;
1554
+ }
1540
1555
  const { packageName, subpath } = parseSpecifier(raw);
1541
1556
  if (NODE_BUILTINS.has(packageName) || NODE_BUILTINS.has(packageName.replace("node:", ""))) {
1542
1557
  throw new NodeBuiltinError(packageName);
@@ -1739,7 +1754,7 @@ var ASSET_IMPORT_EXTENSIONS = {
1739
1754
  ".otf": "font/otf"
1740
1755
  };
1741
1756
  function assembleHTML(options) {
1742
- const { project, config, transformedFiles } = options;
1757
+ const { project, config, transformedFiles, cdnProvider } = options;
1743
1758
  const files = transformedFiles ?? project.files;
1744
1759
  switch (config.type) {
1745
1760
  case "static":
@@ -1749,7 +1764,7 @@ function assembleHTML(options) {
1749
1764
  case "typescript":
1750
1765
  case "jsx-typescript":
1751
1766
  case "vite":
1752
- return assembleESM(files, project, config);
1767
+ return assembleESM(files, project, config, cdnProvider);
1753
1768
  default:
1754
1769
  throw new AssemblyError(`Unsupported project type: ${config.type}`);
1755
1770
  }
@@ -1766,10 +1781,13 @@ function assembleStatic(files, project, config) {
1766
1781
  html = inlineAssets(html, project, config.entryPoint);
1767
1782
  return { html, usesESM: false };
1768
1783
  }
1769
- function assembleESM(files, project, config) {
1784
+ function assembleESM(files, project, config, cdnProvider) {
1770
1785
  const isGenerated = config.entryPoint === "__generated__/index.html";
1771
1786
  rewriteAssetImports(files, project);
1772
- const cssFromJS = extractCSSImports(files);
1787
+ const { css: cssFromJS, links: cssLinks } = extractCSSImports(files, {
1788
+ dependencies: config.dependencies,
1789
+ cdnProvider
1790
+ });
1773
1791
  if (config.isVite) {
1774
1792
  injectImportMetaEnv(files);
1775
1793
  }
@@ -1788,6 +1806,9 @@ function assembleESM(files, project, config) {
1788
1806
  html = rewriteScriptSrcsToImportMap(html, config.entryPoint);
1789
1807
  html = inlineAssets(html, project, config.entryPoint);
1790
1808
  }
1809
+ for (const href of cssLinks) {
1810
+ html = injectIntoHead(html, `<link rel="stylesheet" href="${href}">`);
1811
+ }
1791
1812
  if (cssFromJS) {
1792
1813
  html = injectIntoHead(html, `<style>
1793
1814
  ${cssFromJS}
@@ -2037,20 +2058,32 @@ function rewriteAssetImports(files, project) {
2037
2058
  }
2038
2059
  }
2039
2060
  }
2040
- function extractCSSImports(files) {
2061
+ function extractCSSImports(files, options = {}) {
2041
2062
  const cssChunks = [];
2042
- const cssImportRe = /import\s+['"]([^'"]+\.css)['"]\s*;?/g;
2063
+ const links = [];
2064
+ const seenLinks = /* @__PURE__ */ new Set();
2065
+ const cssImportRe = /import\s+['"]([^'"]+\.css(?:[?#][^'"]*)?)['"]\s*;?/g;
2043
2066
  for (const [path, content] of files) {
2044
2067
  if (!/\.(js|ts|jsx|tsx|mjs)$/.test(path)) continue;
2045
2068
  let modified = content;
2046
2069
  let match;
2070
+ cssImportRe.lastIndex = 0;
2047
2071
  while ((match = cssImportRe.exec(content)) !== null) {
2048
- const cssPath = match[1];
2049
- const resolved = resolvePath(directoryOf(path), cssPath);
2050
- const css = files.get(resolved);
2051
- if (css) {
2052
- cssChunks.push(`/* ${resolved} */
2072
+ const specifier = match[1];
2073
+ const bare = specifier.replace(/[?#].*$/, "");
2074
+ if (bare.startsWith(".") || bare.startsWith("/")) {
2075
+ const resolved = resolvePath(directoryOf(path), bare);
2076
+ const css = files.get(resolved);
2077
+ if (css) {
2078
+ cssChunks.push(`/* ${resolved} */
2053
2079
  ${css}`);
2080
+ }
2081
+ } else {
2082
+ const href = /^https?:\/\//.test(bare) ? bare : buildCSSURL(bare, options);
2083
+ if (!seenLinks.has(href)) {
2084
+ seenLinks.add(href);
2085
+ links.push(href);
2086
+ }
2054
2087
  }
2055
2088
  modified = modified.replace(match[0], "");
2056
2089
  }
@@ -2058,7 +2091,7 @@ ${css}`);
2058
2091
  files.set(path, modified);
2059
2092
  }
2060
2093
  }
2061
- return cssChunks.join("\n\n");
2094
+ return { css: cssChunks.join("\n\n"), links };
2062
2095
  }
2063
2096
  function injectImportMetaEnv(files) {
2064
2097
  const shim = `if(!import.meta.env){Object.defineProperty(import.meta,'env',{value:{MODE:'production',BASE_URL:'/',PROD:true,DEV:false,SSR:false}});}`;
@@ -2169,7 +2202,8 @@ var Diorama = class {
2169
2202
  const { html: html2, usesESM: usesESM2 } = assembleHTML({
2170
2203
  project,
2171
2204
  config,
2172
- transformedFiles: files
2205
+ transformedFiles: files,
2206
+ cdnProvider: this.options.cdnProvider
2173
2207
  });
2174
2208
  return { html: html2, usesESM: usesESM2, repoName: repoName2 };
2175
2209
  };