@taujs/server 0.5.6 → 0.5.7

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.
Files changed (2) hide show
  1. package/dist/index.js +35 -25
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1249,7 +1249,7 @@ import { pathToFileURL } from "url";
1249
1249
 
1250
1250
  // src/utils/Templates.ts
1251
1251
  var CSS_LANGS_RE = /\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/;
1252
- async function collectStyle(server, entries) {
1252
+ var collectStyle = async (server, entries) => {
1253
1253
  const urls = await collectStyleUrls(server, entries);
1254
1254
  const codes = await Promise.all(
1255
1255
  urls.map(async (url) => {
@@ -1258,7 +1258,7 @@ async function collectStyle(server, entries) {
1258
1258
  })
1259
1259
  );
1260
1260
  return codes.flat().filter(Boolean).join("\n\n");
1261
- }
1261
+ };
1262
1262
  async function collectStyleUrls(server, entries) {
1263
1263
  const visited = /* @__PURE__ */ new Set();
1264
1264
  async function traverse(url) {
@@ -1273,7 +1273,7 @@ async function collectStyleUrls(server, entries) {
1273
1273
  await Promise.all(entries.map((url) => traverse(url)));
1274
1274
  return [...visited].filter((url) => url.match(CSS_LANGS_RE));
1275
1275
  }
1276
- function renderPreloadLinks(ssrManifest, basePath = "") {
1276
+ var renderPreloadLinks = (ssrManifest, basePath = "") => {
1277
1277
  const seen = /* @__PURE__ */ new Set();
1278
1278
  let links = "";
1279
1279
  for (const moduleId in ssrManifest) {
@@ -1288,8 +1288,8 @@ function renderPreloadLinks(ssrManifest, basePath = "") {
1288
1288
  }
1289
1289
  }
1290
1290
  return links;
1291
- }
1292
- function renderPreloadLink(file) {
1291
+ };
1292
+ var renderPreloadLink = (file) => {
1293
1293
  const fileType = file.match(/\.(js|css|woff2?|gif|jpe?g|png|svg)$/)?.[1];
1294
1294
  switch (fileType) {
1295
1295
  case "js":
@@ -1309,8 +1309,8 @@ function renderPreloadLink(file) {
1309
1309
  default:
1310
1310
  return "";
1311
1311
  }
1312
- }
1313
- function getCssLinks(manifest, basePath = "") {
1312
+ };
1313
+ var getCssLinks = (manifest, basePath = "") => {
1314
1314
  const seen = /* @__PURE__ */ new Set();
1315
1315
  const styles = [];
1316
1316
  for (const key in manifest) {
@@ -1325,7 +1325,7 @@ function getCssLinks(manifest, basePath = "") {
1325
1325
  }
1326
1326
  }
1327
1327
  return styles.join("\n");
1328
- }
1328
+ };
1329
1329
  var overrideCSSHMRConsoleError = () => {
1330
1330
  const originalConsoleError = console.error;
1331
1331
  console.error = function(message, ...optionalParams) {
@@ -1337,7 +1337,7 @@ var ensureNonNull = (value, errorMessage) => {
1337
1337
  if (value === void 0 || value === null) throw new Error(errorMessage);
1338
1338
  return value;
1339
1339
  };
1340
- function processTemplate(template) {
1340
+ var processTemplate = (template) => {
1341
1341
  const [headSplit, bodySplit] = template.split(SSRTAG.ssrHead);
1342
1342
  if (typeof bodySplit === "undefined") throw new Error(`Template is missing ${SSRTAG.ssrHead} marker.`);
1343
1343
  const [beforeBody, afterBody] = bodySplit.split(SSRTAG.ssrHtml);
@@ -1348,7 +1348,7 @@ function processTemplate(template) {
1348
1348
  beforeBody: beforeBody.replace(/\s*$/, ""),
1349
1349
  afterBody: afterBody.replace(/^\s*/, "")
1350
1350
  };
1351
- }
1351
+ };
1352
1352
  var rebuildTemplate = (parts, headContent, bodyContent) => {
1353
1353
  return `${parts.beforeHead}${headContent}${parts.afterHead}${parts.beforeBody}${bodyContent}${parts.afterBody}`;
1354
1354
  };
@@ -1356,10 +1356,25 @@ var addNonceToInlineScripts = (html, nonce) => {
1356
1356
  if (!nonce) return html;
1357
1357
  return html.replace(/<script(?![^>]*\bnonce=)([^>]*)>/g, `<script nonce="${nonce}"$1>`);
1358
1358
  };
1359
- function extractHeadInner(html) {
1359
+ var stripDevClientAndStyles = (template) => {
1360
+ return template.replace(/<script type="module" src="\/@vite\/client"><\/script>/g, "").replace(/<style type="text\/css">[\s\S]*?<\/style>/g, "");
1361
+ };
1362
+ var applyViteTransform = async (template, url, viteDevServer) => {
1363
+ return viteDevServer.transformIndexHtml(url, template);
1364
+ };
1365
+ var injectBootstrapModule = (template, bootstrapModule, nonce) => {
1366
+ if (!bootstrapModule) return template;
1367
+ const nonceAttr = nonce ? ` nonce="${nonce}"` : "";
1368
+ return template.replace("</body>", `<script${nonceAttr} type="module" src="${bootstrapModule}" defer></script></body>`);
1369
+ };
1370
+ var injectCssLink = (template, cssLink) => {
1371
+ if (!cssLink) return template;
1372
+ return template.replace("</head>", `${cssLink}</head>`);
1373
+ };
1374
+ var extractHeadInner = (html) => {
1360
1375
  const m = html.match(/<head[^>]*>([\s\S]*?)<\/head>/i);
1361
1376
  return (m?.[1] ?? "").trim();
1362
- }
1377
+ };
1363
1378
 
1364
1379
  // src/utils/AssetManager.ts
1365
1380
  var createMaps = () => ({
@@ -1867,18 +1882,14 @@ var handleNotFound = async (req, reply, processedConfigs, maps, opts = {}) => {
1867
1882
  const bootstrapModule = maps.bootstrapModules.get(clientRoot);
1868
1883
  let processedTemplate = template.replace(SSRTAG.ssrHead, "").replace(SSRTAG.ssrHtml, "");
1869
1884
  if (isDevelopment && viteDevServer) {
1870
- processedTemplate = processedTemplate.replace(/<script type="module" src="\/@vite\/client"><\/script>/g, "");
1871
- processedTemplate = processedTemplate.replace(/<style type="text\/css">[\s\S]*?<\/style>/g, "");
1885
+ processedTemplate = stripDevClientAndStyles(processedTemplate);
1872
1886
  const url = req.url ? new URL(req.url, `http://${req.headers.host}`).pathname : "/";
1873
- processedTemplate = await viteDevServer.transformIndexHtml(url, processedTemplate);
1874
- if (cspNonce) processedTemplate = processedTemplate.replace(/<script(?![^>]*\bnonce=)([^>]*)>/g, `<script nonce="${cspNonce}"$1>`);
1887
+ processedTemplate = await applyViteTransform(processedTemplate, url, viteDevServer);
1888
+ if (cspNonce) processedTemplate = addNonceToInlineScripts(processedTemplate, cspNonce);
1875
1889
  } else if (!isDevelopment && cssLink) {
1876
- processedTemplate = processedTemplate.replace("</head>", `${cssLink}</head>`);
1877
- }
1878
- if (bootstrapModule) {
1879
- const nonceAttr = cspNonce ? ` nonce="${cspNonce}"` : "";
1880
- processedTemplate = processedTemplate.replace("</body>", `<script${nonceAttr} type="module" src="${bootstrapModule}" defer></script></body>`);
1890
+ processedTemplate = injectCssLink(processedTemplate, cssLink);
1881
1891
  }
1892
+ processedTemplate = injectBootstrapModule(processedTemplate, bootstrapModule, cspNonce);
1882
1893
  logger.debug?.("ssr", { status: 200 }, "Sending not-found fallback HTML");
1883
1894
  const result = reply.status(200).type("text/html").send(processedTemplate);
1884
1895
  return result;
@@ -1939,8 +1950,7 @@ var handleRender = async (req, reply, routeMatchers, processedConfigs, serviceRe
1939
1950
  let renderModule;
1940
1951
  if (isDevelopment && viteDevServer) {
1941
1952
  try {
1942
- template = template.replace(/<script type="module" src="\/@vite\/client"><\/script>/g, "");
1943
- template = template.replace(/<style type="text\/css">[\s\S]*?<\/style>/g, "");
1953
+ template = stripDevClientAndStyles(template);
1944
1954
  const entryServerFile = resolveEntryFile(clientRoot, entryServer);
1945
1955
  const entryServerPath = path5.join(clientRoot, entryServerFile);
1946
1956
  const executedModule = await viteDevServer.ssrLoadModule(entryServerPath);
@@ -1953,9 +1963,9 @@ var handleRender = async (req, reply, routeMatchers, processedConfigs, serviceRe
1953
1963
  const stub = "<!doctype html><html><head></head><body></body></html>";
1954
1964
  const transformed = await viteDevServer.transformIndexHtml(url, stub);
1955
1965
  devHead = extractHeadInner(transformed);
1956
- if (cspNonce) devHead = devHead.replace(/<script(?![^>]*\bnonce=)([^>]*)>/g, `<script nonce="${cspNonce}"$1>`);
1966
+ if (cspNonce) devHead = addNonceToInlineScripts(devHead, cspNonce);
1957
1967
  } else {
1958
- template = await viteDevServer.transformIndexHtml(url, template);
1968
+ template = await applyViteTransform(template, url, viteDevServer);
1959
1969
  if (cspNonce) template = addNonceToInlineScripts(template, cspNonce);
1960
1970
  }
1961
1971
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taujs/server",
3
- "version": "0.5.6",
3
+ "version": "0.5.7",
4
4
  "description": "τjs [ taujs ]",
5
5
  "author": "John Smith | Aoede <taujs@aoede.uk.net> (https://www.aoede.uk.net)",
6
6
  "license": "MIT",