-
-
Notifications
You must be signed in to change notification settings - Fork 117
feat: reduce use of cache in vite-plugin-svelte #32
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
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/vite-plugin-svelte': minor | ||
--- | ||
|
||
Reduced cache usage, share css cache between SSR and client |
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
44 changes: 17 additions & 27 deletions
44
packages/vite-plugin-svelte/src/utils/VitePluginSvelteCache.ts
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 |
---|---|---|
@@ -1,37 +1,27 @@ | ||
import { SvelteRequest } from './id'; | ||
import { CompileData } from './compile'; | ||
import { Code, CompileData } from './compile'; | ||
|
||
export class VitePluginSvelteCache { | ||
private _compile = new Map<string, CompileData>(); | ||
private _compileSSR = new Map<string, CompileData>(); | ||
private _css = new Map<string, Code>(); | ||
private _js = new Map<string, Code>(); | ||
|
||
private selectCache(ssr: boolean): Map<string, CompileData> { | ||
return ssr ? this._compileSSR : this._compile; | ||
} | ||
|
||
public getCompileData( | ||
svelteRequest: SvelteRequest, | ||
errorOnMissing = true | ||
): CompileData | undefined { | ||
const cache = this.selectCache(svelteRequest.ssr); | ||
const id = svelteRequest.normalizedFilename; | ||
if (cache.has(id)) { | ||
return cache.get(id)!; | ||
} | ||
if (errorOnMissing) { | ||
throw new Error( | ||
`${id} has no corresponding entry in the ${svelteRequest.ssr ? 'ssr' : ''}cache. ` + | ||
`This is a @sveltejs/vite-plugin-svelte internal error, please open an issue.` | ||
); | ||
public update(compileData: CompileData) { | ||
const id = compileData.normalizedFilename; | ||
this._css.set(id, compileData.compiled.css); | ||
if (!compileData.ssr) { | ||
// do not cache SSR js | ||
this._js.set(id, compileData.compiled.js); | ||
} | ||
} | ||
|
||
public setCompileData(compileData: CompileData) { | ||
const cache = this.selectCache(!!compileData.ssr); | ||
const id = compileData.normalizedFilename; | ||
cache.set(id, compileData); | ||
public getCSS(svelteRequest: SvelteRequest) { | ||
return this._css.get(svelteRequest.normalizedFilename); | ||
} | ||
|
||
// TODO accessors by id/url? | ||
// TODO expose on plugin instance? | ||
public getJS(svelteRequest: SvelteRequest) { | ||
if (!svelteRequest.ssr) { | ||
// SSR js isn't cached | ||
return this._js.get(svelteRequest.normalizedFilename); | ||
} | ||
} | ||
} |
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
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.