render.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*!
  2. * node-sass: lib/render.js
  3. */
  4. var chalk = require('chalk'),
  5. fs = require('fs'),
  6. mkdirp = require('mkdirp'),
  7. path = require('path'),
  8. sass = require('./');
  9. /**
  10. * Render
  11. *
  12. * @param {Object} options
  13. * @param {Object} emitter
  14. * @api public
  15. */
  16. module.exports = function(options, emitter) {
  17. var renderOptions = {
  18. includePaths: options.includePath,
  19. omitSourceMapUrl: options.omitSourceMapUrl,
  20. indentedSyntax: options.indentedSyntax,
  21. outFile: options.dest,
  22. outputStyle: options.outputStyle,
  23. precision: options.precision,
  24. sourceComments: options.sourceComments,
  25. sourceMapEmbed: options.sourceMapEmbed,
  26. sourceMapContents: options.sourceMapContents,
  27. sourceMap: options.sourceMap,
  28. sourceMapRoot: options.sourceMapRoot,
  29. importer: options.importer,
  30. functions: options.functions,
  31. indentWidth: options.indentWidth,
  32. indentType: options.indentType,
  33. linefeed: options.linefeed
  34. };
  35. if (options.data) {
  36. renderOptions.data = options.data;
  37. } else if (options.src) {
  38. renderOptions.file = options.src;
  39. }
  40. var sourceMap = options.sourceMap;
  41. var destination = options.dest;
  42. var stdin = options.stdin;
  43. var success = function(result) {
  44. var todo = 1;
  45. var done = function() {
  46. if (--todo <= 0) {
  47. emitter.emit('done');
  48. }
  49. };
  50. if (!destination || stdin) {
  51. emitter.emit('log', result.css.toString());
  52. if (sourceMap && !options.sourceMapEmbed) {
  53. emitter.emit('log', result.map.toString());
  54. }
  55. return done();
  56. }
  57. emitter.emit('warn', chalk.green('Rendering Complete, saving .css file...'));
  58. mkdirp(path.dirname(destination), function(err) {
  59. if (err) {
  60. return emitter.emit('error', chalk.red(err));
  61. }
  62. fs.writeFile(destination, result.css.toString(), function(err) {
  63. if (err) {
  64. return emitter.emit('error', chalk.red(err));
  65. }
  66. emitter.emit('warn', chalk.green('Wrote CSS to ' + destination));
  67. emitter.emit('write', err, destination, result.css.toString());
  68. done();
  69. });
  70. });
  71. if (sourceMap) {
  72. todo++;
  73. mkdirp(path.dirname(sourceMap), function(err) {
  74. if (err) {
  75. return emitter.emit('error', chalk.red(err));
  76. }
  77. fs.writeFile(sourceMap, result.map, function(err) {
  78. if (err) {
  79. return emitter.emit('error', chalk.red('Error' + err));
  80. }
  81. emitter.emit('warn', chalk.green('Wrote Source Map to ' + sourceMap));
  82. emitter.emit('write-source-map', err, sourceMap, result.map);
  83. done();
  84. });
  85. });
  86. }
  87. emitter.emit('render', result.css.toString());
  88. };
  89. var error = function(error) {
  90. emitter.emit('error', chalk.red(JSON.stringify(error, null, 2)));
  91. };
  92. var renderCallback = function(err, result) {
  93. if (err) {
  94. error(err);
  95. }
  96. else {
  97. success(result);
  98. }
  99. };
  100. sass.render(renderOptions, renderCallback);
  101. };