index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. 'use strict';
  2. var gutil = require('gulp-util');
  3. var through = require('through2');
  4. var clonedeep = require('lodash.clonedeep');
  5. var path = require('path');
  6. var applySourceMap = require('vinyl-sourcemaps-apply');
  7. var PLUGIN_NAME = 'gulp-sass';
  8. //////////////////////////////
  9. // Main Gulp Sass function
  10. //////////////////////////////
  11. var gulpSass = function gulpSass(options, sync) {
  12. return through.obj(function(file, enc, cb) {
  13. var opts,
  14. filePush,
  15. errorM,
  16. callback,
  17. result;
  18. if (file.isNull()) {
  19. return cb(null, file);
  20. }
  21. if (file.isStream()) {
  22. return cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
  23. }
  24. if (path.basename(file.path).indexOf('_') === 0) {
  25. return cb();
  26. }
  27. if (!file.contents.length) {
  28. file.path = gutil.replaceExtension(file.path, '.css');
  29. return cb(null, file);
  30. }
  31. opts = clonedeep(options || {});
  32. opts.data = file.contents.toString();
  33. // we set the file path here so that libsass can correctly resolve import paths
  34. opts.file = file.path;
  35. // Ensure `indentedSyntax` is true if a `.sass` file
  36. if (path.extname(file.path) === '.sass') {
  37. opts.indentedSyntax = true;
  38. }
  39. // Ensure file's parent directory in the include path
  40. if (opts.includePaths) {
  41. if (typeof opts.includePaths === 'string') {
  42. opts.includePaths = [opts.includePaths];
  43. }
  44. }
  45. else {
  46. opts.includePaths = [];
  47. }
  48. opts.includePaths.unshift(path.dirname(file.path));
  49. // Generate Source Maps if plugin source-map present
  50. if (file.sourceMap) {
  51. opts.sourceMap = file.path;
  52. opts.omitSourceMapUrl = true;
  53. opts.sourceMapContents = true;
  54. }
  55. //////////////////////////////
  56. // Handles returning the file to the stream
  57. //////////////////////////////
  58. filePush = function filePush(sassObj) {
  59. var sassMap,
  60. sassMapFile,
  61. sassFileSrc,
  62. sassFileSrcPath,
  63. sourceFileIndex;
  64. // Build Source Maps!
  65. if (sassObj.map) {
  66. // Transform map into JSON
  67. sassMap = JSON.parse(sassObj.map.toString());
  68. // Grab the stdout and transform it into stdin
  69. sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin');
  70. // Grab the base file name that's being worked on
  71. sassFileSrc = file.relative;
  72. // Grab the path portion of the file that's being worked on
  73. sassFileSrcPath = path.dirname(sassFileSrc);
  74. if (sassFileSrcPath) {
  75. //Prepend the path to all files in the sources array except the file that's being worked on
  76. sourceFileIndex = sassMap.sources.indexOf(sassMapFile);
  77. sassMap.sources = sassMap.sources.map(function(source, index) {
  78. return (index === sourceFileIndex) ? source : path.join(sassFileSrcPath, source);
  79. });
  80. }
  81. // Remove 'stdin' from souces and replace with filenames!
  82. sassMap.sources = sassMap.sources.filter(function(src) {
  83. if (src !== 'stdin') {
  84. return src;
  85. }
  86. });
  87. // Replace the map file with the original file name (but new extension)
  88. sassMap.file = gutil.replaceExtension(sassFileSrc, '.css');
  89. // Apply the map
  90. applySourceMap(file, sassMap);
  91. }
  92. file.contents = sassObj.css;
  93. file.path = gutil.replaceExtension(file.path, '.css');
  94. cb(null, file);
  95. };
  96. //////////////////////////////
  97. // Handles error message
  98. //////////////////////////////
  99. errorM = function errorM(error) {
  100. var relativePath = '',
  101. filePath = error.file === 'stdin' ? file.path : error.file,
  102. message = '';
  103. filePath = filePath ? filePath : file.path;
  104. relativePath = path.relative(process.cwd(), filePath);
  105. message += gutil.colors.underline(relativePath) + '\n';
  106. message += error.formatted;
  107. error.messageFormatted = message;
  108. error.messageOriginal = error.message;
  109. error.message = gutil.colors.stripColor(message);
  110. error.relativePath = relativePath;
  111. return cb(new gutil.PluginError(
  112. PLUGIN_NAME, error
  113. ));
  114. };
  115. if (sync !== true) {
  116. //////////////////////////////
  117. // Async Sass render
  118. //////////////////////////////
  119. callback = function(error, obj) {
  120. if (error) {
  121. return errorM(error);
  122. }
  123. filePush(obj);
  124. };
  125. gulpSass.compiler.render(opts, callback);
  126. }
  127. else {
  128. //////////////////////////////
  129. // Sync Sass render
  130. //////////////////////////////
  131. try {
  132. result = gulpSass.compiler.renderSync(opts);
  133. filePush(result);
  134. }
  135. catch (error) {
  136. return errorM(error);
  137. }
  138. }
  139. });
  140. };
  141. //////////////////////////////
  142. // Sync Sass render
  143. //////////////////////////////
  144. gulpSass.sync = function sync(options) {
  145. return gulpSass(options, true);
  146. };
  147. //////////////////////////////
  148. // Log errors nicely
  149. //////////////////////////////
  150. gulpSass.logError = function logError(error) {
  151. var message = new gutil.PluginError('sass', error.messageFormatted).toString();
  152. process.stderr.write(message + '\n');
  153. this.emit('end');
  154. };
  155. //////////////////////////////
  156. // Store compiler in a prop
  157. //////////////////////////////
  158. gulpSass.compiler = require('node-sass');
  159. module.exports = gulpSass;