Home Reference Source Test

source/parser.js

  1. import LESS from 'less';
  2.  
  3. import * as SASS from 'sass';
  4.  
  5. import Stylus from 'stylus';
  6.  
  7. import YAML from 'yaml';
  8.  
  9.  
  10. /**
  11. * @private
  12. *
  13. * @param {String} raw
  14. *
  15. * @return {String}
  16. */
  17. export function css(raw) { return raw; }
  18.  
  19.  
  20. /**
  21. * @private
  22. *
  23. * @param {String} raw
  24. * @param {String} path
  25. * @param {Object} [option] - http://lesscss.org/usage/#less-options
  26. *
  27. * @return {String}
  28. */
  29. export async function less(raw, path, option) {
  30.  
  31. return (await LESS.render(raw, { paths: [path], ...option })).css;
  32. }
  33.  
  34.  
  35. /**
  36. * @private
  37. *
  38. * @param {String} raw
  39. * @param {String} path
  40. * @param {Object} [option] - https://github.com/sass/node-sass#options
  41. *
  42. * @return {String}
  43. */
  44. export function sass(raw, path, option) {
  45.  
  46. return SASS.renderSync({ data: raw, includePaths: [path], ... option }).css;
  47. }
  48.  
  49. /**
  50. * @private
  51. *
  52. * @type {Function}
  53. */
  54. export const scss = sass;
  55.  
  56.  
  57. /**
  58. * @private
  59. *
  60. * @param {string} raw
  61. * @param {String} path
  62. * @param {Object} [option] - https://github.com/stylus/stylus/blob/HEAD/docs/js.md
  63. *
  64. * @return {Promise<string>} CSS source code
  65. */
  66. export function stylus(raw, path, option = { }) {
  67.  
  68. return new Promise((resolve, reject) => Stylus.render(
  69. raw,
  70. { paths: [path], ...option },
  71. (error, CSS) => error ? reject( error ) : resolve( CSS )
  72. ));
  73. }
  74.  
  75.  
  76. /**
  77. * @private
  78. *
  79. * @param {String} raw
  80. * @param {Object} [option] - https://eemeli.org/yaml/#options
  81. *
  82. * @return {String}
  83. */
  84. export function yaml(raw, option) { return YAML.parse(raw, option); }