Move SVG vocabulary from JSON to TS. (#59)

Ugh, we need to write out a TypeScript file instead of importing the JSON directly because otherwise the TS compiler will spend an inordinate amount of time doing type inference, which massively slows down type-checking (especially in IDEs and such).

The TS file actually uses `JSON.parse` on a stringified version of the JSON instead of just inlining the JSON itself because [apparently it's much faster][1].

[1]: https://www.bram.us/2019/11/25/faster-javascript-apps-with-json-parse/
pull/60/head
Atul Varma 2021-03-27 16:00:20 -04:00 zatwierdzone przez GitHub
rodzic 3095e5b16a
commit d73116509c
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
3 zmienionych plików z 29 dodań i 8 usunięć

3
.gitignore vendored
Wyświetl plik

@ -2,4 +2,5 @@ node_modules
dist
dist-watch
.cache
lib/_svg-vocabulary.json
lib/_svg-vocabulary-pretty-printed.json
lib/_svg-vocabulary.ts

Wyświetl plik

@ -1,7 +1,5 @@
import type { SvgSymbolData } from "./svg-symbol";
import { Vocabulary } from "./vocabulary";
import _SvgVocabulary from "./_svg-vocabulary.json";
import _SvgVocabulary from "./_svg-vocabulary";
export const SvgVocabulary = new Vocabulary<SvgSymbolData>(
_SvgVocabulary as any
);
export const SvgVocabulary = new Vocabulary<SvgSymbolData>(_SvgVocabulary);

Wyświetl plik

@ -12,7 +12,11 @@ const SUPPORTED_SVG_TAGS = new Set(SUPPORTED_SVG_TAG_ARRAY);
const MY_DIR = __dirname;
export const SVG_SYMBOLS_DIR = path.join(MY_DIR, "..", "assets", "symbols");
const VOCAB_PATH = path.join(MY_DIR, "_svg-vocabulary.json");
const VOCAB_JSON_PATH = path.join(
MY_DIR,
"_svg-vocabulary-pretty-printed.json"
);
const VOCAB_TS_PATH = path.join(MY_DIR, "_svg-vocabulary.ts");
const SVG_EXT = ".svg";
function onlyTags(
@ -152,6 +156,24 @@ export function build() {
}
}
console.log(`Writing ${VOCAB_PATH}.`);
fs.writeFileSync(VOCAB_PATH, JSON.stringify(vocab, null, 2));
console.log(`Writing ${VOCAB_JSON_PATH} (for debugging output).`);
fs.writeFileSync(VOCAB_JSON_PATH, JSON.stringify(vocab, null, 2));
// Ugh, we need to write out a TypeScript file instead of importing
// the JSON directly because otherwise the TS compiler will spend
// a huge amount of resources doing type inference, which massively
// slows down type-checking (especially in IDEs and such).
console.log(`Writing ${VOCAB_TS_PATH} (for importing into code).`);
const stringified = JSON.stringify(vocab);
fs.writeFileSync(
VOCAB_TS_PATH,
[
"// This file is auto-generated, please do not modify it.",
`import type { SvgSymbolData } from "./svg-symbol";`,
`const _SvgVocabulary: SvgSymbolData[] = JSON.parse(${JSON.stringify(
stringified
)});`,
`export default _SvgVocabulary;`,
].join("\n")
);
}