Skip to content
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

chore: anticipate vite cache changes #278

Merged
merged 1 commit into from Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -8,8 +8,7 @@ test('should render component imported via svelte field in package.json', async

if (!isBuild) {
test('should optimize nested cjs deps of excluded svelte deps', () => {
const vitePrebundleMetadata = path.resolve(__dirname, '../node_modules/.vite/_metadata.json');
const metadataFile = fs.readFileSync(vitePrebundleMetadata, 'utf8');
const metadataFile = readVitePrebundleMetadata();
const metadata = JSON.parse(metadataFile);
const optimizedPaths = Object.keys(metadata.optimized);
expect(optimizedPaths).not.toContain('e2e-test-dep-svelte-nested');
Expand All @@ -19,3 +18,20 @@ if (!isBuild) {
);
});
}

function readVitePrebundleMetadata() {
const metadataPaths = [
'../node_modules/.vite/_metadata.json',
'../node_modules/.vite/deps/_metadata.json' // vite 2.9
];
for (const metadataPath of metadataPaths) {
try {
const vitePrebundleMetadata = path.resolve(__dirname, metadataPath);
const metadataFile = fs.readFileSync(vitePrebundleMetadata, 'utf8');
return metadataFile;
} catch {
// ignore
}
}
throw new Error('Unable to find vite prebundle metadata');
}
15 changes: 11 additions & 4 deletions packages/vite-plugin-svelte/src/utils/optimizer.ts
Expand Up @@ -16,11 +16,10 @@ const PREBUNDLE_SENSITIVE_OPTIONS: (keyof ResolvedOptions)[] = [
export async function handleOptimizeDeps(options: ResolvedOptions, viteConfig: ResolvedConfig) {
if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir) return;

const viteMetadataPath = path.resolve(viteConfig.cacheDir, '_metadata.json');
const viteMetadataPath = findViteMetadataPath(viteConfig.cacheDir);
if (!viteMetadataPath) return;

if (!fs.existsSync(viteMetadataPath)) return;

const svelteMetadataPath = path.resolve(viteConfig.cacheDir, '_svelte_metadata.json');
const svelteMetadataPath = path.resolve(viteMetadataPath, '../_svelte_metadata.json');
const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
return typeof value === 'function' ? value.toString() : value;
});
Expand All @@ -41,3 +40,11 @@ function generateSvelteMetadata(options: ResolvedOptions) {
}
return metadata;
}

function findViteMetadataPath(cacheDir: string) {
const metadataPaths = ['_metadata.json', 'deps/_metadata.json'];
for (const metadataPath of metadataPaths) {
const viteMetadataPath = path.resolve(cacheDir, metadataPath);
if (fs.existsSync(viteMetadataPath)) return viteMetadataPath;
}
}