rebase.js
2.63 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
var extractImportUrlAndMedia = require('./extract-import-url-and-media');
var restoreImport = require('./restore-import');
var rewriteUrl = require('./rewrite-url');
var Token = require('../tokenizer/token');
var isImport = require('../utils/is-import');
var SOURCE_MAP_COMMENT_PATTERN = /^\/\*# sourceMappingURL=(\S+) \*\/$/;
function rebase(tokens, rebaseAll, validator, rebaseConfig) {
return rebaseAll ?
rebaseEverything(tokens, validator, rebaseConfig) :
rebaseAtRules(tokens, validator, rebaseConfig);
}
function rebaseEverything(tokens, validator, rebaseConfig) {
var token;
var i, l;
for (i = 0, l = tokens.length; i < l; i++) {
token = tokens[i];
switch (token[0]) {
case Token.AT_RULE:
rebaseAtRule(token, validator, rebaseConfig);
break;
case Token.AT_RULE_BLOCK:
rebaseProperties(token[2], validator, rebaseConfig);
break;
case Token.COMMENT:
rebaseSourceMapComment(token, rebaseConfig);
break;
case Token.NESTED_BLOCK:
rebaseEverything(token[2], validator, rebaseConfig);
break;
case Token.RULE:
rebaseProperties(token[2], validator, rebaseConfig);
break;
}
}
return tokens;
}
function rebaseAtRules(tokens, validator, rebaseConfig) {
var token;
var i, l;
for (i = 0, l = tokens.length; i < l; i++) {
token = tokens[i];
switch (token[0]) {
case Token.AT_RULE:
rebaseAtRule(token, validator, rebaseConfig);
break;
}
}
return tokens;
}
function rebaseAtRule(token, validator, rebaseConfig) {
if (!isImport(token[1])) {
return;
}
var uriAndMediaQuery = extractImportUrlAndMedia(token[1]);
var newUrl = rewriteUrl(uriAndMediaQuery[0], rebaseConfig);
var mediaQuery = uriAndMediaQuery[1];
token[1] = restoreImport(newUrl, mediaQuery);
}
function rebaseSourceMapComment(token, rebaseConfig) {
var matches = SOURCE_MAP_COMMENT_PATTERN.exec(token[1]);
if (matches && matches[1].indexOf('data:') === -1) {
token[1] = token[1].replace(matches[1], rewriteUrl(matches[1], rebaseConfig, true));
}
}
function rebaseProperties(properties, validator, rebaseConfig) {
var property;
var value;
var i, l;
var j, m;
for (i = 0, l = properties.length; i < l; i++) {
property = properties[i];
for (j = 2 /* 0 is Token.PROPERTY, 1 is name */, m = property.length; j < m; j++) {
value = property[j][1];
if (validator.isUrl(value)) {
property[j][1] = rewriteUrl(value, rebaseConfig);
}
}
}
}
module.exports = rebase;