From d0ef5a2382bc8b858dc6762f5db40b93ad6af70e Mon Sep 17 00:00:00 2001 From: erkelost <1256029807@qq.com> Date: Tue, 26 Nov 2024 11:34:39 +0800 Subject: [PATCH 1/3] chore: normalize cache --- .changeset/ten-dolls-provide.md | 5 ++++ examples/arco-pro/farm.config.ts | 2 +- .../normalize-persistent-cache.ts | 17 ++++++------ packages/core/src/types/binding.ts | 26 ++++++++++--------- packages/core/src/utils/logger.ts | 4 +-- 5 files changed, 30 insertions(+), 24 deletions(-) create mode 100644 .changeset/ten-dolls-provide.md diff --git a/.changeset/ten-dolls-provide.md b/.changeset/ten-dolls-provide.md new file mode 100644 index 0000000000..7ee0cb18a7 --- /dev/null +++ b/.changeset/ten-dolls-provide.md @@ -0,0 +1,5 @@ +--- +"@farmfe/core": major +--- + +bump version diff --git a/examples/arco-pro/farm.config.ts b/examples/arco-pro/farm.config.ts index 73d55828bc..83895138fe 100644 --- a/examples/arco-pro/farm.config.ts +++ b/examples/arco-pro/farm.config.ts @@ -12,7 +12,7 @@ export default defineConfig((env) => { sourcemap: false, presetEnv: false, concatenateModules: true, - persistentCache: false, + // persistentCache: false, resolve: { symlinks: true, alias: { diff --git a/packages/core/src/config/normalize-config/normalize-persistent-cache.ts b/packages/core/src/config/normalize-config/normalize-persistent-cache.ts index 603c19ebd5..90c1c5c0ec 100644 --- a/packages/core/src/config/normalize-config/normalize-persistent-cache.ts +++ b/packages/core/src/config/normalize-config/normalize-persistent-cache.ts @@ -8,6 +8,7 @@ import { traceDependencies } from '../../utils/trace-dependencies.js'; import { isDisableCache } from '../env.js'; import { ResolvedUserConfig } from '../index.js'; +const DEFAULT_CACHE_DIR = 'node_modules/.farm/cache'; const defaultGlobalBuiltinCacheKeyStrategy = { define: true, buildDependencies: true, @@ -28,15 +29,7 @@ export async function normalizePersistentCache( return; } - if ( - typeof config.persistentCache === 'object' && - config.persistentCache.cacheDir - ) { - config.persistentCache.cacheDir = path.resolve( - config.root, - config.persistentCache.cacheDir - ); - } + // @ts-ignore if (config.persistentCache === true || config.persistentCache == undefined) { config.persistentCache = { @@ -45,6 +38,12 @@ export async function normalizePersistentCache( envs: {} }; } + + config.persistentCache.cacheDir = path.resolve( + config.root, + //@ts-ignore + config.persistentCache.cacheDir || DEFAULT_CACHE_DIR + ); // globalCacheKeyStrategy should not be passed to rust let { globalBuiltinCacheKeyStrategy } = config.persistentCache; delete config.persistentCache.globalBuiltinCacheKeyStrategy; diff --git a/packages/core/src/types/binding.ts b/packages/core/src/types/binding.ts index 5cc5582a5f..91d8889686 100644 --- a/packages/core/src/types/binding.ts +++ b/packages/core/src/types/binding.ts @@ -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 { diff --git a/packages/core/src/utils/logger.ts b/packages/core/src/utils/logger.ts index fdb579fee9..ba88996d0c 100644 --- a/packages/core/src/utils/logger.ts +++ b/packages/core/src/utils/logger.ts @@ -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) : ''; From 8d9f7a64d138a791fb8bbff1214195e4353a05f8 Mon Sep 17 00:00:00 2001 From: erkelost <1256029807@qq.com> Date: Tue, 26 Nov 2024 17:19:42 +0800 Subject: [PATCH 2/3] chore: update code --- examples/refactor-react/farm.config.ts | 12 ++++++------ .../normalize-persistent-cache.ts | 19 +++++-------------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/examples/refactor-react/farm.config.ts b/examples/refactor-react/farm.config.ts index 2fa0d7ded6..198a04f2d7 100644 --- a/examples/refactor-react/farm.config.ts +++ b/examples/refactor-react/farm.config.ts @@ -41,6 +41,11 @@ export default defineConfig({ // compilerPlugin(), custom() ], + server: { + port: 4855, + appType: "mpa", + https: true + }, compilation: { input: { index: path.resolve(__dirname, "index.html"), @@ -48,18 +53,13 @@ export default defineConfig({ 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 - }, } }); diff --git a/packages/core/src/config/normalize-config/normalize-persistent-cache.ts b/packages/core/src/config/normalize-config/normalize-persistent-cache.ts index 90c1c5c0ec..c6d71907da 100644 --- a/packages/core/src/config/normalize-config/normalize-persistent-cache.ts +++ b/packages/core/src/config/normalize-config/normalize-persistent-cache.ts @@ -9,6 +9,7 @@ import { isDisableCache } from '../env.js'; import { ResolvedUserConfig } from '../index.js'; const DEFAULT_CACHE_DIR = 'node_modules/.farm/cache'; +const DEFAULT_PACKAGE_JSON = 'package.json'; const defaultGlobalBuiltinCacheKeyStrategy = { define: true, buildDependencies: true, @@ -29,8 +30,6 @@ export async function normalizePersistentCache( return; } - // @ts-ignore - if (config.persistentCache === true || config.persistentCache == undefined) { config.persistentCache = { buildDependencies: [], @@ -41,19 +40,14 @@ export async function normalizePersistentCache( config.persistentCache.cacheDir = path.resolve( config.root, - //@ts-ignore 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 = { @@ -83,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)) { From 2881af21fbbab37eab7f69945c891fe3bd01093b Mon Sep 17 00:00:00 2001 From: erkelost <1256029807@qq.com> Date: Tue, 26 Nov 2024 17:22:20 +0800 Subject: [PATCH 3/3] chore: update type --- .../src/config/normalize-config/normalize-persistent-cache.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/src/config/normalize-config/normalize-persistent-cache.ts b/packages/core/src/config/normalize-config/normalize-persistent-cache.ts index c6d71907da..c218852355 100644 --- a/packages/core/src/config/normalize-config/normalize-persistent-cache.ts +++ b/packages/core/src/config/normalize-config/normalize-persistent-cache.ts @@ -3,14 +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 DEFAULT_CACHE_DIR = 'node_modules/.farm/cache'; const DEFAULT_PACKAGE_JSON = 'package.json'; -const defaultGlobalBuiltinCacheKeyStrategy = { +const defaultGlobalBuiltinCacheKeyStrategy: GlobalBuiltinCacheKeyStrategy = { define: true, buildDependencies: true, lockfile: true,