UNPKG

1.51 kBJavaScriptView Raw
1import postcss from "postcss"
2import color from "chalk"
3
4const msg = (name) => (
5 `Warning: postcss-cssnext found a duplicate plugin ('${ name }') ` +
6 "in your postcss plugins. " +
7 `This might be inefficient. You should remove '${ name }' from your ` +
8 "postcss plugin list since it's already included by postcss-cssnext."
9)
10
11let shouldGlobalWarn = true
12const globalWarning = (
13 "Note: If, for a really specific reason, postcss-cssnext warnings are " +
14 "irrelevant for your use case, and you really know what you are doing, " +
15 "you can disable this warnings by setting 'warnForDuplicates' option of " +
16 "postcss-cssnext to 'false'."
17)
18export const spotted = []
19
20const warnForDuplicates = postcss.plugin(
21 "postcss-cssnext-warn-for-duplicates",
22 ({ keys, console: messenger }) => {
23 return (style, result) => {
24 const pluginNames = []
25 result.processor.plugins.forEach((plugin) => {
26 const name = plugin.postcssPlugin
27 if (
28 pluginNames.indexOf(name) > -1 &&
29 // warn for cssnext plugins only
30 keys.indexOf(name) > -1 &&
31 // show warning once
32 spotted.indexOf(name) === -1
33 ) {
34 messenger.log(color.yellow.bold(msg(name)))
35 spotted.push(name)
36 }
37 else {
38 pluginNames.push(name)
39 }
40 })
41
42 if (spotted.length > 0 && shouldGlobalWarn) {
43 shouldGlobalWarn = false
44 messenger.log(globalWarning)
45 }
46 }
47 }
48)
49
50export default warnForDuplicates