Debian Codegraph @

util/build/transforms/read.js

raw symbols SCIP
1 define([
2 "../buildControl",
3 "../fileUtils",
4 "../fs",
5 "../replace"
6 ], function(bc, fileUtils, fs, replace){
7 var
8 getFiletype = fileUtils.getFiletype,
9
10 encodingMap=
11 // map from file type to encoding
12 (bc.transformConfig.read && bc.transformConfig.read.encoding) || {
13 css:"utf8",
14 html:"utf8",
15 htm:"utf8",
16 js:"utf8",
17 json:"utf8",
18 asc:"utf8",
19 c:"utf8",
20 cpp:"utf8",
21 log:"utf8",
22 conf:"utf8",
23 text:"utf8",
24 txt:"utf8",
25 dtd:"utf8",
26 xml:"utf8",
27 png:undefined,
28 jpg:undefined,
29 jpeg:undefined,
30 gif:undefined
31 };
32
33 return function(resource, callback){
34 resource.getText = function(){
35 if(!this.replacementsApplied){
36 this.replacementsApplied = true;
37 if(bc.replacements[this.src]){
38 this.text = replace(this.text, bc.replacements[this.src]);
39 }
40 }
41 return this.text;
42 };
43
44 resource.setText = function(text){
45 resource.text = text;
46 resource.getText = function(){ return this.text; };
47 return text;
48 };
49
50 var filetype = getFiletype(resource.src, 1);
51 // the expression is a little odd since undefined is a legitimate encodingMap value
52 resource.encoding = resource.encoding ||(!(filetype in encodingMap) && "utf8") || encodingMap[filetype];
53 fs.readFile(resource.src, resource.encoding, function(err, data){
54 if(!err){
55 resource.text = data;
56 }
57 callback(resource, err);
58 });
59 return callback;
60 };
61 });