postcss.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = process;
  6. var _path = _interopRequireDefault(require("path"));
  7. var _fileProtocol = _interopRequireDefault(require("./file-protocol"));
  8. function process(postcss, sourceFile, sourceContent, params) {
  9. // #107 libsass emits orphan CR not considered newline, postcss does consider newline (content vs source-map mismatch)
  10. // prepend file protocol to all sources to avoid problems with source map
  11. return postcss([
  12. postcss.plugin("postcss-resolve-url", postcssPlugin)
  13. ]).process(sourceContent, {
  14. from: _fileProtocol.default.prepend(sourceFile),
  15. map: params.outputSourceMap && {
  16. prev: !!params.inputSourceMap && _fileProtocol.default.prepend(params.inputSourceMap),
  17. inline: false,
  18. annotation: false,
  19. sourcesContent: true
  20. }
  21. }).then((result)=>({
  22. content: result.css,
  23. map: params.outputSourceMap ? _fileProtocol.default.remove(result.map.toJSON()) : null
  24. }));
  25. /**
  26. * Plugin for postcss that follows SASS transpilation.
  27. */ function postcssPlugin() {
  28. return function(styles) {
  29. styles.walkDecls(eachDeclaration);
  30. };
  31. /**
  32. * Process a declaration from the syntax tree.
  33. * @param declaration
  34. */ function eachDeclaration(declaration) {
  35. const isValid = declaration.value && declaration.value.indexOf("url") >= 0;
  36. if (isValid) {
  37. // reverse the original source-map to find the original source file before transpilation
  38. const startPosApparent = declaration.source.start, startPosOriginal = params.sourceMapConsumer && params.sourceMapConsumer.originalPositionFor(startPosApparent);
  39. // we require a valid directory for the specified file
  40. const directory = startPosOriginal && startPosOriginal.source && _fileProtocol.default.remove(_path.default.dirname(startPosOriginal.source));
  41. if (directory) {
  42. declaration.value = params.transformDeclaration(declaration.value, directory);
  43. } else if (params.sourceMapConsumer) {
  44. throw new Error("source-map information is not available at url() declaration " + (ORPHAN_CR_REGEX.test(sourceContent) ? "(found orphan CR, try removeCR option)" : "(no orphan CR found)"));
  45. }
  46. }
  47. }
  48. }
  49. }
  50. function _interopRequireDefault(obj) {
  51. return obj && obj.__esModule ? obj : {
  52. default: obj
  53. };
  54. }
  55. const ORPHAN_CR_REGEX = /\r(?!\n)(.|\n)?/g;
  56. //# sourceMappingURL=postcss.js.map