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

feat: re-prebundle when config changed #245

Merged
merged 2 commits into from
Jan 3, 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
5 changes: 5 additions & 0 deletions .changeset/rare-crews-give.md
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`
5 changes: 5 additions & 0 deletions packages/vite-plugin-svelte/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ensureWatchedFile, setupWatchers } from './utils/watch';
import { resolveViaPackageJsonSvelte } from './utils/resolve';
import { PartialResolvedId } from 'rollup';
import { toRollupError } from './utils/error';
import { handleOptimizeDeps } from './utils/optimizer';

export function svelte(inlineOptions?: Partial<Options>): Plugin {
if (process.env.DEBUG != null) {
Expand Down Expand Up @@ -69,6 +70,10 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
log.debug('resolved options', options);
},

async buildStart() {
await handleOptimizeDeps(options, viteConfig);
},

configureServer(server) {
// eslint-disable-next-line no-unused-vars
options.server = server;
Expand Down
43 changes: 43 additions & 0 deletions packages/vite-plugin-svelte/src/utils/optimizer.ts
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;
}
2 changes: 1 addition & 1 deletion packages/vite-plugin-svelte/src/utils/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function setupWatchers(
});
} else {
log.info(`svelte config changed: restarting vite server. - file: ${filename}`);
server.restart(!!options.experimental?.prebundleSvelteLibraries);
server.restart();
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}
};

Expand Down