-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
|
||
const {parseValues, stringifyValues} = require('css-selector-tokenizer'); | ||
const {relative, resolve, sep} = require('path'); | ||
|
||
const isRelativeUrl = filepath => /^(?:\.\.?(?:[\\\/]|$))/.test(filepath); | ||
|
||
exports.isRelativeUrl = isRelativeUrl; | ||
exports.iterateValues = iterateValues; | ||
exports.resolvePaths = resolvePaths; | ||
|
||
function iterateValues(values, iteratee) { | ||
values.nodes.forEach(value => | ||
value.nodes.forEach(item => iteratee(item))); | ||
} | ||
|
||
function resolvePaths(ast, from, to) { | ||
// @import | ||
ast.walkAtRules(atrule => { | ||
if (atrule.name === 'import') { | ||
const values = parseValues(atrule.params); | ||
|
||
iterateValues(values, item => { | ||
if (item.type === 'string' && isRelativeUrl(item.value)) | ||
item.value = resolveUrl(item.value, from, to); | ||
|
||
if (item.type === 'url' && isRelativeUrl(item.url)) | ||
item.url = resolveUrl(item.url, from, to); | ||
}); | ||
|
||
atrule.params = stringifyValues(values); | ||
} | ||
}); | ||
|
||
// background: url(..) | ||
ast.walkDecls(decl => { | ||
if (/url/.test(decl.value)) { | ||
const values = parseValues(decl.value); | ||
|
||
iterateValues(values, item => { | ||
if (item.type === 'url' && isRelativeUrl(item.url)) | ||
item.url = resolveUrl(item.url, from, to); | ||
}); | ||
|
||
decl.value = stringifyValues(values); | ||
} | ||
}); | ||
} | ||
|
||
function resolveUrl(url, from, to) { | ||
const resolvedUrl = relative(to, resolve(from, url)); | ||
|
||
return !isRelativeUrl(resolvedUrl) | ||
? '.' + sep + resolvedUrl | ||
: resolvedUrl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
!node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* should be './lib/reset1.css' */ | ||
@import url('./lib/reset1.css'); | ||
/* should be './lib/reset2.css' */ | ||
@import './lib/reset2.css'; | ||
._nested_url-decl { | ||
/* should be ./lib/spin-icon.svg */ | ||
background: url(./lib/spin-icon.svg) 0 0 no-repeat | ||
} | ||
._source_relative {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* should be './lib/reset1.css' */ | ||
@import url('./reset1.css'); | ||
/* should be './lib/reset2.css' */ | ||
@import './reset2.css'; | ||
|
||
.url-decl | ||
{ | ||
/* should be ./lib/spin-icon.svg */ | ||
background: url(./spin-icon.svg) 0 0 no-repeat; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.relative | ||
{ | ||
composes: url-decl from './lib/nested.css'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
|
||
const setup = require('../../setup'); | ||
const test = require('tape'); | ||
|
||
test('resolve-paths', t => { | ||
const {expected, resulting} = setup( | ||
'local-by-default', | ||
'extract-imports', | ||
'scope', | ||
'self' | ||
)(__dirname); | ||
|
||
t.equal(resulting.trim(), expected.trim()); | ||
t.end(); | ||
}); |