UNPKG

1.59 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-warn-for-duplicates",
22 (options) => {
23 return (style, result) => {
24 // https://github.com/postcss/postcss/issues/768
25 const { keys, console: messenger } = options
26 const pluginNames = []
27 result.processor.plugins.forEach((plugin) => {
28 const name = plugin.postcssPlugin
29 if (
30 pluginNames.indexOf(name) > -1 &&
31 // warn for cssnext plugins only
32 keys.indexOf(name) > -1 &&
33 // show warning once
34 spotted.indexOf(name) === -1
35 ) {
36 messenger.log(color.yellow.bold(msg(name)))
37 spotted.push(name)
38 }
39 else {
40 pluginNames.push(name)
41 }
42 })
43
44 if (spotted.length > 0 && shouldGlobalWarn) {
45 shouldGlobalWarn = false
46 messenger.log(globalWarning)
47 }
48 }
49 }
50)
51
52export default warnForDuplicates