dh-remixer-sdk 0.0.20 → 0.0.21-52e8fa2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dh-remixer-sdk",
3
- "version": "0.0.20",
3
+ "version": "0.0.21-52e8fa2",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "templates",
@@ -10,5 +10,13 @@
10
10
  "bin": {
11
11
  "ssg-helmet": "./scripts/ssg-helmet/index.mjs",
12
12
  "sdk-update": "./scripts/sdk-update/index.mjs"
13
+ },
14
+ "dependencies": {
15
+ "chrome-launcher": "^1.2.1",
16
+ "chrome-remote-interface": "^0.34.0",
17
+ "estree-walker": "^3.0.3",
18
+ "acorn": "^8.14.0",
19
+ "acorn-jsx": "^5.3.2",
20
+ "sucrase": "^3.35.0"
13
21
  }
14
22
  }
@@ -49,9 +49,18 @@ async function main() {
49
49
  // Extract new files
50
50
  await Promise.all([
51
51
  ...Object.entries(extractionList).map(([fileName, target]) => {
52
- return fileName.endsWith(".json")
53
- ? extractJsonFile(fileName, target, templateType)
54
- : extractFile(fileName, target, templateType);
52
+ if (fileName === "package.json") {
53
+ // LEGACY
54
+ return Promise.all([
55
+ extractJsonFile(fileName, "package.json", templateType),
56
+ extractJsonFile(fileName, "new.package.json", templateType),
57
+ ]);
58
+ }
59
+
60
+ if (fileName.endsWith(".json")) {
61
+ return extractJsonFile(fileName, target, templateType);
62
+ }
63
+ return extractFile(fileName, target, templateType);
55
64
  }),
56
65
  ]);
57
66
 
@@ -11,7 +11,7 @@
11
11
  * - Ensures JSON-LD contains real characters (e.g. "&" not "&")
12
12
  */
13
13
 
14
- const DOMAIN = "example.com";
14
+ import { TARGET_DOMAIN } from "./vars.mjs";
15
15
 
16
16
  export function buildRedirectHelmetHtml(
17
17
  prerenderedHtml,
@@ -88,7 +88,7 @@ export function buildRedirectHelmetHtml(
88
88
  return p;
89
89
  };
90
90
 
91
- const canonicalFromDomainRoute = () => normalizeDomain(DOMAIN) + normalizeRoute(route);
91
+ const canonicalFromDomainRoute = () => normalizeDomain(TARGET_DOMAIN) + normalizeRoute(route);
92
92
 
93
93
  // --- extract head ---------------------------------------------------------
94
94
  const lang = "en";
@@ -2,12 +2,10 @@ import fs from "node:fs/promises";
2
2
  import path from "path";
3
3
  import { injectUnpackRedirectScript } from "./inject-unpack-redirect.mjs";
4
4
  import { buildRedirectHelmetHtml } from "./build-helmet-html.mjs";
5
-
6
- const ROOT = "/";
7
- const OUT = path.resolve("./out");
5
+ import { ROOT, WRITE_TARGET } from "./vars.mjs";
8
6
 
9
7
  async function writeFile(route, content) {
10
- const productOut = path.join(OUT, route, "index.html");
8
+ const productOut = path.join(WRITE_TARGET, route, "index.html");
11
9
  await fs.mkdir(path.dirname(productOut), { recursive: true });
12
10
  await fs.writeFile(productOut, content, "utf8");
13
11
  }
@@ -11,12 +11,13 @@ import {
11
11
  } from "./populate-sitemap.mjs";
12
12
  import { buildSitemapXml } from "./build-sitemap-xml.mjs";
13
13
  import { writeHelmetFile } from "./fs-utils.mjs";
14
-
15
- const OUT = path.resolve("./out");
16
- const IS_INDEXABLE = true;
17
- const IS_NOT_INDEXABLE = false;
18
- const ROOT = "/";
19
- const TARGET_DOMAIN = process.env.NEXT_PUBLIC_DOMAIN || "https://example.com";
14
+ import {
15
+ ROOT,
16
+ IS_INDEXABLE,
17
+ IS_NOT_INDEXABLE,
18
+ TARGET_DOMAIN,
19
+ WRITE_TARGET,
20
+ } from "./vars.mjs";
20
21
 
21
22
  /**
22
23
  * Handles:
@@ -61,7 +62,11 @@ async function main() {
61
62
  );
62
63
 
63
64
  const sitemapXml = buildSitemapXml(TARGET_DOMAIN, sitemapRoutes);
64
- await fs.writeFile(path.join(OUT, "sitemap.xml"), sitemapXml, "utf8");
65
+ await fs.writeFile(
66
+ path.join(WRITE_TARGET, "sitemap.xml"),
67
+ sitemapXml,
68
+ "utf8",
69
+ );
65
70
  console.log("[created] sitemap.xml");
66
71
 
67
72
  await prerenderer.close();
@@ -1,7 +1,6 @@
1
1
  import fs from "node:fs/promises";
2
2
  import path from "node:path";
3
-
4
- const UNPACK_SCRIPT_MARKER = "redirect-helmet-unpack-p-v1";
3
+ import { UNPACK_SCRIPT_MARKER } from "./vars.mjs";
5
4
 
6
5
  /**
7
6
  * Reads out/index.html and injects an "unpack ?p=...&q=..." bootstrap script
@@ -94,17 +94,22 @@ export class Prerenderer {
94
94
 
95
95
  await this.#startServer();
96
96
 
97
- // Launch system Chromium headless with remote debugging enabled
97
+ const defaultFlags = [
98
+ "--headless=new",
99
+ "--no-sandbox",
100
+ "--disable-gpu",
101
+ "--disable-dev-shm-usage",
102
+ "--disable-background-networking",
103
+ "--disable-extensions",
104
+ ];
105
+
106
+ const chromeFlags = process.env.CHROME_FLAGS
107
+ ? ["--headless=new", ...process.env.CHROME_FLAGS.split(",").map(f => f.trim())]
108
+ : defaultFlags;
109
+
98
110
  this.#chrome = await chromeLauncher.launch({
99
- chromePath: process.env.CHROME_BIN || undefined, // optional override
100
- chromeFlags: [
101
- "--headless=new",
102
- "--no-sandbox",
103
- "--disable-gpu",
104
- "--disable-dev-shm-usage",
105
- "--disable-background-networking",
106
- "--disable-extensions",
107
- ],
111
+ chromePath: process.env.CHROME_BIN || undefined,
112
+ chromeFlags,
108
113
  });
109
114
 
110
115
  this.#cdpPort = this.#chrome.port;
@@ -0,0 +1,8 @@
1
+ export const OUT = path.resolve("./out");
2
+ export const WRITE_TARGET = path.resolve("./");
3
+ export const IS_INDEXABLE = true;
4
+ export const IS_NOT_INDEXABLE = false;
5
+ export const ROOT = "/";
6
+ export const TARGET_DOMAIN =
7
+ process.env.NEXT_PUBLIC_DOMAIN || "https://example.com";
8
+ export const UNPACK_SCRIPT_MARKER = "redirect-helmet-unpack-p-v1";
@@ -6,5 +6,6 @@
6
6
  "public/robots.txt",
7
7
  ".gitignore",
8
8
  "tsconfig.json",
9
- "vite.config.ts"
9
+ "vite.config.ts",
10
+ "new.package.json"
10
11
  ]
@@ -1,161 +0,0 @@
1
- import fs from "node:fs/promises";
2
- import path from "node:path";
3
- import fssync from "node:fs";
4
- import { fileURLToPath } from "node:url";
5
-
6
- function isObj(x) {
7
- return x != null && typeof x === "object" && !Array.isArray(x);
8
- }
9
-
10
- function jsonMerge(low, high) {
11
- if (Array.isArray(low) && Array.isArray(high)) {
12
- return [...low, ...high];
13
- }
14
-
15
- if (!isObj(low) || !isObj(high)) {
16
- return high === undefined ? low : high;
17
- }
18
-
19
- return Object.fromEntries(
20
- Object.keys({ ...low, ...high }).map((key) => {
21
- const lv = low[key];
22
- const hv = high[key];
23
-
24
- if (hv === undefined) return [key, lv];
25
- if (Array.isArray(lv) && Array.isArray(hv)) return [key, [...lv, ...hv]];
26
- if (Array.isArray(lv) || Array.isArray(hv)) return [key, hv];
27
- if (isObj(lv) && isObj(hv)) return [key, jsonMerge(lv, hv)];
28
- return [key, hv];
29
- }),
30
- );
31
- }
32
-
33
- async function extractFile(baseFileName, tmplFileName, target) {
34
- await fs.mkdir(path.dirname(target), { recursive: true });
35
-
36
- if (fssync.existsSync(tmplFileName)) {
37
- await fs.copyFile(tmplFileName, target);
38
- return;
39
- }
40
-
41
- if (fssync.existsSync(baseFileName)) {
42
- await fs.copyFile(baseFileName, target);
43
- }
44
- }
45
-
46
- async function extractJsonFile(baseFileName, tmplFileName, target) {
47
- const baseExists = fssync.existsSync(baseFileName);
48
- const templateExists = fssync.existsSync(tmplFileName);
49
-
50
- if (!baseExists && !templateExists) {
51
- return;
52
- }
53
-
54
- let jsonContents = {};
55
-
56
- if (baseExists) {
57
- const baseContents = JSON.parse(await fs.readFile(baseFileName, "utf8"));
58
- jsonContents = jsonMerge(jsonContents, baseContents);
59
- }
60
-
61
- if (templateExists) {
62
- const tmplContents = JSON.parse(await fs.readFile(tmplFileName, "utf8"));
63
- jsonContents = jsonMerge(jsonContents, tmplContents);
64
- }
65
-
66
- const stringContents = JSON.stringify(jsonContents, null, 2) + "\n";
67
- await fs.mkdir(path.dirname(target), { recursive: true });
68
- await fs.writeFile(target, stringContents, "utf8");
69
- }
70
-
71
- async function createNewPackageJsonFile(projectRoot, baseRoot, tmplRoot, vSdk) {
72
- const projPkg = JSON.parse(
73
- await fs.readFile(path.join(projectRoot, "package.json"), "utf8"),
74
- );
75
-
76
- const basePkg = JSON.parse(
77
- await fs.readFile(path.join(baseRoot, "package.json"), "utf8"),
78
- );
79
-
80
- const tmplPkg = JSON.parse(
81
- await fs.readFile(path.join(tmplRoot, "package.json"), "utf8"),
82
- );
83
-
84
- const newPkgObj = jsonMerge(projPkg, jsonMerge(basePkg, tmplPkg));
85
- newPkgObj.dependencies = jsonMerge(newPkgObj.dependencies || {}, {
86
- "dh-remixer-sdk": vSdk,
87
- });
88
-
89
- const newPkgString = JSON.stringify(newPkgObj, null, 2) + "\n";
90
- const newPkgPath = path.join(projectRoot, "new.package.json");
91
- await fs.writeFile(newPkgPath, newPkgString, "utf8");
92
- }
93
-
94
- async function main() {
95
- const projectRoot = path.resolve(process.env.INIT_CWD ?? process.cwd());
96
- const projPkgPath = path.join(projectRoot, "package.json");
97
- const projPkg = JSON.parse(await fs.readFile(projPkgPath, "utf8"));
98
- const templateType = projPkg?.remixerMetadata?.template;
99
- if (!templateType) {
100
- console.warn("Missing remixerMetadata.template in package.json");
101
- return;
102
- }
103
-
104
- const sdkRoot = path.resolve(
105
- path.dirname(fileURLToPath(import.meta.url)),
106
- "..",
107
- );
108
- const sdkPkgPath = path.join(sdkRoot, "package.json");
109
- const sdkVersion = JSON.parse(await fs.readFile(sdkPkgPath, "utf8")).version;
110
- const templateRoot = path.join(sdkRoot, "templates", templateType);
111
- const baseRoot = path.join(sdkRoot, "templates", "base");
112
- const baseCleanupPath = path.join(baseRoot, "cleanup.json");
113
- const baseFilemapPath = path.join(baseRoot, "filemap.json");
114
- const templateCleanupPath = path.join(templateRoot, "cleanup.json");
115
- const templateFilemapPath = path.join(templateRoot, "filemap.json");
116
- let cleanup = JSON.parse(await fs.readFile(baseCleanupPath, "utf8"));
117
- let filemap = JSON.parse(await fs.readFile(baseFilemapPath, "utf8"));
118
-
119
- if (fssync.existsSync(templateCleanupPath)) {
120
- cleanup = jsonMerge(
121
- cleanup,
122
- JSON.parse(await fs.readFile(templateCleanupPath, "utf8")),
123
- );
124
- }
125
-
126
- if (fssync.existsSync(templateFilemapPath)) {
127
- filemap = jsonMerge(
128
- filemap,
129
- JSON.parse(await fs.readFile(templateFilemapPath, "utf8")),
130
- );
131
- }
132
-
133
- // Delete old files
134
- await Promise.all(
135
- cleanup.map((relativePath) =>
136
- fs
137
- .rm(path.join(projectRoot, relativePath), {
138
- recursive: true,
139
- force: true,
140
- })
141
- .catch(() => {}),
142
- ),
143
- );
144
-
145
- // Extract new files
146
- await Promise.all([
147
- ...Object.entries(filemap).map(([fileName, target]) => {
148
- const targetPath = path.join(projectRoot, target);
149
- const baseFileName = path.join(baseRoot, fileName);
150
- const templateFileName = path.join(templateRoot, fileName);
151
-
152
- return fileName.endsWith(".json")
153
- ? extractJsonFile(baseFileName, templateFileName, targetPath)
154
- : extractFile(baseFileName, templateFileName, targetPath);
155
- }),
156
- createNewPackageJsonFile(projectRoot, baseRoot, templateRoot, sdkVersion),
157
- ]);
158
- }
159
-
160
- await main();
161
-