react / wstein / node_modules / jest-cli / node_modules / node-haste / lib / loader / ImageLoader.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 inherits = require('util').inherits;16var childProcess = require('child_process');17var fs = require('fs');1819var ResourceLoader = require('./ResourceLoader');20var ImageResource = require('../resource/Image');21var MessageList = require('../MessageList');22var getImageSize = require('../parse/getImageSize');232425/**26* @class Loads and parses __mocks__ / *.js files27*28* @extends {ResourceLoader}29*/30function ImageLoader(options) {31ResourceLoader.call(this, options);32}33inherits(ImageLoader, ResourceLoader);34ImageLoader.prototype.path = __filename;3536ImageLoader.prototype.getResourceTypes = function() {37return [ImageResource];38};3940ImageLoader.prototype.getExtensions = function() {41return ['.jpg', '.png', '.gif'];42};434445/**46* Creates a new resource for a given path.47*48* @protected49* @param {String} path resource being built50* @param {ProjectConfiguration} configuration configuration for the path51* @param {Function} callback52*/53ImageLoader.prototype.loadFromPath =54function(path, configuration, callback) {5556var image = new ImageResource(path);57var messages = MessageList.create();58image.id = path;59fs.readFile(path, function(err, buffer) {60image.networkSize = buffer.length;61var size = getImageSize(buffer);62if (size) {63image.width = size.width;64image.height = size.height;65}66callback(messages, image);67});68};697071var re = /\.(jpg|gif|png)$/;72/**73* Only match __mocks__ / *.js files74* @static75* @param {String} filePath76* @return {Boolean}77*/78ImageLoader.prototype.matchPath = function(filePath) {79return re.test(filePath);80};818283module.exports = ImageLoader;848586