0.2.0 - Mid migration

This commit is contained in:
Daniel Mason 2022-04-25 14:47:15 +12:00
parent 139e6a915e
commit 7e38fdbd7d
42393 changed files with 5358157 additions and 62 deletions

18
web/node_modules/resolve-url-loader/lib/engine/fail.js generated vendored Normal file
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, for testing purposes only'));
}, 100);
});
}
module.exports = process;

View file

@ -0,0 +1,89 @@
/*
* 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 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(styles) {
styles.walkDecls(eachDeclaration);
};
/**
* Process a declaration from the syntax tree.
* @param declaration
*/
function eachDeclaration(declaration) {
var isValid = declaration.value && (declaration.value.indexOf('url') >= 0);
if (isValid) {
// 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);
// 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);
}
// source-map present but invalid entry
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)')
);
}
}
}
}
}
module.exports = process;

View file

@ -0,0 +1,101 @@
/*
* MIT License http://opensource.org/licenses/MIT
* Author: Ben Holloway @bholloway
*/
'use strict';
var path = require('path'),
convert = require('convert-source-map'),
rework = require('rework'),
visit = require('rework-visit');
var fileProtocol = require('../file-protocol');
/**
* 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) {
// 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));
if (directory) {
declaration.value = params.transformDeclaration(declaration.value, directory);
}
// source-map present but invalid entry
else if (params.sourceMapConsumer) {
throw new Error('source-map information is not available at url() declaration');
}
}
}
}
}
module.exports = process;

View file

@ -0,0 +1,39 @@
/*
* MIT License http://opensource.org/licenses/MIT
* Author: Ben Holloway @bholloway
*/
'use strict';
/**
* Prepend file:// protocol to source path string or source-map sources.
*/
function prepend(candidate) {
if (typeof candidate === 'string') {
return 'file://' + candidate;
} else if (candidate && (typeof candidate === 'object') && Array.isArray(candidate.sources)) {
return Object.assign({}, candidate, {
sources: candidate.sources.map(prepend)
});
} else {
throw new Error('expected string|object');
}
}
exports.prepend = prepend;
/**
* Remove file:// protocol from source path string or source-map sources.
*/
function remove(candidate) {
if (typeof candidate === 'string') {
return candidate.replace(/^file:\/{2}/, '');
} else if (candidate && (typeof candidate === 'object') && Array.isArray(candidate.sources)) {
return Object.assign({}, candidate, {
sources: candidate.sources.map(remove)
});
} else {
throw new Error('expected string|object');
}
}
exports.remove = remove;

View file

