1 | var extractImportUrlAndMedia = require('./extract-import-url-and-media');
|
---|
2 | var restoreImport = require('./restore-import');
|
---|
3 | var rewriteUrl = require('./rewrite-url');
|
---|
4 |
|
---|
5 | var Token = require('../tokenizer/token');
|
---|
6 | var isImport = require('../utils/is-import');
|
---|
7 |
|
---|
8 | var SOURCE_MAP_COMMENT_PATTERN = /^\/\*# sourceMappingURL=(\S+) \*\/$/;
|
---|
9 |
|
---|
10 | function rebase(tokens, rebaseAll, validator, rebaseConfig) {
|
---|
11 | return rebaseAll
|
---|
12 | ? rebaseEverything(tokens, validator, rebaseConfig)
|
---|
13 | : rebaseAtRules(tokens, validator, rebaseConfig);
|
---|
14 | }
|
---|
15 |
|
---|
16 | function rebaseEverything(tokens, validator, rebaseConfig) {
|
---|
17 | var token;
|
---|
18 | var i, l;
|
---|
19 |
|
---|
20 | for (i = 0, l = tokens.length; i < l; i++) {
|
---|
21 | token = tokens[i];
|
---|
22 |
|
---|
23 | switch (token[0]) {
|
---|
24 | case Token.AT_RULE:
|
---|
25 | rebaseAtRule(token, validator, rebaseConfig);
|
---|
26 | break;
|
---|
27 | case Token.AT_RULE_BLOCK:
|
---|
28 | rebaseProperties(token[2], validator, rebaseConfig);
|
---|
29 | break;
|
---|
30 | case Token.COMMENT:
|
---|
31 | rebaseSourceMapComment(token, rebaseConfig);
|
---|
32 | break;
|
---|
33 | case Token.NESTED_BLOCK:
|
---|
34 | rebaseEverything(token[2], validator, rebaseConfig);
|
---|
35 | break;
|
---|
36 | case Token.RULE:
|
---|
37 | rebaseProperties(token[2], validator, rebaseConfig);
|
---|
38 | break;
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | return tokens;
|
---|
43 | }
|
---|
44 |
|
---|
45 | function rebaseAtRules(tokens, validator, rebaseConfig) {
|
---|
46 | var token;
|
---|
47 | var i, l;
|
---|
48 |
|
---|
49 | for (i = 0, l = tokens.length; i < l; i++) {
|
---|
50 | token = tokens[i];
|
---|
51 |
|
---|
52 | switch (token[0]) {
|
---|
53 | case Token.AT_RULE:
|
---|
54 | rebaseAtRule(token, validator, rebaseConfig);
|
---|
55 | break;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | return tokens;
|
---|
60 | }
|
---|
61 |
|
---|
62 | function rebaseAtRule(token, validator, rebaseConfig) {
|
---|
63 | if (!isImport(token[1])) {
|
---|
64 | return;
|
---|
65 | }
|
---|
66 |
|
---|
67 | var uriAndMediaQuery = extractImportUrlAndMedia(token[1]);
|
---|
68 | var newUrl = rewriteUrl(uriAndMediaQuery[0], rebaseConfig);
|
---|
69 | var mediaQuery = uriAndMediaQuery[1];
|
---|
70 |
|
---|
71 | token[1] = restoreImport(newUrl, mediaQuery);
|
---|
72 | }
|
---|
73 |
|
---|
74 | function rebaseSourceMapComment(token, rebaseConfig) {
|
---|
75 | var matches = SOURCE_MAP_COMMENT_PATTERN.exec(token[1]);
|
---|
76 |
|
---|
77 | if (matches && matches[1].indexOf('data:') === -1) {
|
---|
78 | token[1] = token[1].replace(matches[1], rewriteUrl(matches[1], rebaseConfig, true));
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | function rebaseProperties(properties, validator, rebaseConfig) {
|
---|
83 | var property;
|
---|
84 | var value;
|
---|
85 | var i, l;
|
---|
86 | var j, m;
|
---|
87 |
|
---|
88 | for (i = 0, l = properties.length; i < l; i++) {
|
---|
89 | property = properties[i];
|
---|
90 |
|
---|
91 | for (j = 2 /* 0 is Token.PROPERTY, 1 is name */, m = property.length; j < m; j++) {
|
---|
92 | value = property[j][1];
|
---|
93 |
|
---|
94 | if (validator.isUrl(value)) {
|
---|
95 | property[j][1] = rewriteUrl(value, rebaseConfig);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | module.exports = rebase;
|
---|