resolve-url-loader
Advanced tools
Comparing version 4.0.0 to 5.0.0-alpha.1
@@ -23,3 +23,3 @@ /* | ||
'DEP_RESOLVE_URL_LOADER_OPTION_ENGINE', | ||
'the "engine" option is deprecated, "postcss" engine is the default, using "rework" engine is not advised' | ||
'the "engine" option is deprecated, "postcss" engine is the default, there are no other available engines' | ||
], | ||
@@ -26,0 +26,0 @@ keepQuery: [ |
@@ -7,10 +7,10 @@ /* | ||
var os = require('os'), | ||
path = require('path'), | ||
postcss = require('postcss'); | ||
const os = require('os'); | ||
const path = require('path'); | ||
const postcss = require('postcss'); | ||
var fileProtocol = require('../file-protocol'); | ||
var algerbra = require('../position-algerbra'); | ||
const fileProtocol = require('../file-protocol'); | ||
const algerbra = require('../position-algerbra'); | ||
var ORPHAN_CR_REGEX = /\r(?!\n)(.|\n)?/g; | ||
const ORPHAN_CR_REGEX = /\r(?!\n)(.|\n)?/g; | ||
@@ -28,10 +28,92 @@ /** | ||
// #107 libsass emits orphan CR not considered newline, postcss does consider newline (content vs source-map mismatch) | ||
var correctedContent = params.removeCR && (os.EOL !== '\r') ? | ||
const correctedContent = params.removeCR && (os.EOL !== '\r') ? | ||
sourceContent.replace(ORPHAN_CR_REGEX, ' $1') : | ||
sourceContent; | ||
// prepend file protocol to all sources to avoid problems with source map | ||
return postcss([ | ||
postcss.plugin('postcss-resolve-url', postcssPlugin) | ||
]) | ||
// IMPORTANT - prepend file protocol to all sources to avoid problems with source map | ||
const plugin = Object.assign( | ||
() => ({ | ||
postcssPlugin: 'postcss-resolve-url', | ||
prepare: () => { | ||
const visited = new Set(); | ||
/** | ||
* 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 | ||
*/ | ||
const positionToOriginalDirectory = (startPosApparent) => { | ||
// reverse the original source-map to find the original source file before transpilation | ||
const startPosOriginal = | ||
!!params.sourceMapConsumer && | ||
params.sourceMapConsumer.originalPositionFor(startPosApparent); | ||
// we require a valid directory for the specified file | ||
const directory = | ||
!!startPosOriginal && | ||
!!startPosOriginal.source && | ||
fileProtocol.remove(path.dirname(startPosOriginal.source)); | ||
return directory; | ||
}; | ||
return { | ||
Declaration: (declaration) => { | ||
var prefix, | ||
isValid = declaration.value && (declaration.value.indexOf('url') >= 0) && !visited.has(declaration); | ||
if (isValid) { | ||
prefix = declaration.prop + declaration.raws.between; | ||
declaration.value = params.transformDeclaration(declaration.value, getPathsAtChar); | ||
visited.add(declaration); | ||
} | ||
/** | ||
* Create a hash 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 {{subString:string, value:string, property:string, selector:string}} Hash of base path strings | ||
*/ | ||
function getPathsAtChar(index) { | ||
var subString = declaration.value.slice(0, index), | ||
posSelector = 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)]); | ||
var result = { | ||
subString: positionToOriginalDirectory(posSubString), | ||
value : positionToOriginalDirectory(posValue), | ||
property : positionToOriginalDirectory(posProperty), | ||
selector : positionToOriginalDirectory(posSelector) | ||
}; | ||
var isValid = [result.subString, result.value, result.property, result.selector].every(Boolean); | ||
if (isValid) { | ||
return result; | ||
} | ||
else if (params.sourceMapConsumer) { | ||
throw new Error( | ||
'source-map information is not available at url() declaration ' + ( | ||
ORPHAN_CR_REGEX.test(sourceContent) ? | ||
'(found orphan CR, try removeCR option)' : | ||
'(no orphan CR found)' | ||
) | ||
); | ||
} else { | ||
throw new Error('a valid source-map is not present (ensure preceding loaders output a source-map)'); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}), | ||
{ postcss: true } | ||
); | ||
// IMPORTANT - prepend file protocol to all sources to avoid problems with source map | ||
return postcss([plugin]) | ||
.process(correctedContent, { | ||
@@ -46,88 +128,8 @@ from: fileProtocol.prepend(sourceFile), | ||
}) | ||
.then(result => ({ | ||
content: result.css, | ||
map : params.outputSourceMap ? fileProtocol.remove(result.map.toJSON()) : null | ||
.then(({css, map}) => ({ | ||
content: css, | ||
map : params.outputSourceMap ? fileProtocol.remove(map.toJSON()) : null | ||
})); | ||
/** | ||
* Plugin for postcss that follows SASS transpilation. | ||
*/ | ||
function postcssPlugin() { | ||
return function applyPlugin(styles) { | ||
styles.walkDecls(eachDeclaration); | ||
}; | ||
/** | ||
* Process a declaration from the syntax tree. | ||
* @param declaration | ||
*/ | ||
function eachDeclaration(declaration) { | ||
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); | ||
} | ||
/** | ||
* Create a hash 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 {{subString:string, value:string, property:string, selector:string}} Hash of base path strings | ||
*/ | ||
function getPathsAtChar(index) { | ||
var subString = declaration.value.slice(0, index), | ||
posSelector = 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)]); | ||
var result = { | ||
subString: positionToOriginalDirectory(posSubString), | ||
value : positionToOriginalDirectory(posValue), | ||
property : positionToOriginalDirectory(posProperty), | ||
selector : positionToOriginalDirectory(posSelector) | ||
}; | ||
var isValid = [result.subString, result.value, result.property, result.selector].every(Boolean); | ||
if (isValid) { | ||
return result; | ||
} | ||
else if (params.sourceMapConsumer) { | ||
throw new Error( | ||
'source-map information is not available at url() declaration ' + | ||
(ORPHAN_CR_REGEX.test(sourceContent) ? '(found orphan CR, try removeCR option)' : '(no orphan CR found)') | ||
); | ||
} else { | ||
throw new Error('a valid source-map is not present (ensure preceding loaders output a source-map)'); | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* 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; |
@@ -25,3 +25,3 @@ /* | ||
(relative.lastIndexOf('..') < 2) ? relative : | ||
absolutePath.split(path.sep); | ||
absolutePath.replace(/^[A-Z]\:/, '').split(path.sep); | ||
return segments.join('/'); | ||
@@ -28,0 +28,0 @@ } |
{ | ||
"name": "resolve-url-loader", | ||
"version": "4.0.0", | ||
"version": "5.0.0-alpha.1", | ||
"description": "Webpack loader that resolves relative paths in url() statements based on the original source file", | ||
@@ -30,3 +30,3 @@ "main": "index.js", | ||
"engines": { | ||
"node": ">=8.9" | ||
"node": ">=12" | ||
}, | ||
@@ -42,17 +42,5 @@ "files": [ | ||
"loader-utils": "^2.0.0", | ||
"postcss": "^7.0.35", | ||
"postcss": "^8.2.14", | ||
"source-map": "0.6.1" | ||
}, | ||
"peerDependencies": { | ||
"rework": "1.0.1", | ||
"rework-visit": "1.0.0" | ||
}, | ||
"peerDependenciesMeta": { | ||
"rework": { | ||
"optional": true | ||
}, | ||
"rework-visit": { | ||
"optional": true | ||
} | ||
} | ||
} |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
5
4
112617
21
953
1
2
+ Addednanoid@3.3.7(transitive)
+ Addedpicocolors@1.0.0(transitive)
+ Addedpostcss@8.4.38(transitive)
+ Addedsource-map-js@1.2.0(transitive)
- Removedatob@2.1.2(transitive)
- Removedconvert-source-map@0.3.5(transitive)
- Removedcss@2.2.4(transitive)
- Removeddecode-uri-component@0.2.2(transitive)
- Removedinherits@2.0.4(transitive)
- Removedpicocolors@0.2.1(transitive)
- Removedpostcss@7.0.39(transitive)
- Removedresolve-url@0.2.1(transitive)
- Removedrework@1.0.1(transitive)
- Removedrework-visit@1.0.0(transitive)
- Removedsource-map-resolve@0.5.3(transitive)
- Removedsource-map-url@0.4.1(transitive)
- Removedurix@0.1.0(transitive)
Updatedpostcss@^8.2.14