index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. var fs = require('fs'),
  2. path = require('path');
  3. require('./extensions');
  4. /**
  5. * Get binding
  6. *
  7. * @api private
  8. */
  9. function getBinding() {
  10. var candidates = [
  11. path.join(__dirname, '..', 'build', 'Debug', 'binding.node'),
  12. path.join(__dirname, '..', 'build', 'Release', 'binding.node'),
  13. path.join(__dirname, '..', 'vendor', process.sassBinaryName, 'binding.node')
  14. ];
  15. var candidate = candidates.filter(fs.existsSync).shift();
  16. if (!candidate) {
  17. throw new Error('`libsass` bindings not found. Try reinstalling `node-sass`?');
  18. }
  19. return candidate;
  20. }
  21. /**
  22. * Get outfile
  23. *
  24. * @param {Object} options
  25. * @api private
  26. */
  27. function getOutFile(options) {
  28. var file = options.file;
  29. var outFile = options.outFile;
  30. if (!outFile || typeof outFile !== 'string' || (!options.data && !file)) {
  31. return null;
  32. }
  33. if (path.resolve(outFile) === path.normalize(outFile).replace(/(.+)([\/|\\])$/, '$1')) {
  34. return outFile;
  35. }
  36. return path.resolve(path.dirname(file), outFile);
  37. }
  38. /**
  39. * Get stats
  40. *
  41. * @param {Object} options
  42. * @api private
  43. */
  44. function getStats(options) {
  45. var stats = {};
  46. stats.entry = options.file || 'data';
  47. stats.start = Date.now();
  48. return stats;
  49. }
  50. /**
  51. * End stats
  52. *
  53. * @param {Object} stats
  54. * @param {Object} sourceMap
  55. * @api private
  56. */
  57. function endStats(stats) {
  58. stats.end = Date.now();
  59. stats.duration = stats.end - stats.start;
  60. return stats;
  61. }
  62. /**
  63. * Get style
  64. *
  65. * @param {Object} options
  66. * @api private
  67. */
  68. function getStyle(options) {
  69. var style = options.output_style || options.outputStyle;
  70. var styles = {
  71. nested: 0,
  72. expanded: 1,
  73. compact: 2,
  74. compressed: 3
  75. };
  76. return styles[style];
  77. }
  78. /**
  79. * Get source map
  80. *
  81. * @param {Object} options
  82. * @api private
  83. */
  84. function getSourceMap(options) {
  85. var file = options.file;
  86. var outFile = options.outFile;
  87. var sourceMap = options.sourceMap;
  88. if (sourceMap) {
  89. if (typeof sourceMap !== 'string') {
  90. sourceMap = outFile ? outFile + '.map' : '';
  91. } else if (outFile) {
  92. sourceMap = path.resolve(path.dirname(file), sourceMap);
  93. }
  94. }
  95. return sourceMap;
  96. }
  97. /**
  98. * Get options
  99. *
  100. * @param {Object} options
  101. * @api private
  102. */
  103. function getOptions(options) {
  104. options = options || {};
  105. options.comments = options.source_comments || options.sourceComments || false;
  106. options.data = options.data || null;
  107. options.file = options.file || null;
  108. options.imagePath = options.image_path || options.imagePath || '';
  109. options.outFile = getOutFile(options);
  110. options.paths = (options.include_paths || options.includePaths || []).join(path.delimiter);
  111. options.precision = parseInt(options.precision) || 5;
  112. options.sourceMap = getSourceMap(options);
  113. options.style = getStyle(options) || 0;
  114. if (options.imagePath && typeof options.imagePath !== 'string') {
  115. throw new Error('`imagePath` needs to be a string');
  116. }
  117. var error = options.error;
  118. var success = options.success;
  119. options.error = function(err, code) {
  120. err = JSON.parse(err);
  121. err.code = code;
  122. if (error) {
  123. error(err);
  124. }
  125. };
  126. options.success = function() {
  127. var result = options.result;
  128. var stats = endStats(result.stats);
  129. if (success) {
  130. success({
  131. css: result.css,
  132. map: result.map,
  133. stats: stats
  134. });
  135. }
  136. };
  137. delete options.image_path;
  138. delete options.include_paths;
  139. delete options.includePaths;
  140. delete options.source_comments;
  141. delete options.sourceComments;
  142. options.result = {
  143. stats: getStats(options)
  144. };
  145. return options;
  146. }
  147. /**
  148. * Require binding
  149. */
  150. var binding = require(getBinding());
  151. /**
  152. * Render
  153. *
  154. * @param {Object} options
  155. * @api public
  156. */
  157. module.exports.render = function(options) {
  158. options = getOptions(options);
  159. var importer = options.importer;
  160. if (importer) {
  161. options.importer = function(file, prev, key) {
  162. function done(data) {
  163. console.log(data); // ugly hack
  164. binding.importedCallback({
  165. index: key,
  166. objectLiteral: data
  167. });
  168. }
  169. var result = importer(file, prev, done);
  170. if (result) {
  171. done(result);
  172. }
  173. };
  174. }
  175. options.data ? binding.render(options) : binding.renderFile(options);
  176. };
  177. /**
  178. * Render sync
  179. *
  180. * @param {Object} options
  181. * @api public
  182. */
  183. module.exports.renderSync = function(options) {
  184. options = getOptions(options);
  185. var importer = options.importer;
  186. if (importer) {
  187. options.importer = function(file, prev) {
  188. return { objectLiteral: importer(file, prev) };
  189. };
  190. }
  191. var status = options.data ? binding.renderSync(options) : binding.renderFileSync(options);
  192. var result = options.result;
  193. if(status) {
  194. result.stats = endStats(result.stats);
  195. return result;
  196. }
  197. };
  198. /**
  199. * API Info
  200. *
  201. * @api public
  202. */
  203. module.exports.info = function() {
  204. var package = require('../package.json');
  205. return [
  206. 'node-sass version: ' + package.version,
  207. 'libsass version: ' + package.libsass
  208. ].join('\n');
  209. };