index.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.processors = void 0;
  4. const merge = require('merge-source-map');
  5. // .scss/.sass processor
  6. const scss = {
  7. render(source, map, options) {
  8. const nodeSass = require('sass');
  9. const finalOptions = Object.assign({}, options, {
  10. data: source,
  11. file: options.filename,
  12. outFile: options.filename,
  13. sourceMap: !!map
  14. });
  15. try {
  16. const result = nodeSass.renderSync(finalOptions);
  17. if (map) {
  18. return {
  19. code: result.css.toString(),
  20. map: merge(map, JSON.parse(result.map.toString())),
  21. errors: []
  22. };
  23. }
  24. return { code: result.css.toString(), errors: [] };
  25. }
  26. catch (e) {
  27. return { code: '', errors: [e] };
  28. }
  29. }
  30. };
  31. const sass = {
  32. render(source, map, options) {
  33. return scss.render(source, map, Object.assign({}, options, { indentedSyntax: true }));
  34. }
  35. };
  36. // .less
  37. const less = {
  38. render(source, map, options) {
  39. const nodeLess = require('less');
  40. let result;
  41. let error = null;
  42. nodeLess.render(source, Object.assign({}, options, { syncImport: true }), (err, output) => {
  43. error = err;
  44. result = output;
  45. });
  46. if (error)
  47. return { code: '', errors: [error] };
  48. if (map) {
  49. return {
  50. code: result.css.toString(),
  51. map: merge(map, result.map),
  52. errors: []
  53. };
  54. }
  55. return { code: result.css.toString(), errors: [] };
  56. }
  57. };
  58. // .styl
  59. const styl = {
  60. render(source, map, options) {
  61. const nodeStylus = require('stylus');
  62. try {
  63. const ref = nodeStylus(source);
  64. Object.keys(options).forEach(key => ref.set(key, options[key]));
  65. if (map)
  66. ref.set('sourcemap', { inline: false, comment: false });
  67. const result = ref.render();
  68. if (map) {
  69. return {
  70. code: result,
  71. map: merge(map, ref.sourcemap),
  72. errors: []
  73. };
  74. }
  75. return { code: result, errors: [] };
  76. }
  77. catch (e) {
  78. return { code: '', errors: [e] };
  79. }
  80. }
  81. };
  82. exports.processors = {
  83. less,
  84. sass,
  85. scss,
  86. styl,
  87. stylus: styl
  88. };