From 63e4ecdb9ac705153e54faca9edc98ad0077e5fc Mon Sep 17 00:00:00 2001 From: Jieiku Date: Thu, 8 Aug 2024 19:56:58 -0700 Subject: Handle Pagefind Intermediate Files --- config.toml | 2 +- package_abridge.js | 59 ++++++++++++++++++++++++++++++++++++-------- static/js/pagefind.index.cjs | 2 +- 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/config.toml b/config.toml index bdf5b71..199eda9 100644 --- a/config.toml +++ b/config.toml @@ -236,7 +236,7 @@ webmanifest = "manifest.min.json" # Required for PWAs ############################################################################### pwa = true # true to load the service worker -pwa_VER = '3.12.0' # Service Worker cache version. (increment if you need to force a new cache) +pwa_VER = '3.12.1' # Service Worker cache version. (increment if you need to force a new cache) ### 3600=1hour, 28800=8hours, 86400=1day, 604800=1week, 1209600=2weeks pwa_NORM_TTL = 0 # 86400 is reasonable. html, json, xml, anything else undefined diff --git a/package_abridge.js b/package_abridge.js index dcc079d..5933f62 100644 --- a/package_abridge.js +++ b/package_abridge.js @@ -35,6 +35,12 @@ const pwa_BASE_CACHE_FILES = data.extra.pwa_BASE_CACHE_FILES; // 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/'; +} + async function execWrapper(cmd) { const { stdout, stderr } = await execPromise(cmd); if (stdout) { @@ -73,12 +79,6 @@ async function abridge() { if (e.code != 'EEXIST') throw e; } - // check if abridge is used directly or as a theme. - bpath = ''; - if (fs.existsSync('./themes')) { - bpath = 'themes/abridge/'; - } - base_url = data.base_url; if (base_url.slice(-1) == "/") { base_url = base_url.slice(0, -1); @@ -89,8 +89,20 @@ async function abridge() { // Has to happen at start otherwise, it happens too late asyncronously. const createIndex = require('./static/js/pagefind.index.cjs'); // run the pagefind index.js script await createIndex(); // makes program wait for pagefind build execution + } else { + _rmRegex(path.join(bpath, "static/js/"),/^wasm.*pagefind$/); + _rmRegex(path.join(bpath, "static/js/"),/^pagefind.*pf_meta$/); + _rmRegex(path.join(bpath, "static/js/"),/^pagefind-entry\.json$/); + _rmRecursive(path.join(bpath, "static/js/index")); + _rmRecursive(path.join(bpath, "static/js/fragment")); } + // cleanup + _rmRegex(path.join(bpath, "static/js/"),/^pagefind\.js$/);//pagefind temporary intermediate files + _rmRegex(path.join(bpath, "static/js/"),/^pagefind-.*\.js$/);//pagefind temporary intermediate files + _rmRegex(path.join(bpath, "static/js/"),/^pagefind-.*\.css$/);//pagefind temporary intermediate files + + if (pwa) {// Update pwa settings, file list, and hashes. if (typeof pwa_VER !== 'undefined' && typeof pwa_NORM_TTL !== 'undefined' && typeof pwa_LONG_TTL !== 'undefined' && typeof pwa_TTL_NORM !== 'undefined' && typeof pwa_TTL_LONG !== 'undefined' && typeof pwa_TTL_EXEMPT !== 'undefined') { // update from abridge theme. @@ -196,6 +208,9 @@ async function abridge() { abridge_bundle = bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search_library,index_format,uglyurls,pwa); minify(abridge_bundle,'static/js/abridge.min.js'); + // cleanup + _rmRegex(path.join(bpath, "static/js/"),/^pagefind_search\.js$/);//pagefind intermediate file that is now in bundle. + console.log('Zola Build to generate new integrity hashes for the previously minified files:'); await execWrapper('zola build'+args); } @@ -204,8 +219,6 @@ async function _headersWASM() { // running WASM in the browser requires wasm-unsafe-eval if using Content-Security-Policy: // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_webassembly_execution // This function adds wasm-unsafe-eval to the pagefind and tinysearch demos without adding it to the elasticlunr demo. - // I keep hoping they will add something like: wasm-src 'self' but as far as I know nothing like that exists. - const { replaceInFileSync } = await import('replace-in-file'); if (search_library === 'pagefind') { replaceInFileSync({files: 'static/_headers', from: /script-src 'self'/g, to: "script-src 'wasm-unsafe-eval' 'self'"}); @@ -216,6 +229,26 @@ async function _headersWASM() { } } +function _rmRecursive(targetFiles) { + try { + fs.rmSync(targetFiles, { recursive: true }); + } catch (error) { + if (error.code !== 'ENOENT') {// Ignore if does not exist, that is the desired result. + console.error("An error occurred:", error); + } + } +} + +function _rmRegex(path,regex) { + try { + fs.readdirSync(path).filter(f => regex.test(f)).forEach(f => fs.unlinkSync(path + f)); + } catch (error) { + if (error.code !== 'ENOENT') {// Ignore if does not exist, that is the desired result. + console.error("An error occurred:", error); + } + } +} + function bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search_library,index_format,uglyurls,pwa) { minify_files = []; @@ -244,7 +277,7 @@ function bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search minify_files.push(bpath+'static/js/elasticlunr.min.js'); minify_files.push(bpath+'static/js/search.js'); } else if (search_library === 'pagefind') { - minify_files.push(bpath+'static/js/pagefind-search.js'); + minify_files.push(bpath+'static/js/pagefind_search.js'); } else if (search_library === 'tinysearch') { minify_files.push(bpath+'static/js/tinysearch.js'); } @@ -295,7 +328,13 @@ if (args === ' offline') { searchChange('offline'); } else if (args === ' elasticlunrjava') { searchChange('elasticlunrjava'); -} else if (args === ' elasticlunr') { +} else if (args === ' elasticlunr') {// Zola default search_library + // I should not need to do cleanup here as well... but this is what works for now to keep the repo clean. + _rmRegex(path.join(bpath, "static/js/"),/^wasm.*pagefind$/); + _rmRegex(path.join(bpath, "static/js/"),/^pagefind.*pf_meta$/); + _rmRegex(path.join(bpath, "static/js/"),/^pagefind-entry\.json$/); + _rmRecursive(path.join(bpath, "static/js/index")); + _rmRecursive(path.join(bpath, "static/js/fragment")); searchChange('elasticlunr'); } else if (args === ' pagefind') { searchChange('pagefind'); diff --git a/static/js/pagefind.index.cjs b/static/js/pagefind.index.cjs index 87e2607..c274efe 100644 --- a/static/js/pagefind.index.cjs +++ b/static/js/pagefind.index.cjs @@ -77,7 +77,7 @@ async function createIndex() { // now insert the CJS into the anonymous function within pagefind.search.js const pagefind_searchPath = path.join(__dirname, "pagefind.search.js");//file to insert into - const search_pagefindPath = path.join(__dirname, "pagefind-search.js");//output + const search_pagefindPath = path.join(__dirname, "pagefind_search.js");//output let pagefind_searchContent = fs.readFileSync(pagefind_searchPath, "utf8"); // Now insert into pagefind.search.js at this location: //insertHere pagefind_searchContent = pagefind_searchContent.replace(/\/\/insertHere/g, pagefindContent); -- cgit v1.3.1