index.js
11.2 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
var postcss = require('postcss');
var Tokenizer = require('css-selector-tokenizer');
function normalizeNodeArray(nodes) {
var array = [];
nodes.forEach(function(x) {
if(Array.isArray(x)) {
normalizeNodeArray(x).forEach(function(item) {
array.push(item);
});
} else if(x) {
array.push(x);
}
});
if(array.length > 0 && array[array.length - 1].type === 'spacing') {
array.pop();
}
return array;
}
function localizeNode(node, context) {
if(context.ignoreNextSpacing && node.type !== 'spacing') {
throw new Error('Missing whitespace after :' + context.ignoreNextSpacing);
}
if(context.enforceNoSpacing && node.type === 'spacing') {
throw new Error('Missing whitespace before :' + context.enforceNoSpacing);
}
var newNodes;
switch(node.type) {
case 'selectors':
var resultingGlobal;
context.hasPureGlobals = false;
newNodes = node.nodes.map(function(n) {
var nContext = {
global: context.global,
lastWasSpacing: true,
hasLocals: false,
explicit: false
};
n = localizeNode(n, nContext);
if(typeof resultingGlobal === 'undefined') {
resultingGlobal = nContext.global;
} else if(resultingGlobal !== nContext.global) {
throw new Error('Inconsistent rule global/local result in rule "' +
Tokenizer.stringify(node) + '" (multiple selectors must result in the same mode for the rule)');
}
if(!nContext.hasLocals) {
context.hasPureGlobals = true;
}
return n;
});
context.global = resultingGlobal;
node = Object.create(node);
node.nodes = normalizeNodeArray(newNodes);
break;
case 'selector':
newNodes = node.nodes.map(function(n) {
return localizeNode(n, context);
});
node = Object.create(node);
node.nodes = normalizeNodeArray(newNodes);
break;
case 'spacing':
if(context.ignoreNextSpacing) {
context.ignoreNextSpacing = false;
context.lastWasSpacing = false;
context.enforceNoSpacing = false;
return null;
}
context.lastWasSpacing = true;
return node;
case 'pseudo-class':
if(node.name === 'local' || node.name === 'global') {
if(context.inside) {
throw new Error('A :' + node.name + ' is not allowed inside of a :' + context.inside + '(...)');
}
context.ignoreNextSpacing = context.lastWasSpacing ? node.name : false;
context.enforceNoSpacing = context.lastWasSpacing ? false : node.name;
context.global = (node.name === 'global');
context.explicit = true;
return null;
}
break;
case 'nested-pseudo-class':
var subContext;
if(node.name === 'local' || node.name === 'global') {
if(context.inside) {
throw new Error('A :' + node.name + '(...) is not allowed inside of a :' + context.inside + '(...)');
}
subContext = {
global: (node.name === 'global'),
inside: node.name,
hasLocals: false,
explicit: true
};
node = node.nodes.map(function(n) {
return localizeNode(n, subContext);
});
// don't leak spacing
node[0].before = undefined;
node[node.length - 1].after = undefined;
} else {
subContext = {
global: context.global,
inside: context.inside,
lastWasSpacing: true,
hasLocals: false,
explicit: context.explicit
};
newNodes = node.nodes.map(function(n) {
return localizeNode(n, subContext);
});
node = Object.create(node);
node.nodes = normalizeNodeArray(newNodes);
}
if(subContext.hasLocals) {
context.hasLocals = true;
}
break;
case 'id':
case 'class':
if(!context.global) {
node = {
type: 'nested-pseudo-class',
name: 'local',
nodes: [node]
};
context.hasLocals = true;
}
break;
}
// reset context
context.lastWasSpacing = false;
context.ignoreNextSpacing = false;
context.enforceNoSpacing = false;
return node;
}
function localizeDeclNode(node, context) {
var newNode;
switch(node.type) {
case 'item':
if(context.localizeNextItem) {
newNode = Object.create(node);
newNode.name = ':local(' + newNode.name + ')';
context.localizeNextItem = false;
return newNode;
}
break;
case 'nested-item':
var newNodes = node.nodes.map(function(n) {
return localizeDeclValue(n, context);
});
node = Object.create(node);
node.nodes = newNodes;
break;
case 'url':
if(context.options && context.options.rewriteUrl) {
newNode = Object.create(node);
newNode.url = context.options.rewriteUrl(context.global, node.url);
return newNode;
}
break;
}
return node;
}
function localizeDeclValue(valueNode, context) {
var newValueNode = Object.create(valueNode);
newValueNode.nodes = valueNode.nodes.map(function(node) {
return localizeDeclNode(node, context);
});
return newValueNode;
}
function localizeAnimationShorthandDeclValueNodes(nodes, context) {
var validIdent = validIdent = /^-?[_a-z][_a-z0-9-]*$/i;
/*
The spec defines some keywords that you can use to describe properties such as the timing
function. These are still valid animation names, so as long as there is a property that accepts
a keyword, it is given priority. Only when all the properties that can take a keyword are
exhausted can the animation name be set to the keyword. I.e.
animation: infinite infinite;
The animation will repeat an infinite number of times from the first argument, and will have an
animation name of infinite from the second.
*/
var animationKeywords = {
'$alternate': 1,
'$alternate-reverse': 1,
'$backwards': 1,
'$both': 1,
'$ease': 1,
'$ease-in': 1,
'$ease-in-out': 1,
'$ease-out': 1,
'$forwards': 1,
'$infinite': 1,
'$linear': 1,
'$none': Infinity, // No matter how many times you write none, it will never be an animation name
'$normal': 1,
'$paused': 1,
'$reverse': 1,
'$running': 1,
'$step-end': 1,
'$step-start': 1,
'$initial': Infinity,
'$inherit': Infinity,
'$unset': Infinity,
};
var didParseAnimationName = false;
var parsedAnimationKeywords = {};
return nodes.map(function(valueNode) {
var value = valueNode.type === 'item'
? valueNode.name.toLowerCase()
: null;
var shouldParseAnimationName = false;
if (!didParseAnimationName && value && validIdent.test(value)) {
if ('$' + value in animationKeywords) {
parsedAnimationKeywords['$' + value] = ('$' + value in parsedAnimationKeywords)
? (parsedAnimationKeywords['$' + value] + 1)
: 0;
shouldParseAnimationName = (parsedAnimationKeywords['$' + value] >= animationKeywords['$' + value]);
} else {
shouldParseAnimationName = true;
}
}
var subContext = {
options: context.options,
global: context.global,
localizeNextItem: shouldParseAnimationName && !context.global
};
return localizeDeclNode(valueNode, subContext);
});
}
function localizeAnimationShorthandDeclValues(valuesNode, decl, context) {
var newValuesNode = Object.create(valuesNode);
newValuesNode.nodes = valuesNode.nodes.map(function(valueNode, index) {
var newValueNode = Object.create(valueNode);
newValueNode.nodes = localizeAnimationShorthandDeclValueNodes(valueNode.nodes, context);
return newValueNode;
});
decl.value = Tokenizer.stringifyValues(newValuesNode);
}
function localizeDeclValues(localize, valuesNode, decl, context) {
var newValuesNode = Object.create(valuesNode);
newValuesNode.nodes = valuesNode.nodes.map(function(valueNode) {
var subContext = {
options: context.options,
global: context.global,
localizeNextItem: localize && !context.global
};
return localizeDeclValue(valueNode, subContext);
});
decl.value = Tokenizer.stringifyValues(newValuesNode);
}
function localizeDecl(decl, context) {
var valuesNode = Tokenizer.parseValues(decl.value);
var isAnimation = /animation?$/.test(decl.prop);
if (isAnimation) return localizeAnimationShorthandDeclValues(valuesNode, decl, context);
var isAnimationName = /animation(-name)?$/.test(decl.prop);
if (isAnimationName) return localizeDeclValues(true, valuesNode, decl, context);
return localizeDeclValues(false, valuesNode, decl, context);
}
module.exports = postcss.plugin('postcss-modules-local-by-default', function (options) {
if (typeof options !== 'object') {
options = {}; // If options is undefined or not an object the plugin fails
}
if(options && options.mode) {
if(options.mode !== 'global' && options.mode !== 'local' && options.mode !== 'pure') {
throw new Error('options.mode must be either "global", "local" or "pure" (default "local")');
}
}
var pureMode = options && options.mode === 'pure';
var globalMode = options && options.mode === 'global';
return function(css) {
css.walkAtRules(function(atrule) {
if(/keyframes$/.test(atrule.name)) {
var globalMatch = /^\s*:global\s*\((.+)\)\s*$/.exec(atrule.params);
var localMatch = /^\s*:local\s*\((.+)\)\s*$/.exec(atrule.params);
var globalKeyframes = globalMode;
if(globalMatch) {
if(pureMode) {
throw atrule.error('@keyframes :global(...) is not allowed in pure mode');
}
atrule.params = globalMatch[1];
globalKeyframes = true;
} else if(localMatch) {
atrule.params = localMatch[0];
globalKeyframes = false;
} else if(!globalMode) {
atrule.params = ':local(' + atrule.params + ')';
}
atrule.walkDecls(function(decl) {
localizeDecl(decl, {
options: options,
global: globalKeyframes
});
});
} else if(atrule.nodes) {
atrule.nodes.forEach(function(decl) {
if(decl.type === 'decl') {
localizeDecl(decl, {
options: options,
global: globalMode
});
}
});
}
});
css.walkRules(function(rule) {
if(rule.parent.type === 'atrule' && /keyframes$/.test(rule.parent.name)) {
// ignore keyframe rules
return;
}
var selector = Tokenizer.parse(rule.selector);
var context = {
options: options,
global: globalMode,
hasPureGlobals: false
};
var newSelector;
try {
newSelector = localizeNode(selector, context);
} catch(e) {
throw rule.error(e.message);
}
if(pureMode && context.hasPureGlobals) {
throw rule.error('Selector "' + Tokenizer.stringify(selector) + '" is not pure ' +
'(pure selectors must contain at least one local class or id)');
}
// Less-syntax mixins parse as rules with no nodes
if (rule.nodes) {
rule.nodes.forEach(function(decl) {
localizeDecl(decl, context);
});
}
rule.selector = Tokenizer.stringify(newSelector);
});
};
});