index.js 5.2 KB

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