Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80668 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
var fs = require('fs');
17
var Resource = require('../resource/Resource');
18
var MessageList = require('../MessageList');
19
20
/**
21
* @class Base ResourceLoader. ResourceLoader works together with
22
* MapUpdateTask to analyze different resource type. Each loader type can load
23
* one or more resource types. Each loader can accept a number of options
24
* to configure loading process. By default loader will read the file's code
25
* and parse it to extract useful information.
26
*/
27
function ResourceLoader(options) {
28
this.options = options || {};
29
}
30
ResourceLoader.prototype.path = __filename;
31
32
ResourceLoader.fromObject = function(object) {
33
var C = require(object.path);
34
return new C(object.options);
35
};
36
37
ResourceLoader.prototype.toObject = function() {
38
return {
39
path: this.path,
40
options: this.options
41
};
42
};
43
44
ResourceLoader.prototype.getResourceTypes = function() {
45
return [Resource];
46
};
47
48
ResourceLoader.prototype.getExtensions = function() {
49
return [];
50
};
51
52
/**
53
* Creates a new resource for a given path. Can be overridden in a sublcass
54
* to perform different loading
55
*
56
* @static
57
* @param {String} path
58
* @param {ProjectConfiguration} configuration
59
* @param {Function} callback
60
*/
61
ResourceLoader.prototype.loadFromPath =
62
function(path, configuration, callback) {
63
var me = this;
64
var messages = MessageList.create();
65
66
// node's 0.6 async I/O adds more overhead than the actuall reading time
67
// when disk cache is warm. Use sync version
68
// var sourceCode;
69
// try {
70
// sourceCode = fs.readFileSync(path, 'utf-8');
71
// } catch(e) {
72
// messages.addClowntownError(this.path, 'resource', e.toString());
73
// callback(messages, null);
74
// return;
75
// }
76
// process.nextTick(function() {
77
// me.loadFromSource(
78
// path,
79
// configuration,
80
// sourceCode || '',
81
// messages,
82
// callback);
83
// });
84
85
fs.readFile(path, 'utf-8', function(err, sourceCode) {
86
if (err) {
87
console.error('Error reading file: `' + path + '`');
88
throw err;
89
} else {
90
me.loadFromSource(
91
path,
92
configuration,
93
sourceCode || '',
94
messages,
95
callback);
96
}
97
});
98
};
99
100
/**
101
* Initialize a resource with the source code and configuration
102
* Loader can parse, gzip, minify the source code to build the resulting
103
* Resource value object
104
*
105
* TODO: @jordwalke: Actually cache the file contents here so that future
106
* packaging stages can read the cached file!
107
*
108
* @protected
109
* @param {String} path resource being built
110
* @param {ProjectConfiguration} configuration configuration for the path
111
* @param {String} sourceCode
112
* @param {MessageList} messages
113
* @param {Function} callback
114
*/
115
ResourceLoader.prototype.loadFromSource =
116
function(path, configuration, sourceCode, messages, callback) {
117
var resource = new Resource(path);
118
process.nextTick(function() { callback(messages, resource); }, 10);
119
};
120
121
/**
122
* Checks if resource can parse the given path. Map builder will match
123
* all available resource types to find the one that can parse the given path
124
* Base resource always returns true, since it can potentially parse any file,
125
* though without much value
126
* @static
127
*
128
* @param {String} path
129
* @return {Boolean}
130
*/
131
ResourceLoader.prototype.matchPath = function(path) {
132
return true;
133
};
134
135
/**
136
* Post process is called after the map is updated but before the update
137
* task is complete.
138
* Can be used to resolve dependencies or to bulk process all loaded resources
139
* @param {ResourceMap} map
140
* @param {Array.<Resource>} resources
141
* @param {Function} callback
142
*/
143
ResourceLoader.prototype.postProcess = function(map, resources, callback) {
144
process.nextTick(function() {
145
callback(MessageList.create());
146
});
147
};
148
149
150
module.exports = ResourceLoader;
151
152