From b0eb09d5095353f3d5e086b8a0435c653cbcd67a Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Tue, 8 Oct 2024 14:32:21 +0800 Subject: [PATCH] fix: add browsers list config to cache key (#6976) --- .changeset/gorgeous-lemons-drop.md | 6 ++++++ packages/shared-config/src/index.ts | 3 ++- packages/shared-config/src/unPlugins/compilation.ts | 4 ++-- packages/webpack-config/src/index.ts | 7 +++++-- 4 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 .changeset/gorgeous-lemons-drop.md diff --git a/.changeset/gorgeous-lemons-drop.md b/.changeset/gorgeous-lemons-drop.md new file mode 100644 index 0000000000..00a2a6d093 --- /dev/null +++ b/.changeset/gorgeous-lemons-drop.md @@ -0,0 +1,6 @@ +--- +'@ice/webpack-config': patch +'@ice/shared-config': patch +--- + +fix: mark browserslist config to cache key diff --git a/packages/shared-config/src/index.ts b/packages/shared-config/src/index.ts index ed9d952343..e65d74db77 100644 --- a/packages/shared-config/src/index.ts +++ b/packages/shared-config/src/index.ts @@ -1,4 +1,4 @@ -import compilationPlugin, { isSupportedFeature, getJsxTransformOptions } from './unPlugins/compilation.js'; +import compilationPlugin, { isSupportedFeature, getJsxTransformOptions, getSupportedBrowsers } from './unPlugins/compilation.js'; import compileExcludes, { SKIP_COMPILE as skipCompilePackages } from './compileExcludes.js'; import getCompilerPlugins from './getCompilerPlugins.js'; import getDefineVars from './getDefineVars.js'; @@ -19,4 +19,5 @@ export { getPostcssOpts, getAliasWithRoot, getDevtoolValue, + getSupportedBrowsers, }; diff --git a/packages/shared-config/src/unPlugins/compilation.ts b/packages/shared-config/src/unPlugins/compilation.ts index f847784228..8103ba978e 100644 --- a/packages/shared-config/src/unPlugins/compilation.ts +++ b/packages/shared-config/src/unPlugins/compilation.ts @@ -262,11 +262,11 @@ export function getJsxTransformOptions({ return commonOptions; } -function getSupportedBrowsers( +export function getSupportedBrowsers( dir: string, isDevelopment: boolean, ): string[] | undefined { - let browsers: any; + let browsers: string[]; try { browsers = browserslist.loadConfig({ path: dir, diff --git a/packages/webpack-config/src/index.ts b/packages/webpack-config/src/index.ts index 3f0ab39271..ffb7b4e236 100644 --- a/packages/webpack-config/src/index.ts +++ b/packages/webpack-config/src/index.ts @@ -1,5 +1,6 @@ import * as path from 'path'; import { createRequire } from 'module'; +import crypto from 'crypto'; import fg from 'fast-glob'; import ReactRefreshWebpackPlugin from '@ice/bundles/compiled/@pmmmwh/react-refresh-webpack-plugin/lib/index.js'; import bundleAnalyzer from '@ice/bundles/compiled/webpack-bundle-analyzer/index.js'; @@ -11,7 +12,7 @@ import ESlintPlugin from '@ice/bundles/compiled/eslint-webpack-plugin/index.js'; import CopyPlugin from '@ice/bundles/compiled/copy-webpack-plugin/index.js'; import type { NormalModule, Compiler, Configuration } from 'webpack'; import type webpack from 'webpack'; -import { compilationPlugin, compileExcludes, getCompilerPlugins, getDefineVars, getAliasWithRoot, getDevtoolValue } from '@ice/shared-config'; +import { compilationPlugin, compileExcludes, getCompilerPlugins, getDefineVars, getAliasWithRoot, getDevtoolValue, getSupportedBrowsers } from '@ice/shared-config'; import type { Config, ModifyWebpackConfig } from '@ice/shared-config/types.js'; import configAssets from './config/assets.js'; import configCss from './config/css.js'; @@ -152,6 +153,8 @@ export function getWebpackConfig(options: GetWebpackConfigOptions): Configuratio module: true, }, minimizerOptions); + const supportBrowsers = getSupportedBrowsers(rootDir, dev); + const browsersMD5 = supportBrowsers ? crypto.createHash('md5').update(supportBrowsers.join('')).digest('hex') : ''; const compilation = compilationPlugin({ rootDir, cacheDir, @@ -257,7 +260,7 @@ export function getWebpackConfig(options: GetWebpackConfigOptions): Configuratio } as Configuration['optimization'], cache: enableCache ? { type: 'filesystem', - version: `${process.env.__ICE_VERSION__}|${userConfigHash}`, + version: `${process.env.__ICE_VERSION__}|${userConfigHash}|${browsersMD5}`, buildDependencies: { config: [path.join(rootDir, 'package.json')] }, cacheDirectory: path.join(cacheDir, 'webpack'), } : false,