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
rodzic
3095e5b16a
commit
d73116509c
|
@ -2,4 +2,5 @@ node_modules
|
||||||
dist
|
dist
|
||||||
dist-watch
|
dist-watch
|
||||||
.cache
|
.cache
|
||||||
lib/_svg-vocabulary.json
|
lib/_svg-vocabulary-pretty-printed.json
|
||||||
|
lib/_svg-vocabulary.ts
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
import type { SvgSymbolData } from "./svg-symbol";
|
import type { SvgSymbolData } from "./svg-symbol";
|
||||||
import { Vocabulary } from "./vocabulary";
|
import { Vocabulary } from "./vocabulary";
|
||||||
import _SvgVocabulary from "./_svg-vocabulary.json";
|
import _SvgVocabulary from "./_svg-vocabulary";
|
||||||
|
|
||||||
export const SvgVocabulary = new Vocabulary<SvgSymbolData>(
|
export const SvgVocabulary = new Vocabulary<SvgSymbolData>(_SvgVocabulary);
|
||||||
_SvgVocabulary as any
|
|
||||||
);
|
|
||||||
|
|
|
@ -12,7 +12,11 @@ const SUPPORTED_SVG_TAGS = new Set(SUPPORTED_SVG_TAG_ARRAY);
|
||||||
|
|
||||||
const MY_DIR = __dirname;
|
const MY_DIR = __dirname;
|
||||||
export const SVG_SYMBOLS_DIR = path.join(MY_DIR, "..", "assets", "symbols");
|
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";
|
const SVG_EXT = ".svg";
|
||||||
|
|
||||||
function onlyTags(
|
function onlyTags(
|
||||||
|
@ -152,6 +156,24 @@ export function build() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Writing ${VOCAB_PATH}.`);
|
console.log(`Writing ${VOCAB_JSON_PATH} (for debugging output).`);
|
||||||
fs.writeFileSync(VOCAB_PATH, JSON.stringify(vocab, null, 2));
|
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")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue