source: imaps-frontend/node_modules/clean-css/lib/reader/load-original-sources.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 3.8 KB
Line 
1var fs = require('fs');
2var path = require('path');
3
4var isAllowedResource = require('./is-allowed-resource');
5
6var hasProtocol = require('../utils/has-protocol');
7var isRemoteResource = require('../utils/is-remote-resource');
8
9function loadOriginalSources(context, callback) {
10 var loadContext = {
11 callback: callback,
12 fetch: context.options.fetch,
13 index: 0,
14 inline: context.options.inline,
15 inlineRequest: context.options.inlineRequest,
16 inlineTimeout: context.options.inlineTimeout,
17 localOnly: context.localOnly,
18 rebaseTo: context.options.rebaseTo,
19 sourcesContent: context.sourcesContent,
20 uriToSource: uriToSourceMapping(context.inputSourceMapTracker.all()),
21 warnings: context.warnings
22 };
23
24 return context.options.sourceMap && context.options.sourceMapInlineSources
25 ? doLoadOriginalSources(loadContext)
26 : callback();
27}
28
29function uriToSourceMapping(allSourceMapConsumers) {
30 var mapping = {};
31 var consumer;
32 var uri;
33 var source;
34 var i, l;
35
36 for (source in allSourceMapConsumers) {
37 consumer = allSourceMapConsumers[source];
38
39 for (i = 0, l = consumer.sources.length; i < l; i++) {
40 uri = consumer.sources[i];
41 source = consumer.sourceContentFor(uri, true);
42
43 mapping[uri] = source;
44 }
45 }
46
47 return mapping;
48}
49
50function doLoadOriginalSources(loadContext) {
51 var uris = Object.keys(loadContext.uriToSource);
52 var uri;
53 var source;
54 var total;
55
56 for (total = uris.length; loadContext.index < total; loadContext.index++) {
57 uri = uris[loadContext.index];
58 source = loadContext.uriToSource[uri];
59
60 if (source) {
61 loadContext.sourcesContent[uri] = source;
62 } else {
63 return loadOriginalSource(uri, loadContext);
64 }
65 }
66
67 return loadContext.callback();
68}
69
70function loadOriginalSource(uri, loadContext) {
71 var content;
72
73 if (isRemoteResource(uri)) {
74 return loadOriginalSourceFromRemoteUri(uri, loadContext, function(content) {
75 loadContext.index++;
76 loadContext.sourcesContent[uri] = content;
77 return doLoadOriginalSources(loadContext);
78 });
79 }
80 content = loadOriginalSourceFromLocalUri(uri, loadContext);
81 loadContext.index++;
82 loadContext.sourcesContent[uri] = content;
83 return doLoadOriginalSources(loadContext);
84}
85
86function loadOriginalSourceFromRemoteUri(uri, loadContext, whenLoaded) {
87 var isAllowed = isAllowedResource(uri, true, loadContext.inline);
88 var isRuntimeResource = !hasProtocol(uri);
89
90 if (loadContext.localOnly) {
91 loadContext.warnings.push('Cannot fetch remote resource from "' + uri + '" as no callback given.');
92 return whenLoaded(null);
93 } if (isRuntimeResource) {
94 loadContext.warnings.push('Cannot fetch "' + uri + '" as no protocol given.');
95 return whenLoaded(null);
96 } if (!isAllowed) {
97 loadContext.warnings.push('Cannot fetch "' + uri + '" as resource is not allowed.');
98 return whenLoaded(null);
99 }
100
101 loadContext.fetch(uri, loadContext.inlineRequest, loadContext.inlineTimeout, function(error, content) {
102 if (error) {
103 loadContext.warnings.push('Missing original source at "' + uri + '" - ' + error);
104 }
105
106 whenLoaded(content);
107 });
108}
109
110function loadOriginalSourceFromLocalUri(relativeUri, loadContext) {
111 var isAllowed = isAllowedResource(relativeUri, false, loadContext.inline);
112 var absoluteUri = path.resolve(loadContext.rebaseTo, relativeUri);
113
114 if (!fs.existsSync(absoluteUri) || !fs.statSync(absoluteUri).isFile()) {
115 loadContext.warnings.push('Ignoring local source map at "' + absoluteUri + '" as resource is missing.');
116 return null;
117 } if (!isAllowed) {
118 loadContext.warnings.push('Cannot fetch "' + absoluteUri + '" as resource is not allowed.');
119 return null;
120 }
121
122 var result = fs.readFileSync(absoluteUri, 'utf8');
123 if (result.charCodeAt(0) === 65279) {
124 result = result.substring(1);
125 }
126 return result;
127}
128
129module.exports = loadOriginalSources;
Note: See TracBrowser for help on using the repository browser.