resolve-url-loader
Advanced tools
Comparing version 3.1.0 to 4.0.0-alpha.1
@@ -12,2 +12,3 @@ /* | ||
var fileProtocol = require('../file-protocol'); | ||
var algerbra = require('../position-algerbra'); | ||
@@ -53,3 +54,3 @@ var ORPHAN_CR_REGEX = /\r(?!\n)(.|\n)?/g; | ||
function postcssPlugin() { | ||
return function(styles) { | ||
return function applyPlugin(styles) { | ||
styles.walkDecls(eachDeclaration); | ||
@@ -63,17 +64,32 @@ }; | ||
function eachDeclaration(declaration) { | ||
var isValid = declaration.value && (declaration.value.indexOf('url') >= 0); | ||
var prefix, | ||
isValid = declaration.value && (declaration.value.indexOf('url') >= 0); | ||
if (isValid) { | ||
prefix = declaration.prop + declaration.raws.between; | ||
declaration.value = params.transformDeclaration(declaration.value, getPathsAtChar); | ||
} | ||
// reverse the original source-map to find the original source file before transpilation | ||
var startPosApparent = declaration.source.start, | ||
startPosOriginal = params.sourceMapConsumer && | ||
params.sourceMapConsumer.originalPositionFor(startPosApparent); | ||
/** | ||
* Create an iterable of base path strings. | ||
* | ||
* Position in the declaration is supported by postcss at the position of the url() statement. | ||
* | ||
* @param {number} index Index in the declaration value at which to evaluate | ||
* @throws Error on invalid source map | ||
* @returns {string[]} Iterable of base path strings possibly empty | ||
*/ | ||
function getPathsAtChar(index) { | ||
var subString = declaration.value.slice(0, index), | ||
posParent = algerbra.sanitise(declaration.parent.source.start), | ||
posProperty = algerbra.sanitise(declaration.source.start), | ||
posValue = algerbra.add([posProperty, algerbra.strToOffset(prefix)]), | ||
posSubString = algerbra.add([posValue, algerbra.strToOffset(subString)]); | ||
// we require a valid directory for the specified file | ||
var directory = | ||
startPosOriginal && | ||
startPosOriginal.source && | ||
fileProtocol.remove(path.dirname(startPosOriginal.source)); | ||
if (directory) { | ||
declaration.value = params.transformDeclaration(declaration.value, directory); | ||
var list = [posSubString, posValue, posProperty, posParent] | ||
.map(positionToOriginalDirectory) | ||
.filter(Boolean) | ||
.filter(filterUnique); | ||
if (list.length) { | ||
return list; | ||
} | ||
@@ -86,2 +102,4 @@ // source-map present but invalid entry | ||
); | ||
} else { | ||
return []; | ||
} | ||
@@ -91,4 +109,32 @@ } | ||
} | ||
/** | ||
* Given an apparent position find the directory of the original file. | ||
* | ||
* @param startPosApparent {{line: number, column: number}} | ||
* @returns {false|string} Directory of original file or false on invalid | ||
*/ | ||
function positionToOriginalDirectory(startPosApparent) { | ||
// reverse the original source-map to find the original source file before transpilation | ||
var startPosOriginal = | ||
!!params.sourceMapConsumer && | ||
params.sourceMapConsumer.originalPositionFor(startPosApparent); | ||
// we require a valid directory for the specified file | ||
var directory = | ||
!!startPosOriginal && | ||
!!startPosOriginal.source && | ||
fileProtocol.remove(path.dirname(startPosOriginal.source)); | ||
return directory; | ||
} | ||
/** | ||
* Simple Array filter predicate for unique elements. | ||
*/ | ||
function filterUnique(element, i, array) { | ||
return array.indexOf(element) === i; | ||
} | ||
} | ||
module.exports = process; |
@@ -78,15 +78,17 @@ /* | ||
if (isValid) { | ||
declaration.value = params.transformDeclaration(declaration.value, getPathsAtChar); | ||
} | ||
// reverse the original source-map to find the original source file before transpilation | ||
var startPosApparent = declaration.position.start, | ||
startPosOriginal = params.sourceMapConsumer && | ||
params.sourceMapConsumer.originalPositionFor(startPosApparent); | ||
// we require a valid directory for the specified file | ||
var directory = | ||
startPosOriginal && | ||
startPosOriginal.source && | ||
fileProtocol.remove(path.dirname(startPosOriginal.source)); | ||
/** | ||
* Create a list of base path strings. | ||
* | ||
* Position in the declaration is not supported since rework does not refine sourcemaps to this detail. | ||
* | ||
* @throws Error on invalid source map | ||
* @returns {string[]} Iterable of base path strings possibly empty | ||
*/ | ||
function getPathsAtChar() { | ||
var directory = positionToOriginalDirectory(declaration.position.start); | ||
if (directory) { | ||
declaration.value = params.transformDeclaration(declaration.value, directory); | ||
return [directory]; | ||
} | ||
@@ -96,2 +98,4 @@ // source-map present but invalid entry | ||
throw new Error('source-map information is not available at url() declaration'); | ||
} else { | ||
return []; | ||
} | ||
@@ -101,4 +105,25 @@ } | ||
} | ||
/** | ||
* Given an apparent position find the directory of the original file. | ||
* | ||
* @param startPosApparent {{line: number, column: number}} | ||
* @returns {false|string} Directory of original file or false on invalid | ||
*/ | ||
function positionToOriginalDirectory(startPosApparent) { | ||
// reverse the original source-map to find the original source file before transpilation | ||
var startPosOriginal = | ||
!!params.sourceMapConsumer && | ||
params.sourceMapConsumer.originalPositionFor(startPosApparent); | ||
// we require a valid directory for the specified file | ||
var directory = | ||
!!startPosOriginal && | ||
!!startPosOriginal.source && | ||
fileProtocol.remove(path.dirname(startPosOriginal.source)); | ||
return directory; | ||
} | ||
} | ||
module.exports = process; |
@@ -70,10 +70,7 @@ /* | ||
* @param {string} uri A uri path, relative or absolute | ||
* @param {string|Iterator.<string>} [baseOrIteratorOrAbsent] Optional absolute base path or iterator thereof | ||
* @param {Iterator<string>} [maybeIterator] Optional iterator of absolute base path strings | ||
* @return {string} Just the uri where base is empty or the uri appended to the base | ||
*/ | ||
return function joinProper(uri, baseOrIteratorOrAbsent) { | ||
var iterator = | ||
(typeof baseOrIteratorOrAbsent === 'undefined') && new Iterator([options.root ]) || | ||
(typeof baseOrIteratorOrAbsent === 'string' ) && new Iterator([baseOrIteratorOrAbsent]) || | ||
baseOrIteratorOrAbsent; | ||
return function joinProper(uri, maybeIterator) { | ||
var iterator = typeof maybeIterator === 'undefined' ? new Iterator([options.root]) : maybeIterator; | ||
@@ -132,3 +129,3 @@ var result = runIterator([]); | ||
* @param {string} uri A uri path, relative or absolute | ||
* @param {Array.<string>} bases Absolute base paths up to and including the found one | ||
* @param {string[]} bases Absolute base paths up to and including the found one | ||
* @param {boolean} isFound Indicates the last base was correct | ||
@@ -135,0 +132,0 @@ * @return {string} Formatted message |
@@ -8,3 +8,4 @@ /* | ||
var path = require('path'), | ||
loaderUtils = require('loader-utils'); | ||
loaderUtils = require('loader-utils'), | ||
Iterator = require('es6-iterator'); | ||
@@ -19,6 +20,8 @@ /** | ||
function valueProcessor(filename, options) { | ||
var URL_STATEMENT_REGEX = /(url\s*\()\s*(?:(['"])((?:(?!\2).)*)(\2)|([^'"](?:(?!\)).)*[^'"]))\s*(\))/g; | ||
var directory = path.dirname(filename); | ||
var join = options.join(filename, options); | ||
var URL_STATEMENT_REGEX = /(url\s*\(\s*)(?:(['"])((?:(?!\2).)*)(\2)|([^'"](?:(?!\)).)*[^'"]))(\s*\))/g, | ||
QUERY_REGEX = /([?#])/g; | ||
var directory = path.dirname(filename), | ||
join = options.join(filename, options); | ||
/** | ||
@@ -28,5 +31,6 @@ * Process the given CSS declaration value. | ||
* @param {string} value A declaration value that may or may not contain a url() statement | ||
* @param {string|Iterator.<string>} candidate An absolute path that may be the correct base or an Iterator thereof | ||
* @param {function(number|number[]):string[]} getPathsAtChar Given an offset in the declaration value get a | ||
* list of possible absolute path strings | ||
*/ | ||
return function transformValue(value, candidate) { | ||
return function transformValue(value, getPathsAtChar) { | ||
@@ -39,2 +43,3 @@ // allow multiple url() values in the declaration | ||
.split(URL_STATEMENT_REGEX) | ||
.map(initialise) | ||
.map(eachSplitOrGroup) | ||
@@ -44,6 +49,23 @@ .join(''); | ||
/** | ||
* Ensure all capture group tokens are a valid string. | ||
* | ||
* @param {string|undefined} token A capture group or uncaptured token | ||
* @returns {string} | ||
*/ | ||
function initialise(token) { | ||
return typeof token === 'string' ? token : ''; | ||
} | ||
/** | ||
* An Array reduce function that accumulates string length. | ||
*/ | ||
function accumulateLength(accumulator, element) { | ||
return accumulator + element.length; | ||
} | ||
/** | ||
* Encode the content portion of <code>url()</code> statements. | ||
* There are 4 capture groups in the split making every 5th unmatched. | ||
* There are 6 capture groups in the split making every 7th unmatched. | ||
* | ||
* @param {string} token A single split item | ||
* @param {string} element A single split item | ||
* @param {number} i The index of the item in the split | ||
@@ -53,11 +75,13 @@ * @param {Array} arr The array of split values | ||
*/ | ||
function eachSplitOrGroup(token, i, arr) { | ||
function eachSplitOrGroup(element, i, arr) { | ||
// we can get groups as undefined under certain match circumstances | ||
var initialised = token || ''; | ||
// the content of the url() statement is either in group 3 or group 5 | ||
var mod = i % 7; | ||
if ((mod === 3) || (mod === 5)) { | ||
// only one of the capture groups 3 or 5 will match the other will be falsey | ||
if (element && ((mod === 3) || (mod === 5))) { | ||
// calculate the offset of the match from the front of the string | ||
var position = arr.slice(0, i - mod + 1).reduce(accumulateLength, 0); | ||
// detect quoted url and unescape backslashes | ||
@@ -67,9 +91,11 @@ var before = arr[i - 1], | ||
isQuoted = (before === after) && ((before === '\'') || (before === '"')), | ||
unescaped = isQuoted ? initialised.replace(/\\{2}/g, '\\') : initialised; | ||
unescaped = isQuoted ? element.replace(/\\{2}/g, '\\') : element; | ||
// split into uri and query/hash and then find the absolute path to the uri | ||
var split = unescaped.split(/([?#])/g), | ||
// construct iterator as late as possible in case sourcemap is invalid at this location | ||
var split = unescaped.split(QUERY_REGEX), | ||
uri = split[0], | ||
absolute = testIsRelative(uri) && join(uri, candidate) || testIsAbsolute(uri) && join(uri), | ||
query = options.keepQuery ? split.slice(1).join('') : ''; | ||
query = options.keepQuery ? split.slice(1).join('') : '', | ||
absolute = testIsRelative(uri) && join(uri, new Iterator(getPathsAtChar(position))) || | ||
testIsAbsolute(uri) && join(uri); | ||
@@ -79,3 +105,3 @@ // use the absolute path in absolute mode or else relative path (or default to initialised) | ||
if (!absolute) { | ||
return initialised; | ||
return element; | ||
} else if (options.absolute) { | ||
@@ -91,3 +117,3 @@ return absolute.replace(/\\/g, '/') + query; | ||
else { | ||
return initialised; | ||
return element; | ||
} | ||
@@ -94,0 +120,0 @@ } |
{ | ||
"name": "resolve-url-loader", | ||
"version": "3.1.0", | ||
"version": "4.0.0-alpha.1", | ||
"description": "Webpack loader that resolves relative paths in url() statements based on the original source file", | ||
@@ -33,3 +33,3 @@ "main": "index.js", | ||
"index.js", | ||
"lib" | ||
"lib/**/+([a-z-]).js" | ||
], | ||
@@ -41,3 +41,3 @@ "scripts": { | ||
"adjust-sourcemap-loader": "2.0.0", | ||
"camelcase": "5.0.0", | ||
"camelcase": "5.3.1", | ||
"compose-function": "3.0.3", | ||
@@ -47,3 +47,3 @@ "convert-source-map": "1.6.0", | ||
"loader-utils": "1.2.3", | ||
"postcss": "7.0.14", | ||
"postcss": "7.0.18", | ||
"rework": "1.0.1", | ||
@@ -50,0 +50,0 @@ "rework-visit": "1.0.0", |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
46607
12
858
1
1
+ Addedcamelcase@5.3.1(transitive)
+ Addedpostcss@7.0.18(transitive)
- Removedpostcss@7.0.14(transitive)
Updatedcamelcase@5.3.1
Updatedpostcss@7.0.18