Skip to content

Commit

Permalink
feat: re-prebundle when config changed (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jan 3, 2022
1 parent fb5591f commit 5a31d04
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
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();
}
};

Expand Down

0 comments on commit 5a31d04

Please sign in to comment.