rework.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * MIT License http://opensource.org/licenses/MIT
  3. * Author: Ben Holloway @bholloway
  4. */
  5. 'use strict';
  6. var path = require('path'),
  7. convert = require('convert-source-map');
  8. var fileProtocol = require('../file-protocol');
  9. var rework = requireOptionalPeerDependency('rework'),
  10. visit = requireOptionalPeerDependency('rework-visit');
  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}} params Named parameters
  18. * @return {{content: string, map: object}} Reworked CSS and optional source-map
  19. */
  20. function process(sourceFile, sourceContent, params) {
  21. // embed source-map in css
  22. // prepend file protocol to all sources to avoid problems with source map
  23. var contentWithMap = sourceContent + (
  24. params.absSourceMap ?
  25. convert.fromObject(fileProtocol.prepend(params.absSourceMap)).toComment({multiline: true}) :
  26. ''
  27. );
  28. // need to prepend file protocol to source as well to avoid problems with source map
  29. var reworked = rework(contentWithMap, {source: fileProtocol.prepend(sourceFile)})
  30. .use(reworkPlugin)
  31. .toString({
  32. sourcemap : params.outputSourceMap,
  33. sourcemapAsObject: params.outputSourceMap
  34. });
  35. // complete with source-map
  36. if (params.outputSourceMap) {
  37. return {
  38. content: reworked.code,
  39. map : fileProtocol.remove(reworked.map)
  40. };
  41. }
  42. // complete without source-map
  43. else {
  44. return {
  45. content: reworked,
  46. map : null
  47. };
  48. }
  49. /**
  50. * Plugin for css rework that follows SASS transpilation.
  51. *
  52. * @param {object} stylesheet AST for the CSS output from SASS
  53. */
  54. function reworkPlugin(stylesheet) {
  55. // visit each node (selector) in the stylesheet recursively using the official utility method
  56. // each node may have multiple declarations
  57. visit(stylesheet, function visitor(declarations) {
  58. if (declarations) {
  59. declarations.forEach(eachDeclaration);
  60. }
  61. });
  62. /**
  63. * Process a declaration from the syntax tree.
  64. * @param declaration
  65. */
  66. function eachDeclaration(declaration) {
  67. var isValid = declaration.value && (declaration.value.indexOf('url') >= 0);
  68. if (isValid) {
  69. declaration.value = params.transformDeclaration(declaration.value, getPathsAtChar);
  70. }
  71. /**
  72. * Create a hash of base path strings.
  73. *
  74. * Position in the declaration is not supported since rework does not refine sourcemaps to this detail.
  75. *
  76. * @throws Error on invalid source map
  77. * @returns {{selector:string, property:string}} Hash of base path strings
  78. */
  79. function getPathsAtChar() {
  80. var posSelector = declaration.parent && declaration.parent.position.start,
  81. posProperty = declaration.position.start;
  82. var result = {
  83. property: positionToOriginalDirectory(posProperty),
  84. selector: positionToOriginalDirectory(posSelector)
  85. };
  86. var isValid = [result.property, result.selector].every(Boolean);
  87. if (isValid) {
  88. return result;
  89. }
  90. else if (params.sourceMapConsumer) {
  91. throw new Error('source-map information is not available at url() declaration');
  92. } else {
  93. throw new Error('a valid source-map is not present (ensure preceding loaders output a source-map)');
  94. }
  95. }
  96. }
  97. }
  98. /**
  99. * Given an apparent position find the directory of the original file.
  100. *
  101. * @param startPosApparent {{line: number, column: number}}
  102. * @returns {false|string} Directory of original file or false on invalid
  103. */
  104. function positionToOriginalDirectory(startPosApparent) {
  105. // reverse the original source-map to find the original source file before transpilation
  106. var startPosOriginal =
  107. !!params.sourceMapConsumer &&
  108. params.sourceMapConsumer.originalPositionFor(startPosApparent);
  109. // we require a valid directory for the specified file
  110. var directory =
  111. !!startPosOriginal &&
  112. !!startPosOriginal.source &&
  113. fileProtocol.remove(path.dirname(startPosOriginal.source));
  114. return directory;
  115. }
  116. }
  117. module.exports = process;
  118. /**
  119. * Require the given filename but fail with an error that `requireOptionalPeerDependencies` must be installed.
  120. *
  121. * @param moduleName The module to require
  122. * @returns {*} The module
  123. * @throws Error when module is not found
  124. */
  125. function requireOptionalPeerDependency(moduleName) {
  126. try {
  127. return require(moduleName);
  128. }
  129. catch (error) {
  130. if (error.message === 'Cannot find module \'' + moduleName + '\'') {
  131. throw new Error('to use the "rework" engine you must install the optionalPeerDependencies');
  132. }
  133. else {
  134. throw error;
  135. }
  136. }
  137. }