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 inherits = require('util').inherits;
17
18
var docblock = require('../parse/docblock');
19
var extract = require('../parse/extract');
20
var ResourceLoader = require('./ResourceLoader');
21
var JSBench = require('../resource/JSBench');
22
23
24
/**
25
* @class Loads and parses __benchmarks__ / *.js files
26
*
27
* @extends {ResourceLoader}
28
*/
29
function JSBenchLoader(options) {
30
ResourceLoader.call(this, options);
31
32
this.pathRe = this.options.matchSubDirs ?
33
/(?:[\\/]|^)__benchmarks__[\\/](.+)\.js$/ :
34
/(?:[\\/]|^)__benchmarks__[\\/]([^\/]+)\.js$/;
35
}
36
inherits(JSBenchLoader, ResourceLoader);
37
JSBenchLoader.prototype.path = __filename;
38
39
JSBenchLoader.prototype.getResourceTypes = function() {
40
return [JSBench];
41
};
42
43
JSBenchLoader.prototype.getExtensions = function() {
44
return ['.js'];
45
};
46
47
48
/**
49
* Initialize a resource with the source code and configuration
50
* Loader can parse, gzip, minify the source code to build the resulting
51
* Resource value object
52
*
53
* @protected
54
* @param {String} path resource being built
55
* @param {ProjectConfiguration} configuration configuration for the path
56
* @param {String} sourceCode
57
* @param {Function} callback
58
*/
59
JSBenchLoader.prototype.loadFromSource =
60
function(path, configuration, sourceCode, messages, callback) {
61
62
var bench = new JSBench(path);
63
64
docblock.parse(docblock.extract(sourceCode)).forEach(function(pair) {
65
var name = pair[0];
66
var value = pair[1];
67
68
switch (name) {
69
case 'emails':
70
bench.contacts = value.split(/\s/);
71
break;
72
default:
73
// do nothing
74
}
75
});
76
77
bench.id = configuration && configuration.resolveID(path);
78
if (!bench.id) {
79
bench.id = path.match(this.pathRe)[1];
80
}
81
bench.requiredModules = extract.requireCalls(sourceCode);
82
callback(messages, bench);
83
};
84
85
/**
86
* Only match __benchmarks__ / *.js files
87
* @static
88
* @param {String} filePath
89
* @return {Boolean}
90
*/
91
JSBenchLoader.prototype.matchPath = function(filePath) {
92
return this.pathRe.test(filePath);
93
};
94
95
96
module.exports = JSBenchLoader;
97
98