index.js 2.4 KB

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