-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: re-prebundle when config changed (#245)
- Loading branch information
Showing
4 changed files
with
54 additions
and
1 deletion.
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 | ||
--- | ||
|
||
Automatically re-prebundle when Svelte config changed for `experimental.prebundleSvelteLibraries` |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { optimizeDeps, ResolvedConfig } from 'vite'; | ||
import { ResolvedOptions } from './options'; | ||
|
||
// List of options that changes the prebundling result | ||
const PREBUNDLE_SENSITIVE_OPTIONS: (keyof ResolvedOptions)[] = [ | ||
'compilerOptions', | ||
'configFile', | ||
'experimental', | ||
'extensions', | ||
'ignorePluginPreprocessors', | ||
'preprocess' | ||
]; | ||
|
||
export async function handleOptimizeDeps(options: ResolvedOptions, viteConfig: ResolvedConfig) { | ||
if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir) return; | ||
|
||
const viteMetadataPath = path.resolve(viteConfig.cacheDir, '_metadata.json'); | ||
|
||
if (!fs.existsSync(viteMetadataPath)) return; | ||
|
||
const svelteMetadataPath = path.resolve(viteConfig.cacheDir, '_svelte_metadata.json'); | ||
const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => { | ||
return typeof value === 'function' ? value.toString() : value; | ||
}); | ||
|
||
if (fs.existsSync(svelteMetadataPath)) { | ||
const existingSvelteMetadata = fs.readFileSync(svelteMetadataPath, 'utf8'); | ||
if (existingSvelteMetadata === currentSvelteMetadata) return; | ||
} | ||
|
||
await optimizeDeps(viteConfig, true); | ||
fs.writeFileSync(svelteMetadataPath, currentSvelteMetadata); | ||
} | ||
|
||
function generateSvelteMetadata(options: ResolvedOptions) { | ||
const metadata: Record<string, any> = {}; | ||
for (const key of PREBUNDLE_SENSITIVE_OPTIONS) { | ||
metadata[key] = options[key]; | ||
} | ||
return metadata; | ||
} |
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