const fs = require('fs'); const path = require("path"); const TOML = require('fast-toml'); const UglifyJS = require('uglify-js'); const jsonminify = require("jsonminify"); const util = require("util"); const { exec } = require("child_process"); const { exit } = require('process'); const execPromise = util.promisify(exec); if (!(fs.existsSync('zola.toml'))) { throw new Error('ERROR: cannot find zola.toml!'); } const tomlString = String(fs.readFileSync('zola.toml')); const data = TOML.parse(tomlString); const js_prestyle = data.extra.js_prestyle; const js_switcher = data.extra.js_switcher; const js_email_encode = data.extra.js_email_encode; const js_copycode = data.extra.js_copycode; const search_library = data.extra.search_library; const index_format = data.search.index_format; const uglyurls = data.extra.uglyurls; const js_bundle = data.extra.js_bundle; const pwa = data.extra.pwa; const pwa_VER = data.extra.pwa_VER; const pwa_NORM_TTL = data.extra.pwa_NORM_TTL; const pwa_LONG_TTL = data.extra.pwa_LONG_TTL; const pwa_TTL_NORM = data.extra.pwa_TTL_NORM; const pwa_TTL_LONG = data.extra.pwa_TTL_LONG; const pwa_TTL_EXEMPT = data.extra.pwa_TTL_EXEMPT; const pwa_cache_all = data.extra.pwa_cache_all; const pwa_BASE_CACHE_FILES = data.extra.pwa_BASE_CACHE_FILES; const pwa_IGNORE_FILES = data.extra.pwa_IGNORE_FILES; // This is used to pass arguments to zola via npm, for example: // npm run abridge -- "--base-url https://abridge.pages.dev" var args = process.argv[2] ? ' ' + process.argv[2] : ''; // check if abridge is used directly or as a theme. bpath = ''; if (fs.existsSync('./themes')) { bpath = 'themes/abridge/'; } // cleanup pagefind files from old builds. _rmRegex(path.join(__dirname, "static/js/"), /^wasm.*pagefind$/); _rmRegex(path.join(__dirname, "static/js/"), /^pagefind.*pf_meta$/); _rmRegex(path.join(__dirname, "static/js/"), /^pagefind-entry.*json$/); _rmRecursive(path.join(__dirname, "static/js/index")); _rmRecursive(path.join(__dirname, "static/js/fragment")); async function execWrapper(cmd) { const { stdout, stderr } = await execPromise(cmd); if (stdout) { console.log(stdout); } if (stderr) { console.log(stderr); } } async function abridge() { await sync(); const { replaceInFileSync } = await import('replace-in-file'); // set index_format for chosen search_library accordingly. if (search_library === 'offline') { replaceInFileSync({ files: 'zola.toml', from: /index_format.*=.*/g, to: "index_format = \"elasticlunr_javascript\"" }); args = args + " -u \"" + __dirname + "\/public\""//set base_url to the path on disk for offline site. } else if (search_library === 'elasticlunrjava') { replaceInFileSync({ files: 'zola.toml', from: /index_format.*=.*/g, to: "index_format = \"elasticlunr_javascript\"" }); } else if (search_library === 'elasticlunr') { replaceInFileSync({ files: 'zola.toml', from: /index_format.*=.*/g, to: "index_format = \"elasticlunr_json\"" }); } else if (search_library === 'pagefind') { replaceInFileSync({ files: 'zola.toml', from: /index_format.*=.*/g, to: "index_format = \"fuse_json\"" }); } else if (search_library === 'tinysearch') { replaceInFileSync({ files: 'zola.toml', from: /index_format.*=.*/g, to: "index_format = \"fuse_json\"" }); } console.log('Zola Build to generate files for minification:'); await execWrapper('zola build' + args); //check that static/js exists, do this after zola build, it will handle creating static if missing. var jsdir = 'static/js'; try { fs.mkdirSync(jsdir); } catch (e) { if (e.code != 'EEXIST') throw e; } base_url = data.base_url; if (base_url.slice(-1) == "/") { base_url = base_url.slice(0, -1); } if (search_library === 'pagefind') { // Generate pagefind index at start, otherwise it happens too late asyncronously. await createPagefindIndex(); // makes program wait for pagefind build execution _rmRegex(path.join(__dirname, "static/js/"), /^pagefind\.js$/);//pagefind temporary intermediate files _rmRegex(path.join(__dirname, "static/js/"), /^pagefind-.*\.js$/);//pagefind temporary intermediate files _rmRegex(path.join(__dirname, "static/js/"), /^pagefind-.*\.css$/);//pagefind temporary intermediate files // This line in pagefind is causing a problem for the PWA: // var e = await (await fetch(this.basePath + "pagefind-entry.json?ts=" + Date.now())).json(); // instead generate an epoch timestamp at build and add it to the filename. var hash = Math.floor(new Date().getTime() / 1000); fs.renameSync(path.join(__dirname, "static/js/pagefind-entry.json"), path.join(__dirname, "static/js/pagefind-entry-" + hash + ".json")); // Pagefind builds the entry URL in several forms across versions, e.g.: // this.basePath+"pagefind-entry.json?ts="+Date.now() // `${this.basePath}pagefind-entry.json?ts=${Date.now()}` // Replace those with a build-time hashed filename (no cache-bust query). // Also strip import.meta (ESM-only) so the classic