-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reduce use of cache in vite-plugin-svelte (#32)
- Loading branch information
Showing
6 changed files
with
56 additions
and
88 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 @@ | ||
--- | ||
'@sveltejs/vite-plugin-svelte': minor | ||
--- | ||
|
||
Reduced cache usage, share css cache between SSR and client |
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
44 changes: 17 additions & 27 deletions
44
packages/vite-plugin-svelte/src/utils/VitePluginSvelteCache.ts
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,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 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