-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Astro plugin and compile flow (#5506)
- Loading branch information
Showing
9 changed files
with
335 additions
and
266 deletions.
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.