|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | var path = require('path');
|
4 |
| -var namedRegexp = require("named-js-regexp"); |
| 4 | +var namedRegexp = require('named-js-regexp'); |
5 | 5 |
|
6 | 6 | module.exports = function (css, resourcePath, aliases) {
|
7 |
| - var IMPORT_REGEX = namedRegexp(/^(:<import>@import\s+)(:<url_tag_open>url\()?\s*(:<quote_open>"|')(:<url>.+)\s*(:<quote_close>"|')(:<url_tag_close>\))?(:<context>.*);$/gm); |
8 |
| - var URL_REGEX = namedRegexp(/(:<url_tag_open>url\()\s*(:<quote_open>"|')?(:<url>.+)\s*(:<quote_close>"|')?(:<url_tag_close>\))/gm); |
9 |
| - css = parse(css, IMPORT_REGEX); |
10 |
| - css = parse(css, URL_REGEX); |
| 7 | + var IMPORT_REGEX = namedRegexp(/^(:<import>@import\s+)(:<url_tag_open>url\()?\s*(:<quote_open>"|')(:<url>.+)\s*(:<quote_close>"|')(:<url_tag_close>\))?(:<context>.*);$/gm); |
| 8 | + var URL_REGEX = namedRegexp(/(:<url_tag_open>url\()\s*(:<quote_open>"|')?(:<url>.+)\s*(:<quote_close>"|')?(:<url_tag_close>\))/gm); |
| 9 | + css = parse(css, IMPORT_REGEX); |
| 10 | + css = parse(css, URL_REGEX); |
11 | 11 |
|
12 |
| - return css; |
13 |
| - |
14 |
| - function resolve(url, resourcePath) { |
| 12 | + return css; |
15 | 13 |
|
16 |
| - for (var aliasName in aliases) { |
17 |
| - var regExp = new RegExp('^' + aliasName + '(.*)') |
| 14 | + function resolve(url, resourcePath) { |
18 | 15 |
|
19 |
| - if (regExp.test(url)) { |
20 |
| - return path.relative(path.dirname(resourcePath), path.resolve(aliases[aliasName] + url.replace(regExp, '$1'))); |
21 |
| - } |
22 |
| - } |
| 16 | + for (var aliasName in aliases) { |
| 17 | + var regExp = new RegExp('^' + aliasName + '(.*)'); |
23 | 18 |
|
24 |
| - return url; |
| 19 | + if (regExp.test(url)) { |
| 20 | + return path.relative(path.dirname(resourcePath), path.resolve(aliases[aliasName] + url.replace(regExp, '$1'))); |
| 21 | + } |
25 | 22 | }
|
26 | 23 |
|
27 |
| - function parse(css, regex) { |
28 |
| - var m, importOriginal, importReplace, replace = []; |
| 24 | + return url; |
| 25 | + } |
29 | 26 |
|
30 |
| - while ((m = regex.exec(css)) !== null) { |
31 |
| - if (m.index === regex.lastIndex) { |
32 |
| - regex.lastIndex++; |
33 |
| - } |
| 27 | + function parse(css, regex) { |
| 28 | + var m, importOriginal, importReplace, replace = []; |
34 | 29 |
|
35 |
| - replace.push(m); |
36 |
| - } |
| 30 | + while ((m = regex.exec(css)) !== null) { |
| 31 | + if (m.index === regex.lastIndex) { |
| 32 | + regex.lastIndex++; |
| 33 | + } |
37 | 34 |
|
38 |
| - for (var item in replace) { |
39 |
| - var m = replace[item]; |
| 35 | + replace.push(m); |
| 36 | + } |
40 | 37 |
|
41 |
| - importOriginal = ''; |
42 |
| - importReplace = ''; |
43 |
| - var mathGroups = m.groups(); |
| 38 | + for (var item in replace) { |
| 39 | + m = replace[item]; |
44 | 40 |
|
45 |
| - for (var groupName in mathGroups) { |
46 |
| - var match = mathGroups[groupName]; |
47 |
| - importOriginal += typeof match !== 'undefined' ? match : ''; |
| 41 | + importOriginal = ''; |
| 42 | + importReplace = ''; |
| 43 | + var mathGroups = m.groups(); |
48 | 44 |
|
49 |
| - if (groupName !== 'url') { |
50 |
| - importReplace += typeof match !== 'undefined' ? match : ''; |
51 |
| - } else { |
52 |
| - importReplace += resolve(match, resourcePath); |
53 |
| - } |
54 |
| - } |
| 45 | + for (var groupName in mathGroups) { |
| 46 | + var match = mathGroups[groupName]; |
| 47 | + importOriginal += typeof match !== 'undefined' ? match : ''; |
55 | 48 |
|
56 |
| - if (importOriginal !== importReplace) { |
57 |
| - css = css.replace(importOriginal, importReplace); |
58 |
| - } |
| 49 | + if (groupName !== 'url') { |
| 50 | + importReplace += typeof match !== 'undefined' ? match : ''; |
| 51 | + } else { |
| 52 | + importReplace += resolve(match, resourcePath); |
59 | 53 | }
|
| 54 | + } |
60 | 55 |
|
61 |
| - return css; |
| 56 | + if (importOriginal !== importReplace) { |
| 57 | + css = css.replace(importOriginal, importReplace); |
| 58 | + } |
62 | 59 | }
|
63 |
| -} |
| 60 | + |
| 61 | + return css; |
| 62 | + } |
| 63 | +}; |
0 commit comments