parse.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict'
  2. var Input = require('postcss/lib/input')
  3. var Parser = require('./parser')
  4. module.exports = function parse(css, opts) {
  5. if ( opts && opts.safe ) {
  6. throw new Error('Option safe was removed. ' +
  7. 'Use parser: require("postcss-safe-parser")');
  8. }
  9. let input = new Input(css, opts);
  10. let parser = new Parser(input);
  11. try {
  12. parser.parse();
  13. } catch (e) {
  14. if ( e.name === 'CssSyntaxError' && opts && opts.from ) {
  15. if ( /\.scss$/i.test(opts.from) ) {
  16. e.message += '\nYou tried to parse SCSS with ' +
  17. 'the standard CSS parser; ' +
  18. 'try again with the postcss-scss parser';
  19. } else if ( /\.sass/i.test(opts.from) ) {
  20. e.message += '\nYou tried to parse Sass with ' +
  21. 'the standard CSS parser; ' +
  22. 'try again with the postcss-sass parser';
  23. } else if ( /\.less$/i.test(opts.from) ) {
  24. e.message += '\nYou tried to parse Less with ' +
  25. 'the standard CSS parser; ' +
  26. 'try again with the postcss-less parser';
  27. }
  28. }
  29. throw e;
  30. }
  31. return parser.root;
  32. }