Skip to content

Commit 17aa718

Browse files
authored
Convert to consistently use JS modules (Stirling-Tools#6854)
# Description of Changes Modernises the codebase and gets rid of warnings where Node complains that it doesn't know what type of JS it's supposed to be reading on `.js` files. We might as well update everything to just use correct JS syntax instead of keeping with some files having Node-specific imports.
1 parent 20204f0 commit 17aa718

27 files changed

Lines changed: 94 additions & 67 deletions

frontend/editor/postcss.config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
module.exports = {
2-
plugins: [require("@tailwindcss/postcss"), require("autoprefixer")],
1+
import tailwindcssPostcss from "@tailwindcss/postcss";
2+
import autoprefixer from "autoprefixer";
3+
4+
export default {
5+
plugins: [tailwindcssPostcss, autoprefixer],
36
};

frontend/editor/scripts/generate-icons.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env node
22

3-
const { icons } = require("@iconify-json/material-symbols");
4-
const fs = require("fs");
5-
const path = require("path");
3+
import { icons } from "@iconify-json/material-symbols";
4+
import fs from "node:fs";
5+
import path from "node:path";
66

77
// Check for verbose flag
88
const isVerbose =
@@ -19,7 +19,7 @@ const debug = (message) => {
1919
// Function to scan codebase for LocalIcon usage
2020
function scanForUsedIcons() {
2121
const usedIcons = new Set();
22-
const srcDir = path.join(__dirname, "..", "src");
22+
const srcDir = path.join(import.meta.dirname, "..", "src");
2323

2424
info("🔍 Scanning codebase for LocalIcon usage...");
2525

@@ -140,7 +140,7 @@ async function main() {
140140

141141
// Check if we need to regenerate (compare with existing)
142142
const outputPath = path.join(
143-
__dirname,
143+
import.meta.dirname,
144144
"..",
145145
"src",
146146
"assets",
@@ -200,7 +200,7 @@ async function main() {
200200
}
201201

202202
// Create output directory
203-
const outputDir = path.join(__dirname, "..", "src", "assets");
203+
const outputDir = path.join(import.meta.dirname, "..", "src", "assets");
204204
if (!fs.existsSync(outputDir)) {
205205
fs.mkdirSync(outputDir, { recursive: true });
206206
}

frontend/editor/scripts/generate-licenses.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
11
#!/usr/bin/env node
22

3-
const { execSync } = require("node:child_process");
4-
const {
5-
existsSync,
6-
mkdirSync,
7-
writeFileSync,
8-
readFileSync,
9-
} = require("node:fs");
10-
const path = require("node:path");
11-
12-
const { argv } = require("node:process");
3+
import { execSync } from "node:child_process";
4+
import { existsSync, mkdirSync, writeFileSync, readFileSync } from "node:fs";
5+
import path from "node:path";
6+
import { argv } from "node:process";
7+
138
const inputIdx = argv.indexOf("--input");
149
const INPUT_FILE = inputIdx > -1 ? argv[inputIdx + 1] : null;
1510
const POSTPROCESS_ONLY = !!INPUT_FILE;
1611

17-
// __dirname is available in CommonJS by default
18-
1912
/**
2013
* Generate 3rd party licenses for frontend dependencies
2114
* This script creates a JSON file similar to the Java backend's 3rdPartyLicenses.json
2215
*/
2316

2417
const OUTPUT_FILE = path.join(
25-
__dirname,
18+
import.meta.dirname,
2619
"..",
2720
"src",
2821
"assets",
2922
"3rdPartyLicenses.json",
3023
);
3124
// package.json lives at the workspace root (frontend/), not editor/. The
3225
// script is at frontend/editor/scripts/, so walk up two levels.
33-
const PACKAGE_JSON = path.join(__dirname, "..", "..", "package.json");
26+
const PACKAGE_JSON = path.join(import.meta.dirname, "..", "..", "package.json");
3427

3528
// Ensure the output directory exists
3629
const outputDir = path.dirname(OUTPUT_FILE);
@@ -192,7 +185,7 @@ try {
192185

193186
// Write license warnings to a separate file for CI/CD
194187
const warningsFile = path.join(
195-
__dirname,
188+
import.meta.dirname,
196189
"..",
197190
"src",
198191
"assets",

frontend/editor/scripts/generate-og-image.mjs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
/* global document, getComputedStyle */ // used inside page.evaluate (browser context)
1717

1818
import fs from "node:fs/promises";
19+
import { readFileSync } from "node:fs";
1920
import path from "node:path";
2021
import { fileURLToPath } from "node:url";
21-
import { createRequire } from "node:module";
2222

23-
const require = createRequire(import.meta.url);
2423
const HERE = path.dirname(fileURLToPath(import.meta.url));
2524
const ROOT = path.resolve(HERE, "..");
2625

@@ -69,11 +68,14 @@ export const THEME = {
6968
};
7069

7170
// ---- icon resolution (material-symbols via iconify) ------------------------
72-
function resolveIcon(icon) {
71+
async function resolveIcon(icon) {
7372
if (!icon) return "";
7473
if (icon.trim().startsWith("<svg")) return icon; // raw svg passed through
75-
const { getIconData, iconToSVG } = require("@iconify/utils");
76-
const set = require("@iconify-json/material-symbols/icons.json");
74+
const { getIconData, iconToSVG } = await import("@iconify/utils");
75+
const { default: set } = await import(
76+
"@iconify-json/material-symbols/icons.json",
77+
{ with: { type: "json" } }
78+
);
7779
const data = getIconData(set, icon);
7880
if (!data) throw new Error(`icon not found in material-symbols: "${icon}"`);
7981
const { attributes, body } = iconToSVG(data);
@@ -148,7 +150,7 @@ const escapeHtml = (s) =>
148150
let _browser = null;
149151
async function getBrowser() {
150152
if (_browser) return _browser;
151-
const puppeteer = require("puppeteer");
153+
const { default: puppeteer } = await import("puppeteer");
152154
_browser = await puppeteer.launch({
153155
headless: "new",
154156
args: ["--no-sandbox"],
@@ -163,7 +165,7 @@ export async function renderOgCard({
163165
outFile,
164166
theme = THEME,
165167
}) {
166-
const iconSvg = resolveIcon(icon);
168+
const iconSvg = await resolveIcon(icon);
167169
const html = await buildHtml({ name, description, iconSvg, theme });
168170
const browser = await getBrowser();
169171
const page = await browser.newPage();
@@ -230,7 +232,7 @@ const kebab = (id) => id.replace(/([A-Z])/g, "-$1").toLowerCase();
230232

231233
// English name/description live next to each tool as the `t(key, fallback)` default.
232234
function readRegistryStrings() {
233-
const src = require("node:fs").readFileSync(
235+
const src = readFileSync(
234236
path.join(ROOT, "src/core/data/useTranslatedToolRegistry.tsx"),
235237
"utf8",
236238
);
@@ -268,7 +270,7 @@ export async function generateMissing(theme = THEME) {
268270
// Each tool's app icon lives as `icon="<material-symbol>"` just before its
269271
// `name: t("home.<id>.title", …)`. Pair each title with the closest preceding icon.
270272
function readRegistryIcons() {
271-
const src = require("node:fs").readFileSync(
273+
const src = readFileSync(
272274
path.join(ROOT, "src/core/data/useTranslatedToolRegistry.tsx"),
273275
"utf8",
274276
);
@@ -288,25 +290,26 @@ function readRegistryIcons() {
288290
return byId;
289291
}
290292

291-
function iconExists(name) {
293+
async function iconExists(name) {
292294
if (!name) return false;
293295
try {
294-
const { getIconData } = require("@iconify/utils");
295-
return !!getIconData(
296-
require("@iconify-json/material-symbols/icons.json"),
297-
name,
296+
const { getIconData } = await import("@iconify/utils");
297+
const { default: set } = await import(
298+
"@iconify-json/material-symbols/icons.json",
299+
{ with: { type: "json" } }
298300
);
301+
return !!getIconData(set, name);
299302
} catch {
300303
return false;
301304
}
302305
}
303306

304307
// First candidate that resolves; also tries dropping a "-rounded" suffix.
305-
function firstResolvableIcon(candidates) {
308+
async function firstResolvableIcon(candidates) {
306309
for (const c of candidates) {
307-
if (iconExists(c)) return c;
310+
if (await iconExists(c)) return c;
308311
const alt = c && c.replace(/-rounded$/, "");
309-
if (alt && alt !== c && iconExists(alt)) return alt;
312+
if (alt && alt !== c && (await iconExists(alt))) return alt;
310313
}
311314
return "description-outline";
312315
}
@@ -324,14 +327,14 @@ export async function generateAll(theme = THEME) {
324327
const { titles, descs } = readRegistryStrings();
325328
const regIcons = readRegistryIcons();
326329
const ogMap = JSON.parse(
327-
require("node:fs").readFileSync(
328-
path.join(ROOT, "src/core/data/ogImageMap.json"),
329-
"utf8",
330-
),
330+
readFileSync(path.join(ROOT, "src/core/data/ogImageMap.json"), "utf8"),
331331
);
332332
const results = [];
333333
for (const [id, basename] of Object.entries(ogMap)) {
334-
const icon = firstResolvableIcon([regIcons[id], MISSING_TOOL_ICONS[id]]);
334+
const icon = await firstResolvableIcon([
335+
regIcons[id],
336+
MISSING_TOOL_ICONS[id],
337+
]);
335338
await renderOgCard({
336339
name: titles[id] || humanizeId(id),
337340
description: descs[id] || "",

frontend/editor/src/core/tests/live/encrypted-unlock-then-tool.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "@app/tests/helpers/ui-helpers";
88
import path from "path";
99

10-
const FIXTURES_DIR = path.join(__dirname, "../test-fixtures");
10+
const FIXTURES_DIR = path.join(import.meta.dirname, "../test-fixtures");
1111
const ENCRYPTED_PDF = path.join(FIXTURES_DIR, "encrypted.pdf");
1212
const SAMPLE_PDF = path.join(FIXTURES_DIR, "sample.pdf");
1313

frontend/editor/src/core/tests/stubbed/add-page-numbers-tool.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { test, expect } from "@app/tests/helpers/stub-test-base";
22
import { uploadFiles } from "@app/tests/helpers/ui-helpers";
33
import path from "path";
44

5-
const SAMPLE_PDF = path.join(__dirname, "../test-fixtures/sample.pdf");
5+
const SAMPLE_PDF = path.join(
6+
import.meta.dirname,
7+
"../test-fixtures/sample.pdf",
8+
);
69

710
/**
811
* Add Page Numbers walks the user through a multi-step config: position

frontend/editor/src/core/tests/stubbed/add-stamp-tool.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { test, expect } from "@app/tests/helpers/stub-test-base";
22
import { uploadFiles } from "@app/tests/helpers/ui-helpers";
33
import path from "path";
44

5-
const SAMPLE_PDF = path.join(__dirname, "../test-fixtures/sample.pdf");
5+
const SAMPLE_PDF = path.join(
6+
import.meta.dirname,
7+
"../test-fixtures/sample.pdf",
8+
);
69

710
/**
811
* AddStamp loads, accepts a PDF upload, and remains interactive.

frontend/editor/src/core/tests/stubbed/cert-sign-wizard.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { uploadFiles } from "@app/tests/helpers/ui-helpers";
33
import type { Page, Route } from "@playwright/test";
44
import path from "path";
55

6-
const FIXTURES_DIR = path.join(__dirname, "../test-fixtures");
6+
const FIXTURES_DIR = path.join(import.meta.dirname, "../test-fixtures");
77
const SAMPLE_PDF = path.join(FIXTURES_DIR, "sample.pdf");
88

99
// app-config the desktop bundle would return: hardware signing is offered only there.

frontend/editor/src/core/tests/stubbed/certificate-validation.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { suppressNativeFilePicker } from "@app/tests/helpers/ui-helpers";
55
// ---------------------------------------------------------------------------
66
// Test fixtures — pre-generated keystores in test-fixtures/certs/
77
// ---------------------------------------------------------------------------
8-
const CERTS_DIR = path.join(__dirname, "../test-fixtures/certs");
8+
const CERTS_DIR = path.join(import.meta.dirname, "../test-fixtures/certs");
99
const VALID_P12 = path.join(CERTS_DIR, "valid-test.p12");
1010
const EXPIRED_P12 = path.join(CERTS_DIR, "expired-test.p12");
1111
const NOT_YET_VALID_P12 = path.join(CERTS_DIR, "not-yet-valid-test.p12");

frontend/editor/src/core/tests/stubbed/comments-sidebar-order.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { test, expect } from "@app/tests/helpers/stub-test-base";
22
import path from "path";
33

44
const ANNOTATED_PDF = path.join(
5-
__dirname,
5+
import.meta.dirname,
66
"../test-fixtures/annotations_out_of_order.pdf",
77
);
88

0 commit comments

Comments
 (0)