livelocale 0.11.0 → 0.12.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.
@@ -713,6 +713,7 @@ var SHARE_MANIFEST_FILE = "manifest.json";
713
713
  var SHARE_FLAG_FILE = "flag.js";
714
714
  var SHARE_LOCALES_DIR = "locales";
715
715
  var SHARE_MANIFEST_FORMAT = 1;
716
+ var MAX_MANIFEST_PAGES = 2e3;
716
717
  var BASE_SHA_RE2 = /^[0-9a-f]{40}([0-9a-f]{24})?$/;
717
718
  function own2(obj, key) {
718
719
  return Object.prototype.hasOwnProperty.call(obj, key) ? obj[key] : void 0;
@@ -2713,6 +2714,18 @@ import { spawn } from "child_process";
2713
2714
  import { existsSync as existsSync5, readFileSync as readFileSync7, statSync as statSync4, promises as fs7 } from "fs";
2714
2715
  import * as path11 from "path";
2715
2716
 
2717
+ // src/core/page-path.ts
2718
+ var NON_ASCII_ESCAPES = /(?:%[89A-Fa-f][0-9A-Fa-f])+/g;
2719
+ function readablePath(path22) {
2720
+ return path22.replace(NON_ASCII_ESCAPES, (run3) => {
2721
+ try {
2722
+ return decodeURIComponent(run3);
2723
+ } catch {
2724
+ return run3;
2725
+ }
2726
+ });
2727
+ }
2728
+
2716
2729
  // src/server/build-info.ts
2717
2730
  import { execFile } from "child_process";
2718
2731
  import { readFileSync as readFileSync5 } from "fs";
@@ -3187,6 +3200,25 @@ async function findHtmlFiles(outDir, shareDir) {
3187
3200
  await walk(outDir);
3188
3201
  return found.sort();
3189
3202
  }
3203
+ function pagePathOf(rel) {
3204
+ const page = rel === "index.html" ? "/" : rel.endsWith("/index.html") ? `/${rel.slice(0, -10)}` : `/${rel}`;
3205
+ const escaped = page.replace(/[%?#\\]/g, (ch) => encodeURIComponent(ch));
3206
+ return readablePath(new URL(escaped, "http://p").pathname);
3207
+ }
3208
+ function isNotFoundPage(rel) {
3209
+ return /^(?:404|_not-found)(?:\.html$|\/)/i.test(rel);
3210
+ }
3211
+ function pagesTruncatedWarning(total) {
3212
+ return `The site has ${total} HTML pages; the manifest lists the first ${MAX_MANIFEST_PAGES} for the Studio's page suggestions.`;
3213
+ }
3214
+ async function findHtmlDocuments(outDir, shareDir) {
3215
+ const documents = [];
3216
+ for (const rel of await findHtmlFiles(outDir, shareDir)) {
3217
+ const html = await fs7.readFile(path11.join(outDir, ...rel.split("/")), "utf-8");
3218
+ if (isHtmlDocument(html)) documents.push(rel);
3219
+ }
3220
+ return documents;
3221
+ }
3190
3222
  async function packOutput(options) {
3191
3223
  const {
3192
3224
  outDir: outOption,
@@ -3221,16 +3253,19 @@ async function packOutput(options) {
3221
3253
  `${toPosix(path11.relative(projectDir, outDir))}/${SHARE_DIR}/ already exists and was not written by LiveLocale. Move it away and run again.`
3222
3254
  );
3223
3255
  }
3256
+ const documents = await findHtmlDocuments(outDir, shareDir);
3257
+ const allPages = documents.filter((rel) => !isNotFoundPage(rel)).map(pagePathOf).sort();
3258
+ const manifest = allPages.length > 0 ? { ...data.manifest, pages: allPages.slice(0, MAX_MANIFEST_PAGES) } : data.manifest;
3224
3259
  await fs7.rm(shareDir, { recursive: true, force: true });
3225
3260
  await fs7.cp(studioDir, shareDir, { recursive: true });
3226
3261
  await fs7.writeFile(
3227
3262
  path11.join(shareDir, SHARE_MANIFEST_FILE),
3228
- JSON.stringify(data.manifest, null, 2) + "\n",
3263
+ JSON.stringify(manifest, null, 2) + "\n",
3229
3264
  "utf-8"
3230
3265
  );
3231
3266
  const localesDir = path11.join(shareDir, SHARE_LOCALES_DIR);
3232
3267
  await fs7.mkdir(localesDir, { recursive: true });
3233
- for (const locale of data.manifest.locales) {
3268
+ for (const locale of manifest.locales) {
3234
3269
  await fs7.writeFile(
3235
3270
  path11.join(localesDir, `${locale}.json`),
3236
3271
  JSON.stringify(data.dictionaries[locale]),
@@ -3239,20 +3274,19 @@ async function packOutput(options) {
3239
3274
  }
3240
3275
  await fs7.writeFile(path11.join(shareDir, SHARE_FLAG_FILE), FLAG_SCRIPT, "utf-8");
3241
3276
  const inject = injectHtml === "auto" ? target.kind !== "next" && !existsSync5(path11.join(outDir, "_next")) : injectHtml;
3242
- const htmlInjected = [];
3243
- for (const rel of inject ? await findHtmlFiles(outDir, shareDir) : []) {
3277
+ const htmlInjected = inject ? documents : [];
3278
+ for (const rel of htmlInjected) {
3244
3279
  const file = path11.join(outDir, ...rel.split("/"));
3245
3280
  const html = await fs7.readFile(file, "utf-8");
3246
- if (!isHtmlDocument(html)) continue;
3247
- htmlInjected.push(rel);
3248
3281
  const injected = injectFlagScript(html, flagSrcFor(base, rel));
3249
3282
  if (injected !== html) await fs7.writeFile(file, injected, "utf-8");
3250
3283
  }
3251
3284
  const warnings = [...data.warnings];
3252
3285
  if (base.kind === "relative") warnings.push(RELATIVE_BASE_WARNING);
3286
+ if (allPages.length > MAX_MANIFEST_PAGES) warnings.push(pagesTruncatedWarning(allPages.length));
3253
3287
  return {
3254
3288
  shareDir,
3255
- manifest: data.manifest,
3289
+ manifest,
3256
3290
  htmlInjected,
3257
3291
  shareUrlPath: shareUrlPathFor(base),
3258
3292
  warnings