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

40
web/node_modules/adjust-sourcemap-loader/lib/loader.js generated vendored Normal file
View file

@ -0,0 +1,40 @@
'use strict';
var path = require('path');
var loaderUtils = require('loader-utils');
var process = require('./process');
/**
* Webpack loader that manipulates the source-map of a preceding loader.
* @this {object} The loader context
* @param {string} content The content
* @param {object} sourceMap The source-map
* @returns {string|String}
*/
function loader(content, sourceMap) {
/* jshint validthis:true */
// loader result is cacheable
this.cacheable();
// webpack 1: prefer loader query, else options object
// webpack 2: prefer loader options
// webpack 3: deprecate loader.options object
// webpack 4: loader.options no longer defined
var options = Object.assign(
{},
this.options && this.options.adjustSourcemapLoader,
loaderUtils.getOptions(this),
{sep: path.sep}
);
// process the source-map
var outputMap = process(this, options, sourceMap);
// need to use callback when there are multiple arguments
this.callback(null, content, outputMap);
}
module.exports = loader;

View file

@ -0,0 +1,11 @@
'use strict';
var process = require('./process');
function moduleFilenameTemplate(options) {
return function templateFn(parameters) {
return process(parameters, options, parameters.resourcePath);
};
}
module.exports = moduleFilenameTemplate;

View file

@ -0,0 +1,58 @@
'use strict';
var PACKAGE_NAME = require('../../package.json').name,
PADDING = (new Array(11)).join(' ');
/**
* Format a debug message
* @param {{resourcePath:string, loaders:Array, loaderIndex:number}} context A loader or compilation
* @param {{input:Array.<string>, absolute:Array.<string>, output:Array.<string>, root:string}} info Source-map info
* @returns {string} An encoded debug string
*/
function debugMessage(context, info) {
return [
' ',
PACKAGE_NAME + ':',
' ' + context.resourcePath,
formatField('@', precedingRequest(context)),
formatField('INPUT', info.input || '(source-map absent)'),
formatField('ABSOLUTE', info.absolute),
formatField('OUTPUT', info.output),
formatField('ROOT', info.root)
]
.filter(Boolean)
.join('\n');
}
module.exports = debugMessage;
/**
* Find the request that precedes this loader in the loader chain
* @param {{loaders:Array, loaderIndex:number}} loader The loader context
* @returns {string} The request of the preceding loader
*/
function precedingRequest(loader) {
var isLoader = ('loaderIndex' in loader) && ('loaders' in loader) && Array.isArray(loader.loaders);
if (isLoader) {
var index = loader.loaderIndex + 1;
return (index in loader.loaders) ? loader.loaders[index].request : '(no preceding loader)';
}
}
/**
* Where the data is truthy then format it with a right-aligned title.
* @param {string} title
* @param {*} data The data to display
* @returns {boolean|string} False where data is falsey, else formatted message
*/
function formatField(title, data) {
return !!data && (rightAlign(title) + formatData(data));
function rightAlign(text) {
return (PADDING + text + ' ').slice(-PADDING.length);
}
function formatData(data) {
return Array.isArray(data) ? data.join('\n' + PADDING) : data;
}
}

View file

@ -0,0 +1,78 @@
'use strict';
var getFieldAsFn = require('./get-field-as-fn');
/**
* Create a decoder for input sources using the given codec hash
* @this {object} A loader or compilation
* @param {Array.<object>} codecs A list of codecs, each with a `decode` function
* @param {boolean} mustDecode Return an error for a source that is not decoded
* @returns {function(string):string|Error} A decode function that returns an absolute path or else an Error
*/
function decodeSourcesWith(codecs, mustDecode) {
/* jshint validthis:true */
var context = this;
// get a list of valid decoders
var candidates = [].concat(codecs)
.reduce(reduceValidDecoder.bind(null, codecs), []);
/**
* Attempt to decode the given source path using the previously supplied codecs
* @param {string} inputSource A source path from a source map
* @returns {Error|string|undefined} An absolute path if decoded else an error if encountered else undefined
*/
return function decode(inputSource) {
// attempt all candidates until a match
for (var i = 0, decoded = null; i < candidates.length && !decoded; i++) {
// call the decoder
try {
decoded = candidates[i].decode.call(context, inputSource);
}
catch (exception) {
return getNamedError(exception);
}
// match implies a return value
if (decoded) {
// abstract sources cannot be decoded, only validated
if (candidates[i].abstract) {
return undefined;
}
// non-string implies error
if (typeof decoded !== 'string') {
return getNamedError('Decoder returned a truthy value but it is not a string:\n' + decoded);
}
// otherwise success
else {
return decoded;
}
}
}
// default is undefined or error
return mustDecode ? new Error('No viable decoder for source: ' + inputSource) : undefined;
function getNamedError(details) {
var name = candidates[i].name || '(unnamed)',
message = [
'Decoding with codec: ' + name,
'Incoming source: ' + inputSource,
details && (details.stack ? details.stack : details)
]
.filter(Boolean)
.join('\n');
return new Error(message);
}
};
}
module.exports = decodeSourcesWith;
function reduceValidDecoder(reduced, codec) {
var decoder = getFieldAsFn('decode')(codec);
return decoder ? reduced.concat(codec) : reduced;
}

