Skip to content

Commit

Permalink
Use JSON in pregen
Browse files Browse the repository at this point in the history
dokempf committed Oct 7, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 932edd3 commit d681edd
Showing 9 changed files with 136 additions and 30 deletions.
119 changes: 119 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Empty file added public/pregen/.gitkeep
Empty file.
10 changes: 9 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -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({
2 changes: 0 additions & 2 deletions src/components/logic/DetailControls.svelte
Original file line number Diff line number Diff line change
@@ -53,8 +53,6 @@
}
$: displayedButtons = calculate($systemidStore);
console.log(displayedButtons);
</script>

{#if displayedButtons.length > 0}
16 changes: 2 additions & 14 deletions src/generate.js
Original file line number Diff line number Diff line change
@@ -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);
5 changes: 1 addition & 4 deletions src/lib/easydbPregen.js
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 3 additions & 7 deletions src/lib/stores.js
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -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()]
})

0 comments on commit d681edd

Please sign in to comment.