Skip to content

Commit deea02c

Browse files
Обмочевский Владислав ВячеславовичОбмочевский Владислав Вячеславович
authored andcommitted
feat(*): remove postcss-global-data and add custom postcss-global-environments postcss plugin
1 parent 7545311 commit deea02c

11 files changed

Lines changed: 27 additions & 93 deletions

File tree

packages/arui-scripts/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"@babel/preset-react": "^7.23.3",
3737
"@babel/preset-typescript": "^7.23.3",
3838
"@babel/runtime": "^7.23.8",
39-
"@csstools/postcss-global-data": "^2.0.1",
4039
"@pmmmwh/react-refresh-webpack-plugin": "0.5.11",
4140
"@swc/core": "^1.7.35",
4241
"@swc/jest": "^0.2.36",

packages/arui-scripts/src/configs/postcss.config.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
import path from 'path';
22

3+
import type { Plugin, PluginCreator, Processor } from 'postcss';
4+
5+
import { postCssGlobalEnvironments } from '../plugins/postcss-global-environments/postcss-global-environments';
6+
37
import config from './app-configs';
48
import supportingBrowsers from './supporting-browsers';
9+
10+
type PostCssPluginName = string | PluginCreator<any>;
11+
type PostcssPlugin = string | [string, unknown] | (() => Plugin | Processor);
12+
513
/**
614
* Функция для создания конфигурационного файла postcss
7-
* @param {String[]} plugins список плагинов
15+
* @param {PostCssPluginName[]} plugins список плагинов
816
* @param {Object} options коллекция конфигураций плагинов, где ключ - название плагина, а значение - аргумент для инициализации
917
* @returns {*}
1018
*/
1119
export function createPostcssConfig(
12-
plugins: string[],
20+
plugins: PostCssPluginName[],
1321
options: Record<string, unknown>,
14-
): string[] | unknown[] {
22+
): PostcssPlugin[] {
1523
return plugins.map((pluginName) => {
16-
if (pluginName in options) {
17-
return [pluginName, options[pluginName]];
24+
if (typeof pluginName === 'string') {
25+
return pluginName in options
26+
? [pluginName, options[pluginName]]
27+
: pluginName;
1828
}
1929

20-
return pluginName;
30+
return () => pluginName(options[pluginName.name]);
2131
});
2232
}
2333

@@ -28,7 +38,7 @@ export const postcssPlugins = [
2838
'postcss-mixins',
2939
'postcss-for',
3040
'postcss-each',
31-
'@csstools/postcss-global-data',
41+
postCssGlobalEnvironments,
3242
'postcss-custom-media',
3343
'postcss-color-mod-function',
3444
!config.keepCssVars && 'postcss-custom-properties',
@@ -40,13 +50,13 @@ export const postcssPlugins = [
4050
'autoprefixer',
4151
'postcss-inherit',
4252
'postcss-discard-comments',
43-
].filter(Boolean) as string[];
53+
].filter(Boolean) as PostCssPluginName[];
4454

4555
export const postcssPluginsOptions = {
4656
'postcss-import': {
4757
path: ['./src'],
4858
},
49-
'@csstools/postcss-global-data': {
59+
'@alfalab/postcss-global-environments': {
5060
files: [path.join(__dirname, 'mq.css'), config.componentsTheme].filter(Boolean) as string[],
5161
},
5262
'postcss-url': {
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { replacePlugin } from '../plugins/insert-plugin';
2-
import { postCssGlobalData } from '../plugins/postcss-global-data/postcss-global-data';
3-
41
import applyOverrides from './util/apply-overrides';
52
import { createPostcssConfig, postcssPlugins, postcssPluginsOptions } from './postcss.config';
63

@@ -9,7 +6,4 @@ const postcssConfig = applyOverrides(
96
createPostcssConfig(postcssPlugins, postcssPluginsOptions),
107
);
118

12-
// заменяем postCssGlobalData на кастомный
13-
const modifiedPostcssConfig = replacePlugin(postcssConfig, '@csstools/postcss-global-data', postCssGlobalData)
14-
15-
export default modifiedPostcssConfig;
9+
export default postcssConfig;

packages/arui-scripts/src/plugins/__tests__/insert-plugin.test.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

packages/arui-scripts/src/plugins/insert-plugin.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

packages/arui-scripts/src/plugins/postcss-global-data/postcss-global-data.ts renamed to packages/arui-scripts/src/plugins/postcss-global-environments/postcss-global-environments.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type PluginOptions = {
66
files?: string[];
77
};
88

9-
const postCssGlobalData: PluginCreator<PluginOptions> = (opts?: PluginOptions) => {
9+
const postCssGlobalEnvironments: PluginCreator<PluginOptions> = (opts?: PluginOptions) => {
1010
const options = {
1111
files: [],
1212
...opts,
@@ -18,13 +18,14 @@ const postCssGlobalData: PluginCreator<PluginOptions> = (opts?: PluginOptions) =
1818
let rulesSelectors = new Set<Rule>();
1919

2020
return {
21-
postcssPlugin: '@alfalab/postcss-global-data',
21+
postcssPlugin: '@alfalab/postcss-global-environments',
2222
prepare(): Plugin {
2323
return {
24-
postcssPlugin: '@alfalab/postcss-global-data',
24+
postcssPlugin: '@alfalab/postcss-global-environments',
2525
Once(root, postcssHelpers): void {
2626
if (!Object.keys(parsedVariables).length) {
2727
options.files.forEach((filePath) => {
28+
console.log('parse file: ', filePath);
2829
const importedCss = parseImport(root, postcssHelpers, filePath);
2930

3031
parseVariables(importedCss, parsedVariables);
@@ -47,6 +48,6 @@ const postCssGlobalData: PluginCreator<PluginOptions> = (opts?: PluginOptions) =
4748
};
4849
};
4950

50-
postCssGlobalData.postcss = true;
51+
postCssGlobalEnvironments.postcss = true;
5152

52-
export { postCssGlobalData };
53+
export { postCssGlobalEnvironments };

packages/arui-scripts/src/plugins/postcss-global-data/utils/__tests__/add-global-variable.tests.ts renamed to packages/arui-scripts/src/plugins/postcss-global-environments/utils/__tests__/add-global-variable.tests.ts

File renamed without changes.

packages/arui-scripts/src/plugins/postcss-global-data/utils/__tests__/get-media-query-name.tests.ts renamed to packages/arui-scripts/src/plugins/postcss-global-environments/utils/__tests__/get-media-query-name.tests.ts

File renamed without changes.

packages/arui-scripts/src/plugins/postcss-global-data/utils/__tests__/parse-variables.tests.ts renamed to packages/arui-scripts/src/plugins/postcss-global-environments/utils/__tests__/parse-variables.tests.ts

File renamed without changes.

packages/arui-scripts/src/plugins/postcss-global-data/utils/utils.ts renamed to packages/arui-scripts/src/plugins/postcss-global-environments/utils/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function parseImport(root: Root, postcssHelpers: Helpers, filePath: strin
1616

1717
postcssHelpers.result.messages.push({
1818
type: 'dependency',
19-
plugin: 'postcss-global-data',
19+
plugin: 'postcss-global-environments',
2020
file: resolvedPath,
2121
parent: root.source?.input?.file,
2222
});

0 commit comments

Comments
 (0)