From 39bba6bef11a6e92134c3287454bd67f570dfdcd Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Wed, 2 Oct 2024 00:55:24 +0100 Subject: [PATCH] website: update all 11ty file to .mjs / ESM modules --- ...nce.11tydata.js => Reference.11tydata.mjs} | 16 +++++---- .../{contributors.js => contributors.mjs} | 13 +++++--- .docs/eleventy.config.mjs | 9 ++--- .docs/eslint.config.mjs | 9 +++++ .docs/lib/{Ansi.js => Ansi.mjs} | 2 +- .docs/lib/{FileFetcher.js => FileFetcher.mjs} | 22 ++++++++----- .docs/lib/{HTMLPicture.js => HTMLPicture.mjs} | 31 ++++++++--------- .../{moondoc_runner.js => moondoc_runner.mjs} | 13 +++++--- .../{parse_sections.js => parse_sections.mjs} | 19 ++++++----- .docs/package-lock.json | 33 ------------------- .docs/package.json | 1 - 11 files changed, 76 insertions(+), 92 deletions(-) rename .docs/{Reference.11tydata.js => Reference.11tydata.mjs} (70%) rename .docs/_data/{contributors.js => contributors.mjs} (62%) create mode 100644 .docs/eslint.config.mjs rename .docs/lib/{Ansi.js => Ansi.mjs} (99%) rename .docs/lib/{FileFetcher.js => FileFetcher.mjs} (69%) rename .docs/lib/{HTMLPicture.js => HTMLPicture.mjs} (89%) rename .docs/lib/{moondoc_runner.js => moondoc_runner.mjs} (51%) rename .docs/lib/{parse_sections.js => parse_sections.mjs} (84%) diff --git a/.docs/Reference.11tydata.js b/.docs/Reference.11tydata.mjs similarity index 70% rename from .docs/Reference.11tydata.js rename to .docs/Reference.11tydata.mjs index 268f5ab..aa68520 100644 --- a/.docs/Reference.11tydata.js +++ b/.docs/Reference.11tydata.mjs @@ -1,13 +1,15 @@ "use strict"; +import fs from "fs"; +import path from "path"; -const fs = require("fs"); -const path = require("path"); +import columnify from "columnify"; +import htmlentities from "html-entities"; -const columnify = require("columnify"); -const htmlentities = require("html-entities"); +import a from "./lib/Ansi.mjs"; +import parse_sections from "./lib/parse_sections.mjs"; -const a = require("./lib/Ansi.js"); -const parse_sections = require("./lib/parse_sections.js"); +// HACK: Make sure __dirname is defined when using es6 modules. I forget where I found this - a PR with a source URL would be great! +const __dirname = import.meta.url.slice(7, import.meta.url.lastIndexOf("/")); let { sections, categories } = parse_sections(fs.readFileSync( path.resolve( @@ -36,7 +38,7 @@ console.log(columnify(Array.from(categories).map(el => { return { colour: el[1] } }))); -module.exports = { +export default { layout: "theme.njk", title: "Reference", tags: "navigable", diff --git a/.docs/_data/contributors.js b/.docs/_data/contributors.mjs similarity index 62% rename from .docs/_data/contributors.js rename to .docs/_data/contributors.mjs index d9f8e6d..48222b4 100644 --- a/.docs/_data/contributors.js +++ b/.docs/_data/contributors.mjs @@ -1,6 +1,11 @@ -const fs = require("fs"); -const path = require("path"); -const htmlentities = require("html-entities"); +"use strict"; + +import fs from 'fs'; +import path from 'path'; +import htmlentities from 'html-entities'; + +// HACK: Make sure __dirname is defined when using es6 modules. I forget where I found this - a PR with a source URL would be great! +const __dirname = import.meta.url.slice(7, import.meta.url.lastIndexOf("/")); function read_contributors() { return fs.readFileSync(path.resolve(__dirname, "../../CONTRIBUTORS.tsv"), "utf-8") @@ -20,4 +25,4 @@ const contributors = read_contributors(); console.log(`CONTRIBUTORS`, contributors); -module.exports = contributors; +export default contributors; diff --git a/.docs/eleventy.config.mjs b/.docs/eleventy.config.mjs index 7ee67e2..7c04b4a 100644 --- a/.docs/eleventy.config.mjs +++ b/.docs/eleventy.config.mjs @@ -10,11 +10,9 @@ import phin from "phin"; import CleanCSS from "clean-css"; import { minify as minify_html } from "html-minifier-terser"; -import UpgradeHelper from "@11ty/eleventy-upgrade-help"; - -import moondoc_runner from "./lib/moondoc_runner.js"; -import HTMLPicture from "./lib/HTMLPicture.js"; -import FileFetcher from "./lib/FileFetcher.js"; +import moondoc_runner from "./lib/moondoc_runner.mjs"; +import HTMLPicture from "./lib/HTMLPicture.mjs"; +import FileFetcher from "./lib/FileFetcher.mjs"; const file_fetcher = new FileFetcher(); // HACK: Make sure __dirname is defined when using es6 modules. I forget where I found this - a PR with a source URL would be great! @@ -129,7 +127,6 @@ export default function config(eleventyConfig) { path.resolve(__dirname, "_site/api/index.html") ); - eleventyConfig.addPlugin(UpgradeHelper); eleventyConfig.addTransform("cssmin", do_minify_css); eleventyConfig.addTransform("htmlmin", do_minify_html); diff --git a/.docs/eslint.config.mjs b/.docs/eslint.config.mjs new file mode 100644 index 0000000..64db309 --- /dev/null +++ b/.docs/eslint.config.mjs @@ -0,0 +1,9 @@ +export default [ + { + files: ["**/*.js", "**/*.cjs", "**/*.mjs"], + rules: { + "prefer-const": "warn", + "no-constant-binary-expression": "error" + } + } +]; diff --git a/.docs/lib/Ansi.js b/.docs/lib/Ansi.mjs similarity index 99% rename from .docs/lib/Ansi.js rename to .docs/lib/Ansi.mjs index 4bb7493..347d73d 100644 --- a/.docs/lib/Ansi.js +++ b/.docs/lib/Ansi.mjs @@ -85,4 +85,4 @@ class Ansi { } } -module.exports = new Ansi(); +export default new Ansi(); diff --git a/.docs/lib/FileFetcher.js b/.docs/lib/FileFetcher.mjs similarity index 69% rename from .docs/lib/FileFetcher.js rename to .docs/lib/FileFetcher.mjs index 7ed82fc..e90f900 100644 --- a/.docs/lib/FileFetcher.js +++ b/.docs/lib/FileFetcher.mjs @@ -1,12 +1,16 @@ "use strict"; -const path = require("path"); -const fs = require("fs"); -const os = require("os"); +import fs from "fs"; +import path from "path"; +import os from "os"; -const phin = require("phin"); +import phin from "phin"; + +import a from "./Ansi.mjs"; + +// HACK: Make sure __dirname is defined when using es6 modules. I forget where I found this - a PR with a source URL would be great! +const __dirname = import.meta.url.slice(7, import.meta.url.lastIndexOf("/")); -const a = require("./Ansi.js"); var pretty_ms = null; class FileFetcher { @@ -19,7 +23,7 @@ class FileFetcher { } fetch_file(url) { - let target_client = path.join(`/img`, path.basename(url)); + const target_client = path.join(`/img`, path.basename(url)); if(this.#cache.includes(url)) return target_client; @@ -37,11 +41,11 @@ class FileFetcher { if(this.#pkg_obj === null) { this.#pkg_obj = JSON.parse(await fs.promises.readFile( - path.join(path.dirname(__dirname), "package.json"), "utf8" + path.resolve(path.dirname(__dirname), "package.json"), "utf8" )); } - let target_download = path.join(`_site/img`, path.basename(url)); + const target_download = path.join(`_site/img`, path.basename(url)); const response = await phin({ url, @@ -64,4 +68,4 @@ class FileFetcher { } -module.exports = FileFetcher; \ No newline at end of file +export default FileFetcher; \ No newline at end of file diff --git a/.docs/lib/HTMLPicture.js b/.docs/lib/HTMLPicture.mjs similarity index 89% rename from .docs/lib/HTMLPicture.js rename to .docs/lib/HTMLPicture.mjs index 5401fa8..4590359 100644 --- a/.docs/lib/HTMLPicture.js +++ b/.docs/lib/HTMLPicture.mjs @@ -1,14 +1,18 @@ "use strict"; -const os = require(`os`); -const fs = require("fs"); -const path = require("path"); +import os from 'os'; +import fs from 'fs'; +import path from 'path'; -const debug = require("debug")("image"); -const imagickal = require("imagickal"); -const htmlentities = require("html-entities"); +import imagickal from 'imagickal'; +import htmlentities from "html-entities"; +import PQueue from "p-queue"; +import pMemoize from "p-memoize"; +import pretty_ms from "pretty-ms"; +import Debug from 'debug'; +const debug = Debug("image"); -const a = require("./Ansi.js"); +import a from './Ansi.mjs'; function calculate_size(width, height, size_spec) { if(size_spec.indexOf("%") > -1) { @@ -34,7 +38,6 @@ var queue = null; async function make_queue() { // 1: Setup task queue - const PQueue = (await import("p-queue")).default; let concurrency = os.cpus().length; if(process.env["MAX_CONCURRENT"]) concurrency = parseInt(process.env["MAX_CONCURRENT"], 10); @@ -142,16 +145,8 @@ async function picture(source_image, alt, target_dir, urlpath, formats = "__AUTO return result; } -var picture_memoize = null; -var pretty_ms; +var picture_memoize = pMemoize(picture); -async function setup_memoize() { - const pMemoize = (await import("p-memoize")).default; - picture_memoize = pMemoize(picture); -} - -module.exports = async function(...args) { - if(picture_memoize === null) await setup_memoize(); - pretty_ms = (await import("pretty-ms")).default; +export default async function(...args) { return await picture_memoize(...args); }; diff --git a/.docs/lib/moondoc_runner.js b/.docs/lib/moondoc_runner.mjs similarity index 51% rename from .docs/lib/moondoc_runner.js rename to .docs/lib/moondoc_runner.mjs index 821b6ea..49d574c 100644 --- a/.docs/lib/moondoc_runner.js +++ b/.docs/lib/moondoc_runner.mjs @@ -1,14 +1,17 @@ "use strict"; -const promisify = require("util").promisify; -const fs = require("fs"); -const path = require("path"); -const child_process = require("child_process"); +import { promisify } from "util"; +import fs from "fs"; +import path from "path"; +import child_process from "child_process"; + +// HACK: Make sure __dirname is defined when using es6 modules. I forget where I found this - a PR with a source URL would be great! +const __dirname = import.meta.url.slice(7, import.meta.url.lastIndexOf("/")); const filepath_moondoc = path.resolve(__dirname, `../node_modules/.bin/moondoc`); const dirpath_root = path.resolve(__dirname, `../..`); -module.exports = function moondoc_runner(filepath_output) { +export default function moondoc_runner(filepath_output) { const dirpath = path.dirname(filepath_output); if(!fs.existsSync(dirpath)) { fs.mkdirSync(dirpath, { recursive: true }); diff --git a/.docs/lib/parse_sections.js b/.docs/lib/parse_sections.mjs similarity index 84% rename from .docs/lib/parse_sections.js rename to .docs/lib/parse_sections.mjs index 6fddd2e..018c65f 100644 --- a/.docs/lib/parse_sections.js +++ b/.docs/lib/parse_sections.mjs @@ -1,14 +1,18 @@ "use strict"; -const crypto = require("crypto"); +import crypto from "crypto"; -const htmlentities = require("html-entities"); -const markdown = require("markdown-it")({ +import htmlentities from "html-entities"; +import MarkdownIt from "markdown-it"; +import chroma from "chroma-js"; + +import markdown_prism from "markdown-it-prism"; +import markdown_alerts from "markdown-it-github-alerts"; + +const markdown = new MarkdownIt({ xhtmlOut: true }); -const chroma = require("chroma-js"); -const markdown_prism = require("markdown-it-prism"); markdown.use(markdown_prism, { init: (Prism) => { Prism.languages.weacmd = { @@ -21,8 +25,7 @@ markdown.use(markdown_prism, { } }); -const alerts = require("markdown-it-github-alerts"); -markdown.use(alerts); +markdown.use(markdown_alerts); function extract_title(line) { return line.match(/#+\s+(.+)\s*/)[1].replace(/^`*|`*$/g, "") @@ -47,7 +50,7 @@ function make_section(acc, cat_current, cats) { }; } -module.exports = function parse_sections(source) { +export default function parse_sections(source) { const cats = new Map(); source.match(/^##\s+.*$/gm) .map(extract_title) diff --git a/.docs/package-lock.json b/.docs/package-lock.json index 88b666d..0489c0f 100644 --- a/.docs/package-lock.json +++ b/.docs/package-lock.json @@ -10,7 +10,6 @@ "license": "MPL-2.0", "dependencies": { "@11ty/eleventy": "^3.0.0", - "@11ty/eleventy-upgrade-help": "^3.0.1", "chroma-js": "^3.1.1", "clean-css": "^5.3.2", "columnify": "^1.6.0", @@ -163,26 +162,6 @@ "url": "https://opencollective.com/11ty" } }, - "node_modules/@11ty/eleventy-upgrade-help": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/@11ty/eleventy-upgrade-help/-/eleventy-upgrade-help-3.0.1.tgz", - "integrity": "sha512-69CttBBfplByurn5vW7eRiLPywZZZHCH6B68Ib3t3348PaHHG9RysVPRqPQzMrGIEEbXg+0+ICm+R8sAkgjfPQ==", - "license": "MIT", - "dependencies": { - "fast-glob": "^3.3.2", - "kleur": "^4.1.5", - "minimist": "^1.2.8", - "posthtml-match-helper": "^2.0.2", - "semver": "^7.6.3" - }, - "engines": { - "node": ">=18" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/11ty" - } - }, "node_modules/@11ty/eleventy-utils": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/@11ty/eleventy-utils/-/eleventy-utils-1.0.3.tgz", @@ -3351,18 +3330,6 @@ "posthtml-match-helper": "^2.0.2" } }, - "@11ty/eleventy-upgrade-help": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/@11ty/eleventy-upgrade-help/-/eleventy-upgrade-help-3.0.1.tgz", - "integrity": "sha512-69CttBBfplByurn5vW7eRiLPywZZZHCH6B68Ib3t3348PaHHG9RysVPRqPQzMrGIEEbXg+0+ICm+R8sAkgjfPQ==", - "requires": { - "fast-glob": "^3.3.2", - "kleur": "^4.1.5", - "minimist": "^1.2.8", - "posthtml-match-helper": "^2.0.2", - "semver": "^7.6.3" - } - }, "@11ty/eleventy-utils": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/@11ty/eleventy-utils/-/eleventy-utils-1.0.3.tgz", diff --git a/.docs/package.json b/.docs/package.json index ef4c2c4..ddf919c 100644 --- a/.docs/package.json +++ b/.docs/package.json @@ -21,7 +21,6 @@ "homepage": "https://github.com/sbrl/Minetest-WorldEditAdditions#readme", "dependencies": { "@11ty/eleventy": "^3.0.0", - "@11ty/eleventy-upgrade-help": "^3.0.1", "chroma-js": "^3.1.1", "clean-css": "^5.3.2", "columnify": "^1.6.0",