-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathindex.ts
62 lines (51 loc) · 1.67 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import type { PluginCreator } from 'postcss';
import { transform } from './transform';
import { parse, parseCustomMedia } from '@csstools/media-query-list-parser';
import { stringify } from '@csstools/css-tokenizer';
/** postcss-media-minmax plugin options */
export type pluginOptions = never;
const creator: PluginCreator<pluginOptions> = () => {
return {
postcssPlugin: 'postcss-media-minmax',
AtRule: {
media(atRule): void {
if (!(atRule.params.includes('<') || atRule.params.includes('>') || atRule.params.includes('='))) {
return;
}
const mediaQueries = parse(atRule.params, {
preserveInvalidMediaQueries: true,
onParseError: () => {
throw atRule.error(`Unable to parse media query "${atRule.params}"`);
},
});
const transformed = transform(mediaQueries);
if (atRule.params === transformed) {
return;
}
atRule.params = transformed;
},
'custom-media'(atRule): void {
if (!(atRule.params.includes('<') || atRule.params.includes('>') || atRule.params.includes('='))) {
return;
}
const customMedia = parseCustomMedia(atRule.params, {
preserveInvalidMediaQueries: true,
onParseError: () => {
throw atRule.error(`Unable to parse media query "${atRule.params}"`);
},
});
if (!customMedia || !customMedia.mediaQueryList) {
return;
}
const original = customMedia.mediaQueryList.map((x) => x.toString()).join(',');
const transformed = transform(customMedia.mediaQueryList);
if (original === transformed) {
return;
}
atRule.params = `${stringify(...customMedia.name)} ${transformed}`;
},
},
};
};
creator.postcss = true;
export default creator;