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
Refactor Astro plugin and compile flow #5506
Merged
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
402c876
Dedupe Astro package when resolving
bluwy d74f39b
Improve Astro cache loading
bluwy 58cd555
Move cache to separate file
bluwy 1f63374
Handle errors as a separate function
bluwy 8b2ba45
Extract main cached compilation flow
bluwy 6eae3b4
Refactor error handling
bluwy 0bc8d0e
Fix missing paths
bluwy 94c62a9
Add changeset
bluwy 7997043
Merge branch 'main' into astro-plugin-refactor
bluwy a09f0d9
Try fix windows
bluwy d1e176f
Revert raw resolve for now
bluwy b9d496a
Fix typo
bluwy 6282c33
I hate windows
bluwy be75a66
Merge branch 'main' into astro-plugin-refactor
bluwy File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Dedupe Astro package when resolving |
This file contains 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,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Refactor Astro compile flow |
This file contains 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,42 @@ | ||
import type { AstroConfig } from '../../@types/astro'; | ||
import { compile, CompileProps, CompileResult } from './compile.js'; | ||
|
||
type CompilationCache = Map<string, CompileResult>; | ||
|
||
const configCache = new WeakMap<AstroConfig, CompilationCache>(); | ||
|
||
export function isCached(config: AstroConfig, filename: string) { | ||
return configCache.has(config) && configCache.get(config)!.has(filename); | ||
} | ||
|
||
export function getCachedCompileResult( | ||
config: AstroConfig, | ||
filename: string | ||
): CompileResult | null { | ||
if (!isCached(config, filename)) return null; | ||
return configCache.get(config)!.get(filename)!; | ||
} | ||
|
||
export function invalidateCompilation(config: AstroConfig, filename: string) { | ||
if (configCache.has(config)) { | ||
const cache = configCache.get(config)!; | ||
cache.delete(filename); | ||
} | ||
} | ||
|
||
export async function cachedCompilation(props: CompileProps): Promise<CompileResult> { | ||
const { astroConfig, filename } = props; | ||
let cache: CompilationCache; | ||
if (!configCache.has(astroConfig)) { | ||
cache = new Map(); | ||
configCache.set(astroConfig, cache); | ||
} else { | ||
cache = configCache.get(astroConfig)!; | ||
} | ||
if (cache.has(filename)) { | ||
return cache.get(filename)!; | ||
} | ||
const compileResult = await compile(props); | ||
cache.set(filename, compileResult); | ||
return compileResult; | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
export type { CompileProps } from './compile'; | ||
export { cachedCompilation, getCachedSource, invalidateCompilation, isCached } from './compile.js'; | ||
export { | ||
cachedCompilation, | ||
getCachedCompileResult, | ||
invalidateCompilation, | ||
isCached, | ||
} from './cache.js'; | ||
export type { CompileProps, CompileResult } from './compile'; | ||
export type { TransformStyle } from './types'; |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that the caching part is split into a separate module now!