Spaces:
Sleeping
Sleeping
File size: 3,617 Bytes
cc651f6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
'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;
|