|
|
'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'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function process(context, opt, sourceMapOrSource) { |
|
|
|
|
|
|
|
|
var options = Object.assign({ |
|
|
sep : '/', |
|
|
debug : false, |
|
|
fail : false, |
|
|
format: false, |
|
|
root : false, |
|
|
codecs: CODECS |
|
|
}, opt); |
|
|
|
|
|
|
|
|
var codecs = options.codecs |
|
|
.filter(testCodec); |
|
|
|
|
|
|
|
|
var inputMap = !!sourceMapOrSource && (typeof sourceMapOrSource === 'object') && sourceMapOrSource, |
|
|
inputPath = (typeof sourceMapOrSource === 'string') && sourceMapOrSource, |
|
|
inputSources = inputMap && inputMap.sources || inputPath && [inputPath]; |
|
|
|
|
|
|
|
|
var absoluteSources, |
|
|
outputSources, |
|
|
outputRoot, |
|
|
outputMap; |
|
|
|
|
|
if (inputSources) { |
|
|
|
|
|
|
|
|
absoluteSources = inputSources |
|
|
.map(decodeSourcesWith.call(context, codecs, options.fail)); |
|
|
|
|
|
|
|
|
throwErrors(context.resourcePath, absoluteSources); |
|
|
|
|
|
|
|
|
outputMap = (!inputMap || (options.format === 'remove')) ? undefined : Object.assign({}, inputMap); |
|
|
|
|
|
|
|
|
if (options.format) { |
|
|
|
|
|
|
|
|
var codec = codecs |
|
|
.filter(testNamedCodec) |
|
|
.pop(); |
|
|
|
|
|
if (!codec) { |
|
|
throw new Error('Specified format "' + options.format + '" does not match any available codec.'); |
|
|
} |
|
|
|
|
|
|
|
|
outputSources = absoluteSources |
|
|
.map(encodeSourcesWith.call(context, codec)) |
|
|
.map(insertAbstractSources) |
|
|
.map(convertPathSep); |
|
|
|
|
|
outputRoot = !!options.root && locateRootWith.call(context, codec)() || undefined; |
|
|
|
|
|
|
|
|
throwErrors(context.resourcePath, outputSources.concat(outputRoot)); |
|
|
|
|
|
|
|
|
if (outputMap) { |
|
|
outputMap.sources = outputSources; |
|
|
outputMap.sourceRoot = outputRoot; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var isDebug = toRegExp(options.debug).test(context.resourcePath); |
|
|
if (isDebug) { |
|
|
console.log(debugMessage(context, { |
|
|
input : inputSources, |
|
|
absolute: absoluteSources, |
|
|
output : outputSources, |
|
|
root : outputRoot |
|
|
})); |
|
|
} |
|
|
|
|
|
|
|
|
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; |
|
|
|