This commit is contained in:
szabomarton
2025-01-28 11:38:27 +01:00
parent 9c5ca86086
commit 7f4a15b9c3
36841 changed files with 4032468 additions and 1 deletions

View File

@@ -0,0 +1,7 @@
/*
* MIT License http://opensource.org/licenses/MIT
* Author: Ben Holloway @bholloway
*/
'use strict';
throw new Error('This "engine" is designed to fail at require time, for testing purposes only');

View File

@@ -0,0 +1,18 @@
/*
* MIT License http://opensource.org/licenses/MIT
* Author: Ben Holloway @bholloway
*/
'use strict';
/**
* Process the given CSS content into reworked CSS content.
*/
function process() {
return new Promise(function (_, reject) {
setTimeout(function () {
reject(new Error('This "engine" is designed to fail at processing time, for testing purposes only'));
}, 100);
});
}
module.exports = process;

View File

@@ -0,0 +1,130 @@
/*
* MIT License http://opensource.org/licenses/MIT
* Author: Ben Holloway @bholloway
*/
'use strict';
var os = require('os'),
path = require('path'),
postcss = require('postcss');
var fileProtocol = require('../file-protocol');
var algerbra = require('../position-algerbra');
var ORPHAN_CR_REGEX = /\r(?!\n)(.|\n)?/g;
/**
* Process the given CSS content into reworked CSS content.
*
* @param {string} sourceFile The absolute path of the file being processed
* @param {string} sourceContent CSS content without source-map
* @param {{outputSourceMap: boolean, transformDeclaration:function, absSourceMap:object,
* sourceMapConsumer:object, removeCR:boolean}} params Named parameters
* @return {{content: string, map: object}} Reworked CSS and optional source-map
*/
function process(sourceFile, sourceContent, params) {
// #107 libsass emits orphan CR not considered newline, postcss does consider newline (content vs source-map mismatch)
var 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)
])
.process(correctedContent, {
from: fileProtocol.prepend(sourceFile),
map : params.outputSourceMap && {
prev : !!params.absSourceMap && fileProtocol.prepend(params.absSourceMap),
inline : false,
annotation : false,
sourcesContent: true // #98 sourcesContent missing from output map
}
})
.then(result => ({
content: result.css,
map : params.outputSourceMap ? fileProtocol.remove(result.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;

View File

@@ -0,0 +1,155 @@
/*
* MIT License http://opensource.org/licenses/MIT
* Author: Ben Holloway @bholloway
*/
'use strict';
var path = require('path'),
convert = require('convert-source-map');
var fileProtocol = require('../file-protocol');
var rework = requireOptionalPeerDependency('rework'),
visit = requireOptionalPeerDependency('rework-visit');
/**
* Process the given CSS content into reworked CSS content.
*
* @param {string} sourceFile The absolute path of the file being processed
* @param {string} sourceContent CSS content without source-map
* @param {{outputSourceMap: boolean, transformDeclaration:function, absSourceMap:object,
* sourceMapConsumer:object}} params Named parameters
* @return {{content: string, map: object}} Reworked CSS and optional source-map
*/
function process(sourceFile, sourceContent, params) {
// embed source-map in css
// prepend file protocol to all sources to avoid problems with source map
var contentWithMap = sourceContent + (
params.absSourceMap ?
convert.fromObject(fileProtocol.prepend(params.absSourceMap)).toComment({multiline: true}) :
''
);
// need to prepend file protocol to source as well to avoid problems with source map
var reworked = rework(contentWithMap, {source: fileProtocol.prepend(sourceFile)})
.use(reworkPlugin)
.toString({
sourcemap : params.outputSourceMap,
sourcemapAsObject: params.outputSourceMap
});
// complete with source-map
if (params.outputSourceMap) {
return {
content: reworked.code,
map : fileProtocol.remove(reworked.map)
};
}
// complete without source-map
else {
return {
content: reworked,
map : null
};
}
/**
* Plugin for css rework that follows SASS transpilation.
*
* @param {object} stylesheet AST for the CSS output from SASS
*/
function reworkPlugin(stylesheet) {
// visit each node (selector) in the stylesheet recursively using the official utility method
// each node may have multiple declarations
visit(stylesheet, function visitor(declarations) {
if (declarations) {
declarations.forEach(eachDeclaration);
}
});
/**
* Process a declaration from the syntax tree.
* @param declaration
*/
function eachDeclaration(declaration) {
var isValid = declaration.value && (declaration.value.indexOf('url') >= 0);
if (isValid) {
declaration.value = params.transformDeclaration(declaration.value, getPathsAtChar);
}
/**
* Create a hash 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 {{selector:string, property:string}} Hash of base path strings
*/
function getPathsAtChar() {
var posSelector = declaration.parent && declaration.parent.position.start,
posProperty = declaration.position.start;
var result = {
property: positionToOriginalDirectory(posProperty),
selector: positionToOriginalDirectory(posSelector)
};
var isValid = [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');
} 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;
/**
* Require the given filename but fail with an error that `requireOptionalPeerDependencies` must be installed.
*
* @param moduleName The module to require
* @returns {*} The module
* @throws Error when module is not found
*/
function requireOptionalPeerDependency(moduleName) {
try {
return require(moduleName);
}
catch (error) {
if (error.message === 'Cannot find module \'' + moduleName + '\'') {
throw new Error('to use the "rework" engine you must install the optionalPeerDependencies');
}
else {
throw error;
}
}
}