-
-
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
Draft
florian-lefebvre
wants to merge
17
commits into
next
Choose a base branch
from
feat/updated-loaders
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+347
−113
Draft
feat: updated loaders #14594
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c227582
wip
florian-lefebvre e79202f
Merge branch 'next' into feat/updated-loaders
florian-lefebvre 17d6570
wip
florian-lefebvre 2b4e38e
feat: simplify
florian-lefebvre e350966
Merge branch 'next' into feat/updated-loaders
florian-lefebvre 75d6661
feat: simplify types
florian-lefebvre 532143b
feat: update name
florian-lefebvre 68a810f
feat: early references check poc
florian-lefebvre cec45ce
chore: clean
florian-lefebvre 3d36f09
chore: clean
florian-lefebvre 05d1954
chore: clean
florian-lefebvre f03e5f2
chore: clean
florian-lefebvre ad5a7f6
fix: test
florian-lefebvre 6bac68f
Merge branch 'next' into feat/updated-loaders
florian-lefebvre cb08d45
feat: split
florian-lefebvre c8567da
feat: improve
florian-lefebvre 771939a
chore: todo
florian-lefebvre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| import { Traverse } from 'neotraverse/modern'; | ||
| import { IMAGE_IMPORT_PREFIX } from './consts.js'; | ||
| import type { DataEntry } from './data-store.js'; | ||
| import type { DataStore } from './mutable-data-store.js'; | ||
|
|
||
| export class InMemoryDataStore implements DataStore { | ||
| #collection: 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; | ||
| } | ||
| > = []; | ||
|
|
||
| constructor(collection: Map<string, any>) { | ||
| this.#collection = collection; | ||
| } | ||
|
|
||
| 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, | ||
| }); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.