forked from postcss/postcss-mixins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
199 lines (175 loc) · 5.09 KB
/
index.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
let { join, basename, extname, relative } = require('path')
let { promisify } = require('util')
let { platform } = require('os')
let { parse } = require('postcss-js')
let sugarss = require('sugarss')
let globby = require('globby')
let vars = require('postcss-simple-vars')
let fs = require('fs')
let readFile = promisify(fs.readFile)
let IS_WIN = platform().includes('win32')
function addMixin (helpers, mixins, rule, file) {
let name = rule.params.split(/\s/, 1)[0]
let other = rule.params.slice(name.length).trim()
let args = []
if (other.length) {
args = helpers.list.comma(other).map(str => {
let arg = str.split(':', 1)[0]
let defaults = str.slice(arg.length + 1)
return [arg.slice(1).trim(), defaults.trim()]
})
}
let content = false
rule.walkAtRules('mixin-content', () => {
content = true
return false
})
mixins[name] = { mixin: rule, args, content }
if (file) mixins[name].file = file
rule.remove()
}
async function loadGlobalMixin (helpers, globs) {
let cwd = process.cwd()
let files = await globby(globs, { caseSensitiveMatch: IS_WIN })
let mixins = {}
await Promise.all(
files.map(async i => {
let ext = extname(i).toLowerCase()
let name = basename(i, extname(i))
let path = join(cwd, relative(cwd, i))
if (ext === '.css' || ext === '.pcss' || ext === '.sss') {
let content = await readFile(path)
let root
if (ext === '.sss') {
root = sugarss.parse(content, { from: path })
} else {
root = helpers.parse(content, { from: path })
}
root.walkAtRules('define-mixin', atrule => {
addMixin(helpers, mixins, atrule, path)
})
} else {
mixins[name] = { mixin: require(path), file: path }
}
})
)
return mixins
}
function addGlobalMixins (helpers, local, global, parent) {
for (let name in global) {
helpers.result.messages.push({
type: 'dependency',
file: global[name].file,
parent: parent || ''
})
local[name] = global[name]
}
}
function processMixinContent (rule, from) {
rule.walkAtRules('mixin-content', content => {
if (from.nodes && from.nodes.length > 0) {
content.replaceWith(from.clone().nodes)
} else {
content.remove()
}
})
}
function insertObject (rule, obj) {
let root = parse(obj)
root.each(node => {
node.source = rule.source
})
processMixinContent(root, rule)
rule.parent.insertBefore(rule, root)
}
function insertMixin (helpers, mixins, rule, opts) {
let name = rule.params.split(/\s/, 1)[0]
let rest = rule.params.slice(name.length).trim()
let params
if (rest.trim() === '') {
params = []
} else {
params = helpers.list.comma(rest)
}
let meta = mixins[name]
let mixin = meta && meta.mixin
if (!meta) {
if (!opts.silent) {
throw rule.error('Undefined mixin ' + name)
}
} else if (mixin.name === 'define-mixin') {
let i
let values = {}
for (i = 0; i < meta.args.length; i++) {
values[meta.args[i][0]] = params[i] || meta.args[i][1]
}
let proxy = new helpers.Root()
for (i = 0; i < mixin.nodes.length; i++) {
let node = mixin.nodes[i].clone()
delete node.raws.before
proxy.append(node)
}
if (meta.args.length) {
proxy = helpers.postcss([vars({ only: values })]).process(proxy).root
}
if (meta.content) processMixinContent(proxy, rule)
rule.parent.insertBefore(rule, proxy)
} else if (typeof mixin === 'object') {
insertObject(rule, mixin)
} else if (typeof mixin === 'function') {
let args = [rule].concat(params)
rule.walkAtRules(atRule => {
insertMixin(helpers, mixins, atRule, opts)
})
let nodes = mixin(...args)
if (typeof nodes === 'object') {
insertObject(rule, nodes)
}
} else {
throw new Error('Wrong ' + name + ' mixin type ' + typeof mixin)
}
if (rule.parent) rule.remove()
}
module.exports = (opts = {}) => {
let loadFrom = []
if (opts.mixinsDir) {
if (!Array.isArray(opts.mixinsDir)) {
opts.mixinsDir = [opts.mixinsDir]
}
loadFrom = opts.mixinsDir.map(dir => join(dir, '*.{js,json,css,sss,pcss}'))
}
if (opts.mixinsFiles) loadFrom = loadFrom.concat(opts.mixinsFiles)
return {
postcssPlugin: 'postcss-mixins',
prepare () {
let mixins = {}
if (typeof opts.mixins === 'object') {
for (let i in opts.mixins) {
mixins[i] = { mixin: opts.mixins[i] }
}
}
return {
Once (root, helpers) {
if (loadFrom.length > 0) {
return loadGlobalMixin(helpers, loadFrom).then(global => {
addGlobalMixins(helpers, mixins, global, opts.parent)
})
}
},
AtRule: {
'define-mixin': (node, helpers) => {
addMixin(helpers, mixins, node)
node.remove()
},
'mixin': (node, helpers) => {
insertMixin(helpers, mixins, node, opts)
},
'add-mixin': (node, helpers) => {
insertMixin(helpers, mixins, node, opts)
}
}
}
}
}
}
module.exports.postcss = true