image-size.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var dimension_1 = tslib_1.__importDefault(require("../less/tree/dimension"));
  5. var expression_1 = tslib_1.__importDefault(require("../less/tree/expression"));
  6. var function_registry_1 = tslib_1.__importDefault(require("./../less/functions/function-registry"));
  7. exports.default = (function (environment) {
  8. function imageSize(functionContext, filePathNode) {
  9. var filePath = filePathNode.value;
  10. var currentFileInfo = functionContext.currentFileInfo;
  11. var currentDirectory = currentFileInfo.rewriteUrls ?
  12. currentFileInfo.currentDirectory : currentFileInfo.entryPath;
  13. var fragmentStart = filePath.indexOf('#');
  14. if (fragmentStart !== -1) {
  15. filePath = filePath.slice(0, fragmentStart);
  16. }
  17. var fileManager = environment.getFileManager(filePath, currentDirectory, functionContext.context, environment, true);
  18. if (!fileManager) {
  19. throw {
  20. type: 'File',
  21. message: "Can not set up FileManager for " + filePathNode
  22. };
  23. }
  24. var fileSync = fileManager.loadFileSync(filePath, currentDirectory, functionContext.context, environment);
  25. if (fileSync.error) {
  26. throw fileSync.error;
  27. }
  28. var sizeOf = require('image-size');
  29. return sizeOf(fileSync.filename);
  30. }
  31. var imageFunctions = {
  32. 'image-size': function (filePathNode) {
  33. var size = imageSize(this, filePathNode);
  34. return new expression_1.default([
  35. new dimension_1.default(size.width, 'px'),
  36. new dimension_1.default(size.height, 'px')
  37. ]);
  38. },
  39. 'image-width': function (filePathNode) {
  40. var size = imageSize(this, filePathNode);
  41. return new dimension_1.default(size.width, 'px');
  42. },
  43. 'image-height': function (filePathNode) {
  44. var size = imageSize(this, filePathNode);
  45. return new dimension_1.default(size.height, 'px');
  46. }
  47. };
  48. function_registry_1.default.addMultiple(imageFunctions);
  49. });
  50. //# sourceMappingURL=image-size.js.map