postcss.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * MIT License http://opensource.org/licenses/MIT
  3. * Author: Ben Holloway @bholloway
  4. */
  5. 'use strict';
  6. var os = require('os'),
  7. path = require('path'),
  8. postcss = require('postcss');
  9. var fileProtocol = require('../file-protocol');
  10. var ORPHAN_CR_REGEX = /\r(?!\n)(.|\n)?/g;
  11. /**
  12. * Process the given CSS content into reworked CSS content.
  13. *
  14. * @param {string} sourceFile The absolute path of the file being processed
  15. * @param {string} sourceContent CSS content without source-map
  16. * @param {{outputSourceMap: boolean, transformDeclaration:function, absSourceMap:object,
  17. * sourceMapConsumer:object, removeCR:boolean}} params Named parameters
  18. * @return {{content: string, map: object}} Reworked CSS and optional source-map
  19. */
  20. function process(sourceFile, sourceContent, params) {
  21. // #107 libsass emits orphan CR not considered newline, postcss does consider newline (content vs source-map mismatch)
  22. var correctedContent = params.removeCR && (os.EOL !== '\r') ?
  23. sourceContent.replace(ORPHAN_CR_REGEX, ' $1') :
  24. sourceContent;
  25. // prepend file protocol to all sources to avoid problems with source map
  26. return postcss([
  27. postcss.plugin('postcss-resolve-url', postcssPlugin)
  28. ])
  29. .process(correctedContent, {
  30. from: fileProtocol.prepend(sourceFile),
  31. map : params.outputSourceMap && {
  32. prev : !!params.absSourceMap && fileProtocol.prepend(params.absSourceMap),
  33. inline : false,
  34. annotation : false,
  35. sourcesContent: true // #98 sourcesContent missing from output map
  36. }
  37. })
  38. .then(result => ({
  39. content: result.css,
  40. map : params.outputSourceMap ? fileProtocol.remove(result.map.toJSON()) : null
  41. }));
  42. /**
  43. * Plugin for postcss that follows SASS transpilation.
  44. */
  45. function postcssPlugin() {
  46. return function(styles) {
  47. styles.walkDecls(eachDeclaration);
  48. };
  49. /**
  50. * Process a declaration from the syntax tree.
  51. * @param declaration
  52. */
  53. function eachDeclaration(declaration) {
  54. var isValid = declaration.value && (declaration.value.indexOf('url') >= 0);
  55. if (isValid) {
  56. // reverse the original source-map to find the original source file before transpilation
  57. var startPosApparent = declaration.source.start,
  58. startPosOriginal = params.sourceMapConsumer &&
  59. params.sourceMapConsumer.originalPositionFor(startPosApparent);
  60. // we require a valid directory for the specified file
  61. var directory =
  62. startPosOriginal &&
  63. startPosOriginal.source &&
  64. fileProtocol.remove(path.dirname(startPosOriginal.source));
  65. if (directory) {
  66. declaration.value = params.transformDeclaration(declaration.value, directory);
  67. }
  68. // source-map present but invalid entry
  69. else if (params.sourceMapConsumer) {
  70. throw new Error(
  71. 'source-map information is not available at url() declaration ' +
  72. (ORPHAN_CR_REGEX.test(sourceContent) ? '(found orphan CR, try removeCR option)' : '(no orphan CR found)')
  73. );
  74. }
  75. }
  76. }
  77. }
  78. }
  79. module.exports = process;