|
| 1 | +import _ from 'lodash' |
| 2 | +import applyClassPrefix from '../util/applyClassPrefix' |
| 3 | +import container from '../generators/container' |
| 4 | +import fs from 'fs' |
| 5 | +import generateModules from '../util/generateModules' |
| 6 | +import postcss from 'postcss' |
| 7 | +import utilityModules from '../utilityModules' |
| 8 | +import wrapWithVariants from '../util/wrapWithVariants' |
| 9 | + |
| 10 | +function defineSelector(selector, properties) { |
| 11 | + const decls = _.map(properties, (value, property) => { |
| 12 | + return postcss.decl({ |
| 13 | + prop: `${property}`, |
| 14 | + value: `${value}`, |
| 15 | + }) |
| 16 | + }) |
| 17 | + |
| 18 | + return postcss.rule({ selector }).append(decls) |
| 19 | +} |
| 20 | + |
| 21 | +export default function(config) { |
| 22 | + return function(css) { |
| 23 | + const unwrappedConfig = config() |
| 24 | + |
| 25 | + const pluginComponents = [] |
| 26 | + const pluginUtilities = [] |
| 27 | + |
| 28 | + unwrappedConfig.plugins.forEach(plugin => { |
| 29 | + plugin({ |
| 30 | + selector: defineSelector, |
| 31 | + addUtilities: (utilities, variants) => { |
| 32 | + pluginUtilities.push(wrapWithVariants(utilities, variants)) |
| 33 | + }, |
| 34 | + addComponents: components => { |
| 35 | + pluginComponents.push(...components) |
| 36 | + }, |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + css.walkAtRules('tailwind', atRule => { |
| 41 | + if (atRule.params === 'preflight') { |
| 42 | + atRule.before( |
| 43 | + postcss.parse(fs.readFileSync(`${__dirname}/../../css/preflight.css`, 'utf8')) |
| 44 | + ) |
| 45 | + atRule.remove() |
| 46 | + } |
| 47 | + |
| 48 | + if (atRule.params === 'components') { |
| 49 | + const tailwindComponentTree = postcss.root({ |
| 50 | + nodes: [...container(unwrappedConfig)], |
| 51 | + }) |
| 52 | + |
| 53 | + const pluginComponentTree = postcss.root({ |
| 54 | + nodes: [...pluginComponents], |
| 55 | + }) |
| 56 | + |
| 57 | + applyClassPrefix(tailwindComponentTree, unwrappedConfig.options.prefix) |
| 58 | + |
| 59 | + tailwindComponentTree.walk(node => (node.source = atRule.source)) |
| 60 | + pluginComponentTree.walk(node => (node.source = atRule.source)) |
| 61 | + |
| 62 | + atRule.before(tailwindComponentTree) |
| 63 | + atRule.before(pluginComponentTree) |
| 64 | + atRule.remove() |
| 65 | + } |
| 66 | + |
| 67 | + if (atRule.params === 'utilities') { |
| 68 | + const utilities = generateModules(utilityModules, unwrappedConfig.modules, unwrappedConfig) |
| 69 | + |
| 70 | + if (unwrappedConfig.options.important) { |
| 71 | + utilities.walkDecls(decl => (decl.important = true)) |
| 72 | + } |
| 73 | + |
| 74 | + const tailwindUtilityTree = postcss.root({ |
| 75 | + nodes: [...utilities.nodes], |
| 76 | + }) |
| 77 | + |
| 78 | + const pluginUtilityTree = postcss.root({ |
| 79 | + nodes: [...pluginUtilities], |
| 80 | + }) |
| 81 | + |
| 82 | + applyClassPrefix(tailwindUtilityTree, unwrappedConfig.options.prefix) |
| 83 | + |
| 84 | + tailwindUtilityTree.walk(node => (node.source = atRule.source)) |
| 85 | + pluginUtilityTree.walk(node => (node.source = atRule.source)) |
| 86 | + |
| 87 | + atRule.before(tailwindUtilityTree) |
| 88 | + atRule.before(pluginUtilityTree) |
| 89 | + atRule.remove() |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
0 commit comments