View file

@ -0,0 +1,48 @@
'use strict';
var getFieldAsFn = require('./get-field-as-fn'),
CustomError = require('./get-error');
/**
* Create an encoder for output sources using the given codec hash
* @throws Error Where the given codec is missing an encode function
* @this {object} A loader or compilation
* @param {{encode:function}} codec A single codec with an `encode` function
* @returns {function(string):string|Error|false} An encode function that takes an absolute path
*/
function encodeSourcesWith(codec) {
/* jshint validthis:true */
var context = this,
encoder = getFieldAsFn('encode')(codec);
if (!encoder) {
return new CustomError('Specified format does not support encoding (it lacks an "encoder" function)');
}
else {
return function encode(absoluteSource) {
// call the encoder
var encoded;
try {
encoded = absoluteSource && encoder.call(context, absoluteSource);
}
catch (exception) {
return getNamedError(exception);
}
return encoded;
function getNamedError(details) {
var name = codec.name || '(unnamed)',
message = [
'Encoding with codec: ' + name,
'Absolute source: ' + absoluteSource,
details && (details.stack ? details.stack : details)
]
.filter(Boolean)
.join('\n');
return new Error(message);
}
};
}
}
module.exports = encodeSourcesWith;

View file

@ -0,0 +1,17 @@
'use strict';
var PACKAGE_NAME = require('../../package.json').name;
/**
* Get an Error instance for the given message
* @param {...*} message Any number of message arguments
* @returns {Error}
*/
function getError() {
var message = (PACKAGE_NAME + ':\n' + Array.prototype.slice.call(arguments).join(' '))
.split(/\s*\n\s*/)
.join('\n ');
return new Error(message);
}
module.exports = getError;

View file

@ -0,0 +1,14 @@
'use strict';
/**
* Create a method that will retrieve the given field from an object where that field has a function value
* @param {string} field The field to consider
* @returns {function(object):function} A method that gets functions from the given field
*/
function getFieldAsFn(field) {
return function getFromValue(value) {
return !!value && (typeof value === 'object') && (typeof value[field] === 'function') && value[field];
};
}
module.exports = getFieldAsFn;

View file

@ -0,0 +1,117 @@
'use strict';
var debugMessage = require('./debug-message'),
toRegExp = require('./to-reg-exp'),
throwErrors = require('./throw-errors'),
decodeSourcesWith = require('./decode-sources-with'),
locateRootWith = require('./locate-root-with'),
encodeSourcesWith = require('./encode-sources-with'),
testCodec = require('./test-codec');
var CODECS = require('../../codec');
/**
* Process the given source-map per the given options.
* @param {{resourcePath:string, context:string, output:{path:string}}} context A loader or compilation
* @param {{debug:boolean, fail:boolean, format:string|boolean, root:string, codecs:object}} opt Options hash
* @param {object|string} sourceMapOrSource An incoming source-map or single source path
* @returns {undefined|object|string} An amended source-map or source path else undefined
*/
function process(context, opt, sourceMapOrSource) {
// default options
var options = Object.assign({
sep : '/',
debug : false,
fail : false,
format: false,
root : false,
codecs: CODECS
}, opt);
// validate codecs
var codecs = options.codecs
.filter(testCodec);
// determine what is present
var inputMap = !!sourceMapOrSource && (typeof sourceMapOrSource === 'object') && sourceMapOrSource,
inputPath = (typeof sourceMapOrSource === 'string') && sourceMapOrSource,
inputSources = inputMap && inputMap.sources || inputPath && [inputPath];
// what we need to produce
var absoluteSources,
outputSources,
outputRoot,
outputMap;
if (inputSources) {
// decode each source with the first valid codec
absoluteSources = inputSources
.map(decodeSourcesWith.call(context, codecs, options.fail));
// check for decode errors
throwErrors(context.resourcePath, absoluteSources);
// output map is a copy unless absent or we are removing
outputMap = (!inputMap || (options.format === 'remove')) ? undefined : Object.assign({}, inputMap);
// some change in format
if (options.format) {
// find the specified codec in the codecs list
var codec = codecs
.filter(testNamedCodec)
.pop();
if (!codec) {
throw new Error('Specified format "' + options.format + '" does not match any available codec.');
}
// use the encoder where specified in 'format'
outputSources = absoluteSources
.map(encodeSourcesWith.call(context, codec))
.map(insertAbstractSources)
.map(convertPathSep);
outputRoot = !!options.root && locateRootWith.call(context, codec)() || undefined;
// check for encode errors
throwErrors(context.resourcePath, outputSources.concat(outputRoot));
// commit the change
if (outputMap) {
outputMap.sources = outputSources;
outputMap.sourceRoot = outputRoot;
}
}
}
// debugging information
var isDebug = toRegExp(options.debug).test(context.resourcePath);
if (isDebug) {
console.log(debugMessage(context, {
input : inputSources,
absolute: absoluteSources,
output : outputSources,
root : outputRoot
}));
}
// complete
return inputMap ? outputMap : outputSources ? outputSources[0] : undefined;
function testNamedCodec(value) {
return (value.name === options.format);
}
function insertAbstractSources(value, i) {
return value || inputSources[i];
}
function convertPathSep(value) {
return (value instanceof Error) ? value : value.replace(/[\\\/]/g, options.sep);
}
}
module.exports = process;