@ -0,0 +1,190 @@
/*
* MIT License http://opensource.org/licenses/MIT
* Author: Ben Holloway @bholloway
*/
'use strict';
var path = require('path'),
fs = require('fs'),
compose = require('compose-function'),
Iterator = require('es6-iterator');
var PACKAGE_NAME = require('../package.json').name;
var simpleJoin = compose(path.normalize, path.join);
/**
* The default join function iterates over possible base paths until a suitable join is found.
*
* The first base path is used as fallback for the case where none of the base paths can locate the actual file.
*
* @type {function}
*/
exports.defaultJoin = createJoinForPredicate(
function predicate(_, uri, base, i, next) {
var absolute = simpleJoin(base, uri);
return fs.existsSync(absolute) ? absolute : next((i === 0) ? absolute : null);
},
'defaultJoin'
);
/**
* Define a join function by a predicate that tests possible base paths from an iterator.
*
* The `predicate` is of the form:
*
* ```
* function(filename, uri, base, i, next):string|null
* ```
*
* Given the uri and base it should either return:
* - an absolute path success
* - a call to `next(null)` as failure
* - a call to `next(absolute)` where absolute is placeholder and the iterator continues
*
* The value given to `next(...)` is only used if success does not eventually occur.
*
* The `file` value is typically unused but useful if you would like to differentiate behaviour.
*
* You can write a much simpler function than this if you have specific requirements.
*
* @param {function} predicate A function that tests values
* @param {string} [name] Optional name for the resulting join function
*/
function createJoinForPredicate(predicate, name) {
/**
* A factory for a join function with logging.
*
* @param {string} filename The current file being processed
* @param {{debug:function|boolean,root:string}} options An options hash
*/
function join(filename, options) {
var log = createDebugLogger(options.debug);
/**
* Join function proper.
*
* For absolute uri only `uri` will be provided. In this case we substitute any `root` given in options.
*
* @param {string} uri A uri path, relative or absolute
* @param {string|Iterator.<string>} [baseOrIteratorOrAbsent] Optional absolute base path or iterator thereof
* @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;
var result = runIterator([]);
log(createJoinMsg, [filename, uri, result, result.isFound]);
return (typeof result.absolute === 'string') ? result.absolute : uri;
function runIterator(accumulator) {
var nextItem = iterator.next();
var base = !nextItem.done && nextItem.value;
if (typeof base === 'string') {
var element = predicate(filename, uri, base, accumulator.length, next);
if ((typeof element === 'string') && path.isAbsolute(element)) {
return Object.assign(
accumulator.concat(base),
{isFound: true, absolute: element}
);
} else if (Array.isArray(element)) {
return element;
} else {
throw new Error('predicate must return an absolute path or the result of calling next()');
}
} else {
return accumulator;
}
function next(fallback) {
return runIterator(Object.assign(
accumulator.concat(base),
(typeof fallback === 'string') && {absolute: fallback}
));
}
}
};
}
function toString() {
return '[Function: ' + name + ']';
}
return Object.assign(join, name && {
valueOf : toString,
toString: toString
});
}
exports.createJoinForPredicate = createJoinForPredicate;
/**
* Format a debug message.
*
* @param {string} file The file being processed by webpack
* @param {string} uri A uri path, relative or absolute
* @param {Array.<string>} bases Absolute base paths up to and including the found one
* @param {boolean} isFound Indicates the last base was correct
* @return {string} Formatted message
*/
function createJoinMsg(file, uri, bases, isFound) {
return [PACKAGE_NAME + ': ' + pathToString(file) + ': ' + uri]
.concat(bases.map(pathToString).filter(Boolean))
.concat(isFound ? 'FOUND' : 'NOT FOUND')
.join('\n ');
/**
* If given path is within `process.cwd()` then show relative posix path, otherwise show absolute posix path.
*
* @param {string} absolute An absolute path
* @return {string} A relative or absolute path
*/
function pathToString(absolute) {
if (!absolute) {
return null;
} else {
var relative = path.relative(process.cwd(), absolute)
.split(path.sep);
return ((relative[0] === '..') ? absolute.split(path.sep) : ['.'].concat(relative).filter(Boolean))
.join('/');
}
}
}
exports.createJoinMsg = createJoinMsg;
/**
* A factory for a log function predicated on the given debug parameter.
*
* The logging function created accepts a function that formats a message and parameters that the function utilises.
* Presuming the message function may be expensive we only call it if logging is enabled.
*
* The log messages are de-duplicated based on the parameters, so it is assumed they are simple types that stringify
* well.
*
* @param {function|boolean} debug A boolean or debug function
* @return {function(function, array)} A logging function possibly degenerate
*/
function createDebugLogger(debug) {
var log = !!debug && ((typeof debug === 'function') ? debug : console.log);
var cache = {};
return log ? actuallyLog : noop;
function noop() {}
function actuallyLog(msgFn, params) {
var key = JSON.stringify(params);
if (!cache[key]) {
cache[key] = true;
log(msgFn.apply(null, params));
}
}
}
exports.createDebugLogger = createDebugLogger;

View file

@ -0,0 +1,26 @@
/*
* MIT License http://opensource.org/licenses/MIT
* Author: Ben Holloway @bholloway
*/
'use strict';
var stream = require('stream');
var maybeStream = process[process.env.RESOLVE_URL_LOADER_TEST_HARNESS];
function logToTestHarness(options) {
if (!!maybeStream && (typeof maybeStream === 'object') && (maybeStream instanceof stream.Writable)) {
Object.keys(options).map(eachOptionKey);
maybeStream = null; // ensure we log only once
}
function eachOptionKey(key) {
var value = options[key];
var text = (typeof value === 'undefined') ?
String(value) :
(JSON.stringify(value.valueOf()) || '-unstringifyable-');
maybeStream.write(key + ': ' + text + '\n');
}
}
module.exports = logToTestHarness;

View file

@ -0,0 +1,114 @@
/*
* MIT License http://opensource.org/licenses/MIT
* Author: Ben Holloway @bholloway
*/
'use strict';
var path = require('path'),
loaderUtils = require('loader-utils');
/**
* Create a value processing function for a given file path.
*
* @param {string} filename The current file being processed
* @param {{absolute:string, keepQuery:boolean, join:function, root:string}} options Options hash
* @return {function} value processing function
*/
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);
/**
* 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
*/
return function transformValue(value, candidate) {
// 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
return value
.split(URL_STATEMENT_REGEX)
.map(eachSplitOrGroup)
.join('');
/**
* Encode the content portion of <code>url()</code> statements.
* There are 4 capture groups in the split making every 5th unmatched.
*
* @param {string} token A single split item
* @param {number} i The index of the item in the split
* @param {Array} arr The array of split values
* @returns {string} Every 3 or 5 items is an encoded url everything else is as is
*/
function eachSplitOrGroup(token, 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)) {
// detect quoted url and unescape backslashes
var before = arr[i - 1],
after = arr[i + 1],
isQuoted = (before === after) && ((before === '\'') || (before === '"')),
unescaped = isQuoted ? initialised.replace(/\\{2}/g, '\\') : initialised;
// split into uri and query/hash and then find the absolute path to the uri
var split = unescaped.split(/([?#])/g),
uri = split[0],
absolute = testIsRelative(uri) && join(uri, candidate) || testIsAbsolute(uri) && join(uri),
query = options.keepQuery ? split.slice(1).join('') : '';
// use the absolute path in absolute mode or else relative path (or default to initialised)
// #6 - backslashes are not legal in URI
if (!absolute) {
return initialised;
} else if (options.absolute) {
return absolute.replace(/\\/g, '/') + query;
} else {
return loaderUtils.urlToRequest(
path.relative(directory, absolute).replace(/\\/g, '/') + query
);
}
}
// everything else, including parentheses and quotation (where present) and media statements
else {
return initialised;
}
}
};
/**
* The loaderUtils.isUrlRequest() doesn't support windows absolute paths on principle. We do not subscribe to that
* dogma so we add path.isAbsolute() check to allow them.
*
* We also eliminate module relative (~) paths.
*
* @param {string|undefined} uri A uri string possibly empty or undefined
* @return {boolean} True for relative uri
*/
function testIsRelative(uri) {
return !!uri && loaderUtils.isUrlRequest(uri, false) && !path.isAbsolute(uri) && (uri.indexOf('~') !== 0);
}
/**
* The loaderUtils.isUrlRequest() doesn't support windows absolute paths on principle. We do not subscribe to that
* dogma so we add path.isAbsolute() check to allow them.
*
* @param {string|undefined} uri A uri string possibly empty or undefined
* @return {boolean} True for absolute uri
*/
function testIsAbsolute(uri) {
return !!uri && (typeof options.root === 'string') && loaderUtils.isUrlRequest(uri, options.root) &&
(/^\//.test(uri) || path.isAbsolute(uri));
}
}
module.exports = valueProcessor;