From d681edd0ca56e5d50d1115eb021ea6f14f13284c Mon Sep 17 00:00:00 2001 From: Dominic Kempf Date: Mon, 7 Oct 2024 16:04:20 +0200 Subject: [PATCH] Use JSON in pregen --- package-lock.json | 119 +++++++++++++++++++++ package.json | 1 + public/pregen/.gitkeep | 0 rollup.config.js | 10 +- src/components/logic/DetailControls.svelte | 2 - src/generate.js | 16 +-- src/lib/easydbPregen.js | 5 +- src/lib/stores.js | 10 +- vite.config.ts | 3 +- 9 files changed, 136 insertions(+), 30 deletions(-) create mode 100644 public/pregen/.gitkeep diff --git a/package-lock.json b/package-lock.json index a4e13b8..a56949c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "postcss": "^8.4.47", "postcss-load-config": "^5.0.2", "rollup": "^4.21.2", + "rollup-plugin-copy": "^3.5.0", "rollup-plugin-css-only": "^4.5.2", "rollup-plugin-postcss": "^4.0.2", "rollup-plugin-svelte": "^7.2.2", @@ -3022,12 +3023,37 @@ "integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==", "dev": true }, + "node_modules/@types/fs-extra": { + "version": "8.1.5", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-8.1.5.tgz", + "integrity": "sha512-0dzKcwO+S8s2kuF5Z9oUWatQJj5Uq/iqphEtE3GQJVRRYm/tD1LglU2UnXi2A8jLq5umkGouOXOR9y0n613ZwQ==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/glob": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz", + "integrity": "sha512-ZUxbzKl0IfJILTS6t7ip5fQQM/J3TJYubDm3nMbgubNNYS62eXeUpoLUC8/7fJNiFYHTrGPQn7hspDUzIHX3UA==", + "dev": true, + "dependencies": { + "@types/minimatch": "*", + "@types/node": "*" + } + }, "node_modules/@types/http-cache-semantics": { "version": "4.0.4", "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.4.tgz", "integrity": "sha512-1m0bIFVc7eJWyve9S0RnuRgcQqF/Xd5QsUZAZeQFr1Q3/p9JWoQQEqmVy+DPTNpGXwhgIetAoYF8JSc33q29QA==", "dev": true }, + "node_modules/@types/minimatch": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-5.1.2.tgz", + "integrity": "sha512-K0VQKziLUWkVKiRVrx4a40iPaxTUefQmjtkQofBkYRcoaaL/8rhwDWww9qWbrgicNOgnpIsMxyNIUM4+n6dUIA==", + "dev": true + }, "node_modules/@types/mute-stream": { "version": "0.0.4", "resolved": "https://registry.npmjs.org/@types/mute-stream/-/mute-stream-0.0.4.tgz", @@ -3608,6 +3634,12 @@ "integrity": "sha512-jeC1axXpnb0/2nn/Y1LPuLdgXBLH7aDcHu4KEKfqw3CUhX7ZpfBSlPKyqXE6btIgEzfWtrX3/tyBCaCvXvMkOw==", "dev": true }, + "node_modules/colorette": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/colorette/-/colorette-1.4.0.tgz", + "integrity": "sha512-Y2oEozpomLn7Q3HFP7dpww7AtMJplbM9lGZP6RDfHqmbeRjiwRg4n6VM6j4KLmRke85uWEI7JqF17f3pqdRA0g==", + "dev": true + }, "node_modules/commander": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/commander/-/commander-4.1.1.tgz", @@ -4836,6 +4868,15 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/is-plain-object": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-3.0.1.tgz", + "integrity": "sha512-Xnpx182SBMrr/aBik8y+GuR4U1L9FqMSojwDQwPMmxyC6bvEqly9UBCxhauBF5vNh2gwWJNX6oDV7O+OM4z34g==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/is-reference": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/is-reference/-/is-reference-3.0.2.tgz", @@ -6467,6 +6508,84 @@ "fsevents": "~2.3.2" } }, + "node_modules/rollup-plugin-copy": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/rollup-plugin-copy/-/rollup-plugin-copy-3.5.0.tgz", + "integrity": "sha512-wI8D5dvYovRMx/YYKtUNt3Yxaw4ORC9xo6Gt9t22kveWz1enG9QrhVlagzwrxSC455xD1dHMKhIJkbsQ7d48BA==", + "dev": true, + "dependencies": { + "@types/fs-extra": "^8.0.1", + "colorette": "^1.1.0", + "fs-extra": "^8.1.0", + "globby": "10.0.1", + "is-plain-object": "^3.0.0" + }, + "engines": { + "node": ">=8.3" + } + }, + "node_modules/rollup-plugin-copy/node_modules/brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "dependencies": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "node_modules/rollup-plugin-copy/node_modules/glob": { + "version": "7.2.3", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", + "integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==", + "deprecated": "Glob versions prior to v9 are no longer supported", + "dev": true, + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.1.1", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + }, + "engines": { + "node": "*" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/rollup-plugin-copy/node_modules/globby": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/globby/-/globby-10.0.1.tgz", + "integrity": "sha512-sSs4inE1FB2YQiymcmTv6NWENryABjUNPeWhOvmn4SjtKybglsyPZxFB3U1/+L1bYi0rNZDqCLlHyLYDl1Pq5A==", + "dev": true, + "dependencies": { + "@types/glob": "^7.1.1", + "array-union": "^2.1.0", + "dir-glob": "^3.0.1", + "fast-glob": "^3.0.3", + "glob": "^7.1.3", + "ignore": "^5.1.1", + "merge2": "^1.2.3", + "slash": "^3.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/rollup-plugin-copy/node_modules/minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, + "dependencies": { + "brace-expansion": "^1.1.7" + }, + "engines": { + "node": "*" + } + }, "node_modules/rollup-plugin-css-only": { "version": "4.5.2", "resolved": "https://registry.npmjs.org/rollup-plugin-css-only/-/rollup-plugin-css-only-4.5.2.tgz", diff --git a/package.json b/package.json index d825c7e..f26d271 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "postcss": "^8.4.47", "postcss-load-config": "^5.0.2", "rollup": "^4.21.2", + "rollup-plugin-copy": "^3.5.0", "rollup-plugin-css-only": "^4.5.2", "rollup-plugin-postcss": "^4.0.2", "rollup-plugin-svelte": "^7.2.2", diff --git a/public/pregen/.gitkeep b/public/pregen/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/rollup.config.js b/rollup.config.js index 58f6473..14eed8c 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,7 +1,7 @@ +import copy from 'rollup-plugin-copy'; import svelte from 'rollup-plugin-svelte'; import resolve from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; -import replace from '@rollup/plugin-replace'; import terser from '@rollup/plugin-terser'; import postcss from 'rollup-plugin-postcss'; import { sveltePreprocess } from 'svelte-preprocess'; @@ -15,6 +15,14 @@ export default { inlineDynamicImports: true }, plugins: [ + copy({ + targets: [ + { + src: 'public/pregen/*', + dest: 'bundle/pregen' + } + ] + }), svelte({ emitCss: false, preprocess: sveltePreprocess({ diff --git a/src/components/logic/DetailControls.svelte b/src/components/logic/DetailControls.svelte index d9e8ad1..ee4527f 100644 --- a/src/components/logic/DetailControls.svelte +++ b/src/components/logic/DetailControls.svelte @@ -53,8 +53,6 @@ } $: displayedButtons = calculate($systemidStore); - - console.log(displayedButtons); {#if displayedButtons.length > 0} diff --git a/src/generate.js b/src/generate.js index 67a3463..961f113 100644 --- a/src/generate.js +++ b/src/generate.js @@ -2,13 +2,6 @@ import { writeFile } from 'fs/promises'; import { accessInstance } from './lib/easydbData.js'; import { Command, Flags } from '@oclif/core'; -// This "trick" is super-unfortunate, but Svelte Preprocessing is fully regex-based -// and any attempts to pass stringified data that contains backslashes will result in -// a particularly bad instance of Regex hell. We therefore resort to doing this in -// the serialization and ondoing it in the deserialization as part of the injected code. -function escape(str) { - return str.replaceAll("\\\"", "__ESCAPED_QUOTES__").replaceAll("\\", "__BACKSLASH__"); -} function useMask(name, include_mask, exclude_mask) { if (include_mask) { @@ -56,15 +49,10 @@ export class PrefetchCommand extends Command { data.masks = filterMasks(data.masks, flags.include_mask, flags.exclude_mask); - // Inject the data into the Svelte component as a constant - const content = - `export const pregen_instance = '${instance}';\n` + - `export const pregen_masks = JSON.parse('${escape(JSON.stringify(data.masks))}'.replaceAll("__ESCAPED_QUOTES__", "\\\\\\"").replaceAll("__BACKSLASH__", "\\\\"));\n` + - `export const pregen_l10n = JSON.parse('${escape(JSON.stringify(data.l10n))}'.replaceAll("__ESCAPED_QUOTES__", "\\\\\\"").replaceAll("__BACKSLASH__", "\\\\"));\n` + - `export const pregen_schemas = JSON.parse('${escape(JSON.stringify(data.schemas))}'.replaceAll("__ESCAPED_QUOTES__", "\\\\\\"").replaceAll("__BACKSLASH__", "\\\\"));\n`; + await writeFile('src/lib/easydbPregen.js', `export const pregen_instance = "${instance}";`); try { - await writeFile('src/lib/easydbPregen.js', content); + await writeFile('public/pregen/data.json', JSON.stringify(data)); console.log('Successfully regenerated the EasyDB data.'); } catch (error) { console.error('Error writing the EasyDB data:', error); diff --git a/src/lib/easydbPregen.js b/src/lib/easydbPregen.js index b733d0a..a79986d 100644 --- a/src/lib/easydbPregen.js +++ b/src/lib/easydbPregen.js @@ -1,4 +1 @@ -export const pregen_instance = null; -export const pregen_masks = null; -export const pregen_l10n = null; -export const pregen_schemas = null; +export const pregen_instance = null; \ No newline at end of file diff --git a/src/lib/stores.js b/src/lib/stores.js index 22e6825..15a77b1 100644 --- a/src/lib/stores.js +++ b/src/lib/stores.js @@ -1,6 +1,6 @@ import { derived, get, writable } from "svelte/store"; import { accessInstance } from "./easydbData"; -import { pregen_instance, pregen_l10n, pregen_masks, pregen_schemas } from "./easydbPregen"; +import { pregen_instance } from "./easydbPregen"; // A derived store that resolves a promise function derivedPromise(store) { @@ -13,12 +13,8 @@ function derivedPromise(store) { // Our pregenerated defaults wrapped in a Promise async function pregenDefaults() { - return { - instance: pregen_instance, - masks: pregen_masks, - schemas: pregen_schemas, - l10n: pregen_l10n, - }; + const response = await fetch(`/pregen/data.json`); + return response.json(); } // This manages the global state of the current app language diff --git a/vite.config.ts b/vite.config.ts index 78556fd..401b4d4 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -3,6 +3,5 @@ import { svelte } from '@sveltejs/vite-plugin-svelte' // https://vitejs.dev/config/ export default defineConfig({ - plugins: [svelte()], - base: '/svelte-easydb-detail-view/' + plugins: [svelte()] })