index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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-dart-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. if (file.stat) {
  86. file.stat.atime = file.stat.mtime = file.stat.ctime = new Date(); // eslint-disable-line
  87. }
  88. cb(null, file);
  89. };
  90. //////////////////////////////
  91. // Handles error message
  92. //////////////////////////////
  93. const errorM = (error) => {
  94. const filePath = (error.file === 'stdin' ? file.path : error.file) || file.path;
  95. const relativePath = path.relative(process.cwd(), filePath);
  96. const message = [chalk.underline(relativePath), error.formatted].join('\n');
  97. error.messageFormatted = message; // eslint-disable-line no-param-reassign
  98. error.messageOriginal = error.message; // eslint-disable-line no-param-reassign
  99. error.message = stripAnsi(message); // eslint-disable-line no-param-reassign
  100. error.relativePath = relativePath; // eslint-disable-line no-param-reassign
  101. return cb(new PluginError(PLUGIN_NAME, error));
  102. };
  103. if (sync !== true) {
  104. //////////////////////////////
  105. // Async Sass render
  106. //////////////////////////////
  107. const callback = (error, obj) => { // eslint-disable-line consistent-return
  108. if (error) {
  109. return errorM(error);
  110. }
  111. filePush(obj);
  112. };
  113. gulpSass.compiler.render(opts, callback);
  114. } else {
  115. //////////////////////////////
  116. // Sync Sass render
  117. //////////////////////////////
  118. try {
  119. filePush(gulpSass.compiler.renderSync(opts));
  120. } catch (error) {
  121. return errorM(error);
  122. }
  123. }
  124. });
  125. //////////////////////////////
  126. // Sync Sass render
  127. //////////////////////////////
  128. gulpSass.sync = options => gulpSass(options, true);
  129. //////////////////////////////
  130. // Log errors nicely
  131. //////////////////////////////
  132. gulpSass.logError = function logError(error) {
  133. const message = new PluginError('sass', error.messageFormatted).toString();
  134. process.stderr.write(`${message}\n`);
  135. this.emit('end');
  136. };
  137. //////////////////////////////
  138. // Store compiler in a prop
  139. //////////////////////////////
  140. gulpSass.compiler = require('sass');
  141. module.exports = gulpSass;