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: normalize cache logic #1967

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ten-dolls-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/core": major
---

bump version
2 changes: 1 addition & 1 deletion examples/arco-pro/farm.config.ts
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ export default defineConfig((env) => {
sourcemap: false,
presetEnv: false,
concatenateModules: true,
persistentCache: false,
// persistentCache: false,
resolve: {
symlinks: true,
alias: {
12 changes: 6 additions & 6 deletions examples/refactor-react/farm.config.ts
Original file line number Diff line number Diff line change
@@ -41,25 +41,25 @@ export default defineConfig({
// compilerPlugin(),
custom()
],
server: {
port: 4855,
appType: "mpa",
https: true
},
compilation: {
input: {
index: path.resolve(__dirname, "index.html"),
base: path.resolve(__dirname, 'base.html'),
about: path.resolve(__dirname, 'about.html'),
},
progress: false,
persistentCache: false,
// persistentCache: false,
// persistentCache: {
// cacheDir: "node_modules/.adny",
// },
output: {
// publicPath: "/aaa/",
},
server: {
port: 4855,
appType: "mpa",
https: true
},
}
});

Original file line number Diff line number Diff line change
@@ -3,12 +3,14 @@ import { createRequire } from 'node:module';
import path from 'node:path';

import { RustPlugin } from '../../plugin/index.js';
import { Config } from '../../types/binding.js';
import { Config, GlobalBuiltinCacheKeyStrategy } from '../../types/binding.js';
import { traceDependencies } from '../../utils/trace-dependencies.js';
import { isDisableCache } from '../env.js';
import { ResolvedUserConfig } from '../index.js';

const defaultGlobalBuiltinCacheKeyStrategy = {
const DEFAULT_CACHE_DIR = 'node_modules/.farm/cache';
const DEFAULT_PACKAGE_JSON = 'package.json';
const defaultGlobalBuiltinCacheKeyStrategy: GlobalBuiltinCacheKeyStrategy = {
define: true,
buildDependencies: true,
lockfile: true,
@@ -28,33 +30,24 @@ export async function normalizePersistentCache(
return;
}

if (
typeof config.persistentCache === 'object' &&
config.persistentCache.cacheDir
) {
config.persistentCache.cacheDir = path.resolve(
config.root,
config.persistentCache.cacheDir
);
}

if (config.persistentCache === true || config.persistentCache == undefined) {
config.persistentCache = {
buildDependencies: [],
moduleCacheKeyStrategy: {},
envs: {}
};
}

config.persistentCache.cacheDir = path.resolve(
config.root,
config.persistentCache.cacheDir || DEFAULT_CACHE_DIR
);
// globalCacheKeyStrategy should not be passed to rust
let { globalBuiltinCacheKeyStrategy } = config.persistentCache;
delete config.persistentCache.globalBuiltinCacheKeyStrategy;
if (!globalBuiltinCacheKeyStrategy) {
globalBuiltinCacheKeyStrategy = {};
}
globalBuiltinCacheKeyStrategy = {
const globalBuiltinCacheKeyStrategy = {
...defaultGlobalBuiltinCacheKeyStrategy,
...globalBuiltinCacheKeyStrategy
...(config.persistentCache?.globalBuiltinCacheKeyStrategy ?? {})
};
delete config.persistentCache.globalBuiltinCacheKeyStrategy;

if (globalBuiltinCacheKeyStrategy.env) {
config.persistentCache.envs = {
@@ -84,10 +77,7 @@ export async function normalizePersistentCache(
}

// add type of package.json to envs
const packageJsonPath = path.join(
config.root ?? process.cwd(),
'package.json'
);
const packageJsonPath = path.join(config.root, DEFAULT_PACKAGE_JSON);

if (globalBuiltinCacheKeyStrategy.packageJson) {
if (existsSync(packageJsonPath)) {
26 changes: 14 additions & 12 deletions packages/core/src/types/binding.ts
Original file line number Diff line number Diff line change
@@ -323,6 +323,19 @@ export interface CssConfig {
_viteCssOptions?: any;
}

export interface GlobalBuiltinCacheKeyStrategy {
/** @default true */
define?: boolean;
/** @default true */
buildDependencies?: boolean;
/** @default true */
lockfile?: boolean;
/** @default true */
packageJson?: boolean;
/** @default true */
env?: boolean;
}

export interface PersistentCacheConfig {
namespace?: string;
cacheDir?: string;
@@ -341,18 +354,7 @@ export interface PersistentCacheConfig {
* lockfile: false
* }
*/
globalBuiltinCacheKeyStrategy?: {
/** @default true */
define?: boolean;
/** @default true */
buildDependencies?: boolean;
/** @default true */
lockfile?: boolean;
/** @default true */
packageJson?: boolean;
/** @default true */
env?: boolean;
};
globalBuiltinCacheKeyStrategy?: GlobalBuiltinCacheKeyStrategy;
}

export interface PartialBundlingConfig {
4 changes: 2 additions & 2 deletions packages/core/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -271,8 +271,8 @@ export function bootstrap(
const shortFile = getShortName(config.configFilePath, config.root);
config.logger.info(`Using config file at ${bold(green(shortFile))}`, true);
}
const usePersistentCache = config.compilation.persistentCache && hasCacheDir;
const persistentCacheFlag = usePersistentCache
const hasPersistentCache = config.compilation.persistentCache && hasCacheDir;
const persistentCacheFlag = hasPersistentCache
? colors.bold(PersistentCacheBrand)
: '';