-
Notifications
You must be signed in to change notification settings - Fork 320
Implement enableDevCache
option in Vite plugin
#1591
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
Open
hartz89
wants to merge
3
commits into
vanilla-extract-css:master
Choose a base branch
from
hartz89:vite-dev-cache-option
base: master
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.
Open
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 @@ | ||
--- | ||
'@vanilla-extract/vite-plugin': minor | ||
--- | ||
|
||
Implement an `enableDevCache` option in the Vite plugin which helps alleviate dev server performance issues in large projects caused by redundant `.vanilla.css` file loading |
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,5 +1,5 @@ | ||
import path from 'path'; | ||
|
||
import fs from 'fs/promises'; | ||
import type { | ||
Plugin, | ||
ResolvedConfig, | ||
|
@@ -16,6 +16,7 @@ import { | |
getPackageInfo, | ||
transform, | ||
normalizePath, | ||
hash | ||
} from '@vanilla-extract/integration'; | ||
|
||
const PLUGIN_NAME = 'vite-plugin-vanilla-extract'; | ||
|
@@ -41,6 +42,13 @@ type PluginFilter = (filterProps: { | |
|
||
interface Options { | ||
identifiers?: IdentifierOption; | ||
/** | ||
* Enables hash-based (MD5) caching of `.css.ts` file contents in dev mode to avoid unnecessary module | ||
* reloads. Helpful in large projects where the same `.css.ts` files (e.g. sprinkles) are imported | ||
* in many places and we don't want the dev client to have to re-download them every time they're imported. | ||
* @default false | ||
*/ | ||
enableDevCache?: boolean; | ||
unstable_pluginFilter?: PluginFilter; | ||
unstable_mode?: 'transform' | 'emitCss'; | ||
} | ||
|
@@ -59,6 +67,7 @@ const withUserPluginFilter = | |
|
||
export function vanillaExtractPlugin({ | ||
identifiers, | ||
enableDevCache = false, | ||
unstable_pluginFilter: pluginFilter = defaultPluginFilter, | ||
unstable_mode: mode = 'emitCss', | ||
}: Options = {}): Plugin { | ||
|
@@ -91,14 +100,54 @@ export function vanillaExtractPlugin({ | |
return normalizePath(resolvedId); | ||
}; | ||
|
||
// Cache for file content hashes to avoid unnecessary invalidations | ||
const fileContentHashes = new Map<string, string>(); | ||
|
||
// Helper to hash file contents | ||
async function getFileHash(filePath: string): Promise<string | undefined> { | ||
try { | ||
const content = await fs.readFile(filePath, 'utf8'); | ||
return hash(content); | ||
} catch { | ||
console.warn('Unable to read file for hash calculation:', filePath); | ||
// If file can't be read, treat as changed | ||
return undefined; | ||
} | ||
} | ||
|
||
// Helper to determine if a module should be invalidated based on dev cache | ||
async function shouldInvalidateModule(moduleId: string): Promise<boolean> { | ||
if ( | ||
!enableDevCache || | ||
!moduleId | ||
) { | ||
return true; | ||
} | ||
|
||
const hash = await getFileHash(moduleId); | ||
|
||
if (!hash) { | ||
return true; | ||
} | ||
|
||
const prevHash = fileContentHashes.get(moduleId); | ||
|
||
if (hash !== prevHash) { | ||
fileContentHashes.set(moduleId, hash); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function invalidateModule(absoluteId: string) { | ||
if (!server) return; | ||
|
||
const { moduleGraph } = server; | ||
const modules = moduleGraph.getModulesByFile(absoluteId); | ||
|
||
if (modules) { | ||
for (const module of modules) { | ||
for (const module of modules) { | ||
moduleGraph.invalidateModule(module); | ||
|
||
// Vite uses this timestamp to add `?t=` query string automatically for HMR. | ||
|
@@ -217,9 +266,10 @@ export function vanillaExtractPlugin({ | |
} | ||
|
||
for (const file of watchFiles) { | ||
const normalizedFilePath = normalizePath(file); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't sure if I should put the invalidation check before or after the "add watch file" block, or how these blocks might affect one another. Let me know if you have thoughts. |
||
if ( | ||
!file.includes('node_modules') && | ||
normalizePath(file) !== absoluteId | ||
normalizedFilePath !== absoluteId | ||
) { | ||
this.addWatchFile(file); | ||
} | ||
|
@@ -228,6 +278,12 @@ export function vanillaExtractPlugin({ | |
// The deps have to be invalidated in case one of them changing was the trigger causing | ||
// the current transformation | ||
if (cssFileFilter.test(file)) { | ||
const shouldInvalidate = await shouldInvalidateModule(normalizedFilePath); | ||
|
||
if (!shouldInvalidate) { | ||
continue; | ||
} | ||
|
||
invalidateModule(fileIdToVirtualId(file)); | ||
} | ||
} | ||
|
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.
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.
Is the hashing step just a waste of CPU time? Should we just use the file contents as the values of the map? Hashing CPU time is a trade-off for cache memory usage.