render.js 2.9 KB

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