From 3388ef15fbe965185a512df93d95ab10d295a03a Mon Sep 17 00:00:00 2001 From: Aleksandr Kitov Date: Wed, 31 Jul 2024 17:00:20 +0300 Subject: [PATCH] feat(overrides): pass additional arguments to postcss overrides --- .changeset/twenty-days-attack.md | 5 +++++ packages/arui-scripts/docs/overrides.md | 14 +++++++------ packages/arui-scripts/src/configs/postcss.ts | 1 + .../src/configs/util/apply-overrides.ts | 7 +++++++ .../example-modules/arui-scripts.overrides.ts | 21 +++++++------------ packages/example/arui-scripts.overrides.ts | 21 +++++++------------ 6 files changed, 37 insertions(+), 32 deletions(-) create mode 100644 .changeset/twenty-days-attack.md diff --git a/.changeset/twenty-days-attack.md b/.changeset/twenty-days-attack.md new file mode 100644 index 00000000..547b7608 --- /dev/null +++ b/.changeset/twenty-days-attack.md @@ -0,0 +1,5 @@ +--- +'arui-scripts': minor +--- + +Добавлена возможность более тонкой настройки postcss без необходимости полного переопределения плагинов через оверрайды diff --git a/packages/arui-scripts/docs/overrides.md b/packages/arui-scripts/docs/overrides.md index 6501a7cd..260253d7 100644 --- a/packages/arui-scripts/docs/overrides.md +++ b/packages/arui-scripts/docs/overrides.md @@ -53,14 +53,16 @@ export default overrides; - `postcss` - конфигурация для `postcss`. Ключи: `postcss`. > `config` postcss содержит массив с уже инициализированными плагинами, параметры которых уже зафиксированны. Если необходимо изменить параметры плагинов можно пересоздать конфиг, таким образом: ```javascript - import { - createPostcssConfig, // функция для создания конфигурационного файла postcss - postcssPlugins, // список плагинов - postcssPluginsOptions, // коллекция конфигураций плагинов - } from 'arui-scripts/build/configs/postcss.config'; module.exports = { - postcss: (config) => { + postcss: ( + config, + appConfiguration, + { + createPostcssConfig, // функция для создания конфигурационного файла postcss + postcssPlugins, // список плагинов + postcssPluginsOptions, // коллекция конфигураций плагинов + }) => { const { files } = postcssPluginsOptions['@csstools/postcss-global-data']; const newOption = { ...postcssPluginsOptions, diff --git a/packages/arui-scripts/src/configs/postcss.ts b/packages/arui-scripts/src/configs/postcss.ts index 122c706b..d0a49e95 100644 --- a/packages/arui-scripts/src/configs/postcss.ts +++ b/packages/arui-scripts/src/configs/postcss.ts @@ -4,6 +4,7 @@ import { createPostcssConfig, postcssPlugins, postcssPluginsOptions } from './po const postcssConfig = applyOverrides( 'postcss', createPostcssConfig(postcssPlugins, postcssPluginsOptions), + { createPostcssConfig, postcssPluginsOptions, postcssPlugins }, ); export default postcssConfig; diff --git a/packages/arui-scripts/src/configs/util/apply-overrides.ts b/packages/arui-scripts/src/configs/util/apply-overrides.ts index 524e4788..7de9001c 100644 --- a/packages/arui-scripts/src/configs/util/apply-overrides.ts +++ b/packages/arui-scripts/src/configs/util/apply-overrides.ts @@ -54,6 +54,12 @@ type ServerWebpackAdditionalArgs = { findPlugin: ReturnType>; }; +type PostCssAdditionalArgs = { + createPostcssConfig: (plugins: string[], options: Record) => string[] | unknown[]; + postcssPluginsOptions: Record; + postcssPlugins: string[]; +}; + /** * Дополнительные аргументы, которые будут переданы в функцию оверрайда */ @@ -67,6 +73,7 @@ type OverridesAdditionalArgs = { webpackServer: ServerWebpackAdditionalArgs; webpackServerDev: ServerWebpackAdditionalArgs; webpackServerProd: ServerWebpackAdditionalArgs; + postcss: PostCssAdditionalArgs; }; type OverrideFunction< diff --git a/packages/example-modules/arui-scripts.overrides.ts b/packages/example-modules/arui-scripts.overrides.ts index b4acd317..f7418694 100644 --- a/packages/example-modules/arui-scripts.overrides.ts +++ b/packages/example-modules/arui-scripts.overrides.ts @@ -49,20 +49,15 @@ const overrides: OverrideFile = { return config; }); }, - postcss: (config) => { - const overridesConfig = config - .map(name => { - if (name !== 'postcss-mixins') return name; + postcss: (config, appConfig, { createPostcssConfig, postcssPluginsOptions, postcssPlugins }) => { + const options = { + ...postcssPluginsOptions, + 'postcss-mixins': { + mixinsFiles: [path.join(process.cwd(), '../../node_modules/@alfalab/core-components/vars/typography.css')], + }, + }; - return [ - name, - { - mixinsFiles: [path.join(process.cwd(), '../../node_modules/@alfalab/core-components/vars/typography.css')] - } - ] - }) - - return overridesConfig; + return createPostcssConfig(postcssPlugins, options); } }; diff --git a/packages/example/arui-scripts.overrides.ts b/packages/example/arui-scripts.overrides.ts index e69fb5b9..c4be5b20 100644 --- a/packages/example/arui-scripts.overrides.ts +++ b/packages/example/arui-scripts.overrides.ts @@ -61,20 +61,15 @@ const overrides: OverrideFile = { return config; }); }, - postcss: (config) => { - const overridesConfig = config - .map(name => { - if (name !== 'postcss-mixins') return name; + postcss: (config, appConfig, { createPostcssConfig, postcssPluginsOptions, postcssPlugins }) => { + const options = { + ...postcssPluginsOptions, + 'postcss-mixins': { + mixinsFiles: [path.join(process.cwd(), '../../node_modules/@alfalab/core-components/vars/typography.css')], + }, + }; - return [ - name, - { - mixinsFiles: [path.join(process.cwd(), '../../node_modules/@alfalab/core-components/vars/typography.css')] - } - ] - }) - - return overridesConfig; + return createPostcssConfig(postcssPlugins, options); } };