Code-split mcschema imports per version

This commit is contained in:
Misode
2022-05-06 16:53:07 +02:00
parent 690e2f60bf
commit 3ed7fd8bdd

View File

@@ -1,11 +1,5 @@
import type { CollectionRegistry, INode, SchemaRegistry } from '@mcschema/core'
import { ChoiceNode, DataModel, Reference, StringNode } from '@mcschema/core'
import * as java15 from '@mcschema/java-1.15'
import * as java16 from '@mcschema/java-1.16'
import * as java17 from '@mcschema/java-1.17'
import * as java18 from '@mcschema/java-1.18'
import * as java182 from '@mcschema/java-1.18.2'
import * as java19 from '@mcschema/java-1.19'
import config from '../../config.json'
import { message } from '../Utils'
import { fetchData } from './DataFetcher'
@@ -38,17 +32,17 @@ type ModelData = {
const Models: Record<string, ModelData> = {}
const versionGetter: {
[versionId in VersionId]: {
[versionId in VersionId]: () => Promise<{
getCollections: () => CollectionRegistry,
getSchemas: (collections: CollectionRegistry) => SchemaRegistry,
}
}>
} = {
1.15: java15,
1.16: java16,
1.17: java17,
1.18: java18,
'1.18.2': java182,
1.19: java19,
1.15: () => import('@mcschema/java-1.15'),
1.16: () => import('@mcschema/java-1.16'),
1.17: () => import('@mcschema/java-1.17'),
1.18: () => import('@mcschema/java-1.18'),
'1.18.2': () => import('@mcschema/java-1.18.2'),
1.19: () => import('@mcschema/java-1.19'),
}
export let CachedDecorator: INode<any>
@@ -59,10 +53,11 @@ async function getVersion(id: VersionId): Promise<VersionData> {
if (!Versions[id]) {
Versions[id] = (async () => {
try {
const collections = versionGetter[id].getCollections()
const mcschema = await versionGetter[id]()
const collections = mcschema.getCollections()
const blockStates: BlockStateRegistry = {}
await fetchData(id, collections, blockStates)
const schemas = versionGetter[id].getSchemas(collections)
const schemas = mcschema.getSchemas(collections)
Versions[id] = { collections, schemas, blockStates }
return Versions[id]
} catch (e) {