From 6eff863b3ca56b1f77fb16f5c46afccd802621bd Mon Sep 17 00:00:00 2001 From: Aleksandr Kitov Date: Wed, 24 Apr 2024 13:58:39 +0300 Subject: [PATCH] feat(webpack): invalidate caches on overrides and presets changes --- .changeset/perfect-numbers-call.md | 5 +++++ .../src/configs/app-configs/read-config-file.ts | 6 +++++- .../src/configs/app-configs/update-with-presets.ts | 10 +++++++--- .../configs/util/get-webpack-cache-dependencies.ts | 13 +++++++++++++ packages/arui-scripts/src/configs/webpack.client.ts | 2 ++ packages/arui-scripts/src/configs/webpack.server.ts | 2 ++ 6 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 .changeset/perfect-numbers-call.md create mode 100644 packages/arui-scripts/src/configs/util/get-webpack-cache-dependencies.ts diff --git a/.changeset/perfect-numbers-call.md b/.changeset/perfect-numbers-call.md new file mode 100644 index 00000000..d292ab97 --- /dev/null +++ b/.changeset/perfect-numbers-call.md @@ -0,0 +1,5 @@ +--- +'arui-scripts': minor +--- + +Улучшен механизм инвалидации кешей вебпака diff --git a/packages/arui-scripts/src/configs/app-configs/read-config-file.ts b/packages/arui-scripts/src/configs/app-configs/read-config-file.ts index 78eebc6f..2799a6a4 100644 --- a/packages/arui-scripts/src/configs/app-configs/read-config-file.ts +++ b/packages/arui-scripts/src/configs/app-configs/read-config-file.ts @@ -3,7 +3,7 @@ import path from 'path'; import { tryResolve } from '../util/resolve'; export function readConfigFile(cwd: string) { - const appConfigPath = tryResolve(path.join(cwd, '/arui-scripts.config')); + const appConfigPath = getConfigFilePath(cwd); if (appConfigPath) { // Мы не можем использовать импорты, нам нужен именно require, потому что мы не знаем заранее не только путь до файла, @@ -22,3 +22,7 @@ export function readConfigFile(cwd: string) { return null; } + +export function getConfigFilePath(cwd: string) { + return tryResolve(path.join(cwd, '/arui-scripts.config')); +} diff --git a/packages/arui-scripts/src/configs/app-configs/update-with-presets.ts b/packages/arui-scripts/src/configs/app-configs/update-with-presets.ts index 97158ca7..d90e28e0 100644 --- a/packages/arui-scripts/src/configs/app-configs/update-with-presets.ts +++ b/packages/arui-scripts/src/configs/app-configs/update-with-presets.ts @@ -11,9 +11,7 @@ export function updateWithPresets(config: AppConfigs, context: AppContext) { return config; } - const presetsConfigPath = tryResolve(`${config.presets}/arui-scripts.config`, { - paths: [context.cwd], - }); + const presetsConfigPath = getPresetsConfigPath(config, context.cwd); const presetsOverridesPath = tryResolve(`${config.presets}/arui-scripts.overrides`, { paths: [context.cwd], }); @@ -37,3 +35,9 @@ export function updateWithPresets(config: AppConfigs, context: AppContext) { return config; } + +export function getPresetsConfigPath(config: AppConfigs, cwd: string) { + return tryResolve(`${config.presets}/arui-scripts.config`, { + paths: [cwd], + }); +} diff --git a/packages/arui-scripts/src/configs/util/get-webpack-cache-dependencies.ts b/packages/arui-scripts/src/configs/util/get-webpack-cache-dependencies.ts new file mode 100644 index 00000000..6210c00b --- /dev/null +++ b/packages/arui-scripts/src/configs/util/get-webpack-cache-dependencies.ts @@ -0,0 +1,13 @@ +import configs from '../app-configs'; +import { getConfigFilePath } from '../app-configs/read-config-file'; +import { getPresetsConfigPath } from '../app-configs/update-with-presets'; + +export function getWebpackCacheDependencies(): Record { + return { + overrides: configs.overridesPath, + appConfigs: [ + getConfigFilePath(configs.cwd), + getPresetsConfigPath(configs, configs.cwd), + ].filter(Boolean) as string[], + } +} diff --git a/packages/arui-scripts/src/configs/webpack.client.ts b/packages/arui-scripts/src/configs/webpack.client.ts index 13d873e0..d25b5b72 100644 --- a/packages/arui-scripts/src/configs/webpack.client.ts +++ b/packages/arui-scripts/src/configs/webpack.client.ts @@ -28,6 +28,7 @@ import { patchMainWebpackConfigForModules, patchWebpackConfigForCompat } from '. import postcssConf from './postcss'; import { processAssetsPluginOutput } from './process-assets-plugin-output'; import { AruiRuntimePlugin, getInsertCssRuntimeMethod } from '../plugins/arui-runtime'; +import { getWebpackCacheDependencies } from './util/get-webpack-cache-dependencies'; const PnpWebpackPlugin = require('pnp-webpack-plugin'); const CssMinimizerPlugin = require('css-minimizer-webpack-plugin'); @@ -239,6 +240,7 @@ export const createSingleClientWebpackConfig = ( type: 'filesystem', buildDependencies: { config: [__filename], + ...getWebpackCacheDependencies(), }, } : false, diff --git a/packages/arui-scripts/src/configs/webpack.server.ts b/packages/arui-scripts/src/configs/webpack.server.ts index 4afca7ed..1e7fca9e 100644 --- a/packages/arui-scripts/src/configs/webpack.server.ts +++ b/packages/arui-scripts/src/configs/webpack.server.ts @@ -16,6 +16,7 @@ import { babelDependencies } from './babel-dependencies'; import babelConf from './babel-server'; import postcssConf from './postcss'; import { serverExternalsExemptions } from './server-externals-exemptions'; +import { getWebpackCacheDependencies } from './util/get-webpack-cache-dependencies'; const ReloadServerPlugin = require('../plugins/reload-server-webpack-plugin'); const WatchMissingNodeModulesPlugin = require('../plugins/watch-missing-node-modules-plugin'); @@ -71,6 +72,7 @@ export const createServerConfig = (mode: 'dev' | 'prod'): Configuration => ({ name: 'server', buildDependencies: { config: [__filename], + ...getWebpackCacheDependencies(), }, } : false,