-
-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: updated loaders #14594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
feat: updated loaders #14594
Changes from 4 commits
c227582
e79202f
17d6570
2b4e38e
e350966
75d6661
532143b
68a810f
cec45ce
3d36f09
05d1954
f03e5f2
ad5a7f6
6bac68f
cb08d45
c8567da
771939a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -485,3 +485,162 @@ export interface MetaStore { | |
| has: (key: string) => boolean; | ||
| delete: (key: string) => void; | ||
| } | ||
|
|
||
| export class InMemoryDataStore implements DataStore { | ||
| #collection = new Map<string, any>(); | ||
| #events: Array< | ||
| | { | ||
| type: 'set'; | ||
| entry: DataEntry; | ||
| } | ||
| | { | ||
| type: 'delete'; | ||
| key: string; | ||
| } | ||
| | { | ||
| type: 'clear'; | ||
| } | ||
| | { | ||
| type: 'addAssetImport'; | ||
| assetImport: string; | ||
| filePath: string; | ||
| } | ||
| | { | ||
| type: 'addAssetImports'; | ||
| assets: Array<string>; | ||
| filePath: string; | ||
| } | ||
| | { | ||
| type: 'addModuleImport'; | ||
| filePath: string; | ||
| } | ||
| > = []; | ||
|
|
||
| get events() { | ||
| return this.#events; | ||
| } | ||
|
|
||
| get<TData extends Record<string, unknown> = Record<string, unknown>>( | ||
| key: string, | ||
| ): DataEntry<TData> { | ||
| return this.#collection.get(key); | ||
| } | ||
|
|
||
| entries() { | ||
| return [...this.#collection.entries()]; | ||
| } | ||
|
|
||
| values() { | ||
| return [...this.#collection.values()]; | ||
| } | ||
|
|
||
| keys() { | ||
| return [...this.#collection.keys()]; | ||
| } | ||
|
|
||
| set<TData extends Record<string, unknown>>(inputEntry: DataEntry<TData>) { | ||
| this.#events.push({ | ||
| type: 'set', | ||
| entry: inputEntry, | ||
| }); | ||
| const { | ||
| id: key, | ||
| data, | ||
| body, | ||
| filePath, | ||
| deferredRender, | ||
| digest, | ||
| rendered, | ||
| assetImports, | ||
| } = inputEntry; | ||
| if (!key) { | ||
| throw new Error(`ID must be a non-empty string`); | ||
| } | ||
| const id = String(key); | ||
| if (digest) { | ||
| const existing = this.#collection.get(id) as DataEntry | undefined; | ||
| if (existing && existing.digest === digest) { | ||
| return false; | ||
| } | ||
| } | ||
| const foundAssets = new Set<string>(assetImports); | ||
| // Check for image imports in the data. These will have been prefixed during schema parsing | ||
| new Traverse(data).forEach((_, val) => { | ||
| if (typeof val === 'string' && val.startsWith(IMAGE_IMPORT_PREFIX)) { | ||
| const src = val.replace(IMAGE_IMPORT_PREFIX, ''); | ||
| foundAssets.add(src); | ||
| } | ||
| }); | ||
|
||
|
|
||
| const entry: DataEntry = { | ||
| id, | ||
| data, | ||
| }; | ||
| // We do it like this so we don't waste space stringifying | ||
| // the fields if they are not set | ||
| if (body) { | ||
| entry.body = body; | ||
| } | ||
| if (filePath) { | ||
| if (filePath.startsWith('/')) { | ||
| throw new Error(`File path must be relative to the site root. Got: ${filePath}`); | ||
| } | ||
| entry.filePath = filePath; | ||
| } | ||
|
|
||
| if (foundAssets.size) { | ||
| entry.assetImports = Array.from(foundAssets); | ||
| } | ||
|
|
||
| if (digest) { | ||
| entry.digest = digest; | ||
| } | ||
| if (rendered) { | ||
| entry.rendered = rendered; | ||
| } | ||
| if (deferredRender) { | ||
| entry.deferredRender = deferredRender; | ||
| } | ||
| this.#collection.set(id, entry); | ||
| return true; | ||
| } | ||
|
|
||
| delete(key: string) { | ||
| this.#events.push({ | ||
| type: 'delete', | ||
| key, | ||
| }); | ||
| return this.#collection.delete(key); | ||
| } | ||
|
|
||
| clear() { | ||
| this.#collection = new Map(); | ||
| } | ||
|
|
||
| has(key: string) { | ||
| return this.#collection.has(key); | ||
| } | ||
|
|
||
| addAssetImport(assetImport: string, filePath: string) { | ||
| this.#events.push({ | ||
| type: 'addAssetImport', | ||
| assetImport, | ||
| filePath, | ||
| }); | ||
| } | ||
|
|
||
| addAssetImports(assets: Array<string>, filePath: string) { | ||
| this.#events.push({ | ||
| type: 'addAssetImports', | ||
| assets, | ||
| filePath, | ||
| }); | ||
| } | ||
|
|
||
| addModuleImport(filePath: string) { | ||
| this.#events.push({ | ||
| type: 'addModuleImport', | ||
| filePath, | ||
| }); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.