Skip to content

Commit

Permalink
chore: Change the default Vite cacheDir
Browse files Browse the repository at this point in the history
  • Loading branch information
spring-raining committed Jan 14, 2025
1 parent 120edd3 commit 416f829
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/config/vite.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import upath from 'upath';
import {
ConfigEnv,
createLogger,
Expand All @@ -24,10 +25,16 @@ export async function resolveViteConfig({
viteConfig,
viteConfigFile,
logLevel,
workspaceDir,
mode,
}: Pick<
ResolvedTaskConfig,
'context' | 'server' | 'viteConfig' | 'viteConfigFile' | 'logLevel'
| 'context'
| 'server'
| 'viteConfig'
| 'viteConfigFile'
| 'logLevel'
| 'workspaceDir'
> & {
mode: 'preview' | 'build';
}): Promise<ResolvedViteConfig> {
Expand Down Expand Up @@ -69,6 +76,7 @@ export async function resolveViteConfig({
configFile: viteConfigFile === true ? undefined : viteConfigFile,
root: context,
customLogger: viteLogger,
cacheDir: upath.join(workspaceDir, '.vite'),
} satisfies InlineConfig);
return await resolveConfig(
finalUserConfig,
Expand Down
17 changes: 16 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import { ResolvedTaskConfig } from './config/resolve.js';
import { ParsedVivliostyleInlineConfig } from './config/schema.js';
import { EMPTY_DATA_URI, VIEWER_ROOT_PATH } from './const.js';
import { Logger } from './logger.js';
import { getDefaultEpubOpfPath, isValidUri, openEpub } from './util.js';
import {
getDefaultEpubOpfPath,
isValidUri,
openEpub,
registerExitHandler,
} from './util.js';
import { vsBrowserPlugin } from './vite/vite-plugin-browser.js';
import { vsDevServerPlugin } from './vite/vite-plugin-dev-server.js';
import { vsStaticServePlugin } from './vite/vite-plugin-static-serve.js';
Expand Down Expand Up @@ -206,9 +211,19 @@ export async function createViteServer({
server: viteConfig.server,
preview: viteConfig.preview,
customLogger: viteConfig.customLogger,
cacheDir: viteConfig.cacheDir,
} satisfies InlineConfig;
Logger.debug('createViteServer > viteInlineConfig %O', viteInlineConfig);

if (config.context === config.workspaceDir) {
const { cacheDir } = viteInlineConfig;
registerExitHandler('Removing the Vite cacheDir', () => {
if (fs.existsSync(cacheDir)) {
fs.rmSync(cacheDir, { recursive: true });
}
});
}

if (mode === 'preview') {
return await createServer(viteInlineConfig);
} else {
Expand Down
6 changes: 5 additions & 1 deletion tests/global-setup/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { glob } from 'tinyglobby';

const tmpFilePatterns = [
'tests/fixtures/**/.vs-*',
'tests/fixtures/**/.vite',
'tests/fixtures/config/**/vvv.*',
'tests/fixtures/cover/publication.json',
'tests/fixtures/toc/publication.json',
];
Expand All @@ -14,7 +16,9 @@ export default function clean() {
dot: true,
});
for (const file of files) {
fs.rmSync(file, { recursive: true });
if (fs.existsSync(file)) {
fs.rmSync(file, { recursive: true });
}
}
};
}

0 comments on commit 416f829

Please sign in to comment.