react / wstein / node_modules / jest-cli / node_modules / node-haste / lib / loader / ResourceLoader.js
80668 views/**1* Copyright 2013 Facebook, Inc.2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/15var fs = require('fs');16var Resource = require('../resource/Resource');17var MessageList = require('../MessageList');1819/**20* @class Base ResourceLoader. ResourceLoader works together with21* MapUpdateTask to analyze different resource type. Each loader type can load22* one or more resource types. Each loader can accept a number of options23* to configure loading process. By default loader will read the file's code24* and parse it to extract useful information.25*/26function ResourceLoader(options) {27this.options = options || {};28}29ResourceLoader.prototype.path = __filename;3031ResourceLoader.fromObject = function(object) {32var C = require(object.path);33return new C(object.options);34};3536ResourceLoader.prototype.toObject = function() {37return {38path: this.path,39options: this.options40};41};4243ResourceLoader.prototype.getResourceTypes = function() {44return [Resource];45};4647ResourceLoader.prototype.getExtensions = function() {48return [];49};5051/**52* Creates a new resource for a given path. Can be overridden in a sublcass53* to perform different loading54*55* @static56* @param {String} path57* @param {ProjectConfiguration} configuration58* @param {Function} callback59*/60ResourceLoader.prototype.loadFromPath =61function(path, configuration, callback) {62var me = this;63var messages = MessageList.create();6465// node's 0.6 async I/O adds more overhead than the actuall reading time66// when disk cache is warm. Use sync version67// var sourceCode;68// try {69// sourceCode = fs.readFileSync(path, 'utf-8');70// } catch(e) {71// messages.addClowntownError(this.path, 'resource', e.toString());72// callback(messages, null);73// return;74// }75// process.nextTick(function() {76// me.loadFromSource(77// path,78// configuration,79// sourceCode || '',80// messages,81// callback);82// });8384fs.readFile(path, 'utf-8', function(err, sourceCode) {85if (err) {86console.error('Error reading file: `' + path + '`');87throw err;88} else {89me.loadFromSource(90path,91configuration,92sourceCode || '',93messages,94callback);95}96});97};9899/**100* Initialize a resource with the source code and configuration101* Loader can parse, gzip, minify the source code to build the resulting102* Resource value object103*104* TODO: @jordwalke: Actually cache the file contents here so that future105* packaging stages can read the cached file!106*107* @protected108* @param {String} path resource being built109* @param {ProjectConfiguration} configuration configuration for the path110* @param {String} sourceCode111* @param {MessageList} messages112* @param {Function} callback113*/114ResourceLoader.prototype.loadFromSource =115function(path, configuration, sourceCode, messages, callback) {116var resource = new Resource(path);117process.nextTick(function() { callback(messages, resource); }, 10);118};119120/**121* Checks if resource can parse the given path. Map builder will match122* all available resource types to find the one that can parse the given path123* Base resource always returns true, since it can potentially parse any file,124* though without much value125* @static126*127* @param {String} path128* @return {Boolean}129*/130ResourceLoader.prototype.matchPath = function(path) {131return true;132};133134/**135* Post process is called after the map is updated but before the update136* task is complete.137* Can be used to resolve dependencies or to bulk process all loaded resources138* @param {ResourceMap} map139* @param {Array.<Resource>} resources140* @param {Function} callback141*/142ResourceLoader.prototype.postProcess = function(map, resources, callback) {143process.nextTick(function() {144callback(MessageList.create());145});146};147148149module.exports = ResourceLoader;150151152