View file

@ -0,0 +1,47 @@
'use strict';
var getFieldAsFn = require('./get-field-as-fn'),
CustomError = require('./get-error');
/**
* Locate the root for input sources using the given codec hash
* @throws Error Where the given codec is missing an encode function
* @this {object} A loader or compilation
* @param {{encode:function}} codec A single codec with an `encode` function
* @returns {function(string):string|Error} An encode function that takes an absolute path
*/
function locateRootWith(codec) {
/* jshint validthis:true */
var context = this,
root = getFieldAsFn('root')(codec);
if (!root) {
return new CustomError('Specified format does not support encoding (it lacks a "root" function)');
}
else {
return function locate() {
// call the root
var located;
try {
located = root.call(context);
}
catch (exception) {
return getNamedError(exception);
}
return located;
function getNamedError(details) {
var name = codec.name || '(unnamed)',
message = [
'Locating root with codec: ' + name,
details && (details.stack ? details.stack : details)
]
.filter(Boolean)
.join('\n');
return new Error(message);
}
};
}
}
module.exports = locateRootWith;

View file

@ -0,0 +1,37 @@
'use strict';
var assert = require('assert');
/**
* Reducer function that converts a codec list to a hash.
* @throws Error on bad codec
* @param {{name:string, decode:function, encode:function, root:function}} candidate A possible codec
* @returns True where an error is not thrown
*/
function testCodec(candidate) {
assert(
!!candidate && (typeof candidate === 'object'),
'Codec must be an object'
);
assert(
(typeof candidate.name === 'string') && /^[\w-]+$/.test(candidate.name),
'Codec.name must be a kebab-case string'
);
assert(
(typeof candidate.decode === 'function') && (candidate.decode.length === 1),
'Codec.decode must be a function that accepts a single source string'
);
assert(
(typeof candidate.encode === 'undefined') ||
((typeof candidate.encode === 'function') && (candidate.encode.length === 1)),
'Codec.encode must be a function that accepts a single absolute path string, or else be omitted'
);
assert(
(typeof candidate.root === 'undefined') ||
(typeof candidate.root === 'function') && (candidate.root.length === 0),
'Codec.root must be a function that accepts no arguments, or else be omitted'
);
return true;
}
module.exports = testCodec;

View file

@ -0,0 +1,30 @@
'use strict';
var getError = require('./get-error');
/**
* Where the given list is non-null and contains error instances then consolidate and throw
* @throws Error
* @param {string} resourcePath The path to the resource being processed
* @param {null|Array} candidates A possible Array with possible error elements
*/
function throwErrors(resourcePath, candidates) {
var errors = !!candidates && candidates
.filter(testIsError)
.map(getMessage);
var hasError = !!errors && errors.length;
if (hasError) {
throw getError(['For resource: ' + resourcePath].concat(errors).join('\n'));
}
function testIsError(candidate) {
return !!candidate && (typeof candidate === 'object') && (candidate instanceof Error);
}
function getMessage(error) {
return error.message;
}
}
module.exports = throwErrors;

View file

@ -0,0 +1,19 @@
'use strict';
var regexParser = require('regex-parser');
var REGEXP = /(\/?)(.+)\1([a-z]*)/i;
/**
* Parse the give value as a regular expression or give a pass-none expression where it is invalid
* @param {RegExp|string|*} value An existing expression, or its string representation, or degenerate value
* @returns {RegExp} The given expression or one matching the RegExp string else a pass-none expression
*/
function toRegExp(value) {
return ((typeof value === 'object') && (typeof value.test === 'function') && value) ||
((typeof value === 'string') && REGEXP.test(value) && regexParser(value)) ||
(/^true$|^$/.test(value) && /.*/) ||
/matchnone^/;
}
module.exports = toRegExp;