source: trip-planner-front/node_modules/adjust-sourcemap-loader/lib/process/index.js@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.5 KB
Line 
1'use strict';
2
3var debugMessage = require('./debug-message'),
4 toRegExp = require('./to-reg-exp'),
5 throwErrors = require('./throw-errors'),
6 decodeSourcesWith = require('./decode-sources-with'),
7 locateRootWith = require('./locate-root-with'),
8 encodeSourcesWith = require('./encode-sources-with'),
9 testCodec = require('./test-codec');
10
11var CODECS = require('../../codec');
12
13/**
14 * Process the given source-map per the given options.
15 * @param {{resourcePath:string, context:string, output:{path:string}}} context A loader or compilation
16 * @param {{debug:boolean, fail:boolean, format:string|boolean, root:string, codecs:object}} opt Options hash
17 * @param {object|string} sourceMapOrSource An incoming source-map or single source path
18 * @returns {undefined|object|string} An amended source-map or source path else undefined
19 */
20function process(context, opt, sourceMapOrSource) {
21
22 // default options
23 var options = Object.assign({
24 sep : '/',
25 debug : false,
26 fail : false,
27 format: false,
28 root : false,
29 codecs: CODECS
30 }, opt);
31
32 // validate codecs
33 var codecs = options.codecs
34 .filter(testCodec);
35
36 // determine what is present
37 var inputMap = !!sourceMapOrSource && (typeof sourceMapOrSource === 'object') && sourceMapOrSource,
38 inputPath = (typeof sourceMapOrSource === 'string') && sourceMapOrSource,
39 inputSources = inputMap && inputMap.sources || inputPath && [inputPath];
40
41 // what we need to produce
42 var absoluteSources,
43 outputSources,
44 outputRoot,
45 outputMap;
46
47 if (inputSources) {
48
49 // decode each source with the first valid codec
50 absoluteSources = inputSources
51 .map(decodeSourcesWith.call(context, codecs, options.fail));
52
53 // check for decode errors
54 throwErrors(context.resourcePath, absoluteSources);
55
56 // output map is a copy unless absent or we are removing
57 outputMap = (!inputMap || (options.format === 'remove')) ? undefined : Object.assign({}, inputMap);
58
59 // some change in format
60 if (options.format) {
61
62 // find the specified codec in the codecs list
63 var codec = codecs
64 .filter(testNamedCodec)
65 .pop();
66
67 if (!codec) {
68 throw new Error('Specified format "' + options.format + '" does not match any available codec.');
69 }
70
71 // use the encoder where specified in 'format'
72 outputSources = absoluteSources
73 .map(encodeSourcesWith.call(context, codec))
74 .map(insertAbstractSources)
75 .map(convertPathSep);
76
77 outputRoot = !!options.root && locateRootWith.call(context, codec)() || undefined;
78
79 // check for encode errors
80 throwErrors(context.resourcePath, outputSources.concat(outputRoot));
81
82 // commit the change
83 if (outputMap) {
84 outputMap.sources = outputSources;
85 outputMap.sourceRoot = outputRoot;
86 }
87 }
88 }
89
90 // debugging information
91 var isDebug = toRegExp(options.debug).test(context.resourcePath);
92 if (isDebug) {
93 console.log(debugMessage(context, {
94 input : inputSources,
95 absolute: absoluteSources,
96 output : outputSources,
97 root : outputRoot
98 }));
99 }
100
101 // complete
102 return inputMap ? outputMap : outputSources ? outputSources[0] : undefined;
103
104 function testNamedCodec(value) {
105 return (value.name === options.format);
106 }
107
108 function insertAbstractSources(value, i) {
109 return value || inputSources[i];
110 }
111
112 function convertPathSep(value) {
113 return (value instanceof Error) ? value : value.replace(/[\\\/]/g, options.sep);
114 }
115}
116
117module.exports = process;
Note: See TracBrowser for help on using the repository browser.