Skip to content

Commit

Permalink
fix: clear cache before start dev server
Browse files Browse the repository at this point in the history
  • Loading branch information
dgaponov committed Jan 29, 2025
1 parent c7a2528 commit b09951c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/commands/dev/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {RspackMode, rspackConfigFactory} from '../../common/rspack/config';

import type {Configuration, HttpProxyMiddlewareOptionsFilter} from 'webpack-dev-server';
import type {NormalizedServiceConfig} from '../../common/models';
import {clearCacheDirectory} from '../../common/rspack/utils';

export async function watchClientCompilation(
config: NormalizedServiceConfig,
Expand Down Expand Up @@ -156,6 +157,8 @@ async function buildDevServer(config: NormalizedServiceConfig) {
// Rspack multicompiler dont work with lazy compilation
const compiler = rspack(rspackConfigs[0]!);

Check warning on line 158 in src/commands/dev/client.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Forbidden non-null assertion
server = new RspackDevServer(options, compiler);
// Need to clean cache before start. https://github.com/web-infra-dev/rspack/issues/9025
clearCacheDirectory(rspackConfigs[0]!, logger);

Check warning on line 161 in src/commands/dev/client.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Forbidden non-null assertion
} else {
const compiler = webpack(webpackConfigs);
server = new WebpackDevServer(options, compiler);
Expand Down
7 changes: 7 additions & 0 deletions src/common/rspack/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ function configureExperiments({
snapshot: {
managedPaths: config.watchOptions?.watchPackages ? [] : undefined,
},
storage: {
type: 'filesystem',
directory:
typeof config.cache === 'object' && 'cacheDirectory' in config.cache
? config.cache.cacheDirectory
: undefined,
},
},
lazyCompilation,
};
Expand Down
27 changes: 25 additions & 2 deletions src/common/rspack/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import {prettyTime} from '../logger/pretty-time';

import type {MultiStats} from '@rspack/core';
import * as fs from 'node:fs';
import * as path from 'node:path';
import type {Configuration, MultiStats} from '@rspack/core';
import type {Logger} from '../logger';
import paths from '../../common/paths';

export function clearCacheDirectory(config: Configuration, logger: Logger) {
if (!config.cache) {
return;
}

let cacheDirectory = path.join(paths.appNodeModules, '.cache/rspack');

if (
typeof config.experiments?.cache === 'object' &&
config.experiments.cache.type === 'persistent' &&
config.experiments.cache.storage?.directory
) {
cacheDirectory = config.experiments.cache.storage?.directory;
}

if (fs.existsSync(cacheDirectory)) {
fs.rmdirSync(cacheDirectory, {recursive: true});
logger.message(`Rspack cache ${cacheDirectory} successfully cleared`);
}
}

export function rspackCompilerHandlerFactory(logger: Logger, onCompilationEnd?: () => void) {
return async (err?: Error | null, stats?: MultiStats) => {
Expand Down

0 comments on commit b09951c

Please sign in to comment.