resolve-url-loader
Advanced tools
Comparing version 1.1.0 to 1.2.0
107
index.js
@@ -11,2 +11,3 @@ /* | ||
visit = require('rework-visit'), | ||
convert = require('convert-source-map'), | ||
SourceMapConsumer = require('source-map').SourceMapConsumer; | ||
@@ -23,14 +24,16 @@ | ||
*/ | ||
module.exports = function loader(content, sourceMap) { | ||
module.exports = function resolveUrlLoader(content, sourceMap) { | ||
/* jshint validthis:true */ | ||
// path of the file being processed | ||
var filePath = this.context, | ||
options = loaderUtils.parseQuery(this.query); | ||
// details of the file being processed | ||
var loader = this, | ||
filePath = loader.context, | ||
options = loaderUtils.parseQuery(loader.query); | ||
// loader result is cacheable | ||
this.cacheable(); | ||
loader.cacheable(); | ||
// incoming source-map | ||
var sourceMapConsumer; | ||
var sourceMapConsumer, | ||
contentWithMap; | ||
if (sourceMap) { | ||
@@ -44,23 +47,31 @@ | ||
sourceMapConsumer = new SourceMapConsumer(sourceMap); | ||
// embed source-map in css | ||
contentWithMap = content + convert.fromObject(sourceMap).toComment({multiline: true}); | ||
} | ||
// absent source map | ||
else { | ||
contentWithMap = content; | ||
} | ||
// process | ||
// rework will throw on css syntax errors | ||
var FILENAME_PLACEHOLDER = '<filename>'; | ||
var useMap = loader.sourceMap || options.sourceMap, | ||
reworked; | ||
try { | ||
return rework(content, { source: FILENAME_PLACEHOLDER }) | ||
reworked = rework(contentWithMap, {source: loader.resourcePath}) | ||
.use(reworkPlugin) | ||
.toString({ | ||
sourcemap: this.sourceMap || options.sourceMap | ||
sourcemap : useMap, | ||
sourcemapAsObject: useMap | ||
}); | ||
} | ||
// fail gracefully | ||
catch(exception) { | ||
var message = ('CSS syntax error (resolve-url-loader did not operate)' + exception.message) | ||
.replace(FILENAME_PLACEHOLDER, ''); | ||
catch (exception) { | ||
var message = 'CSS syntax error (resolve-url-loader did not operate): ' + exception.message; | ||
if (options.fail) { | ||
this.emitError(message); | ||
loader.emitError(message); | ||
} | ||
else if (!options.silent) { | ||
this.emitWarning(message); | ||
loader.emitWarning(message); | ||
} | ||
@@ -70,2 +81,15 @@ return content; // original content unchanged | ||
// adjust source-map | ||
if (reworked.map) { | ||
reworked.map.sources | ||
.forEach(relativePath); | ||
} | ||
// complete | ||
if (useMap) { | ||
loader.callback(null, reworked.code, reworked.map); | ||
} else { | ||
return reworked; | ||
} | ||
/** | ||
@@ -75,9 +99,24 @@ * Convert each relative file in the given array to absolute path. | ||
function absolutePath(value, i, array) { | ||
var location = value | ||
.replace(/^\//, '') // no leading slash | ||
.replace(/\b\/+\b/g, '/'); // remove duplicate slashes | ||
array[i] = path.resolve(filePath, location); | ||
// badly formed absolute (missing a leading slash) | ||
if (value.indexOf(process.cwd().slice(1)) === 0) { | ||
array[i] = '/' + value; | ||
} | ||
// not absolute | ||
else if (value.indexOf(process.cwd()) !== 0) { | ||
var location = value | ||
.replace(/\b[\\\/]+\b/g, path.sep) // remove duplicate slashes (windows) | ||
.replace(/^[\\\/]\./, '.'); // remove erroneous leading slash on relative paths | ||
array[i] = path.resolve(filePath, location); | ||
} | ||
} | ||
/** | ||
* Convert each absolute file in the given array to a relative path. | ||
*/ | ||
function relativePath(value, i, array) { | ||
array[i] = path.relative(filePath, value); | ||
} | ||
/** | ||
* Plugin for css rework that follows SASS transpilation | ||
@@ -87,2 +126,3 @@ * @param {object} stylesheet AST for the CSS output from SASS | ||
function reworkPlugin(stylesheet) { | ||
var hasErrored = false; | ||
@@ -113,14 +153,23 @@ // visit each node (selector) in the stylesheet recursively using the official utility method | ||
// we require a valid directory for the specified file | ||
if (!directory) { | ||
throw new Error('failed to decode source map'); | ||
if (directory) { | ||
// allow multiple url() values in the declaration | ||
// split by url statements and process the content | ||
// additional capture groups are needed to match quotations correctly | ||
// escaped quotations are not considered | ||
declaration.value = declaration.value | ||
.split(URL_STATEMENT_REGEX) | ||
.map(eachSplitOrGroup) | ||
.join(''); | ||
} | ||
// allow multiple url() values in the declaration | ||
// split by url statements and process the content | ||
// additional capture groups are needed to match quotations correctly | ||
// escaped quotations are not considered | ||
declaration.value = declaration.value | ||
.split(URL_STATEMENT_REGEX) | ||
.map(eachSplitOrGroup) | ||
.join(''); | ||
// invalid source map | ||
else if (!hasErrored) { | ||
hasErrored = true; | ||
var message = 'failed to decode source map, ensure CSS source map is present'; | ||
if (options.fail) { | ||
loader.emitError(message); | ||
} else if (!options.silent) { | ||
loader.emitWarning(message); | ||
} | ||
} | ||
} | ||
@@ -127,0 +176,0 @@ |
{ | ||
"name": "resolve-url-loader", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Webpack loader that resolves relative paths in url() statements based on the original source file", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
@@ -32,2 +29,3 @@ "type": "git", | ||
"dependencies": { | ||
"convert-source-map": "^1.1.1", | ||
"loader-utils": "^0.2.11", | ||
@@ -34,0 +32,0 @@ "rework": "^1.0.1", |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
14532
265
5
+ Addedconvert-source-map@^1.1.1
+ Addedconvert-source-map@1.9.0(transitive)