parse.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict'
  2. let Container = require('./container')
  3. let Input = require('./input')
  4. let Parser = require('./parser')
  5. function parse(css, opts) {
  6. let input = new Input(css, opts)
  7. let parser = new Parser(input)
  8. try {
  9. parser.parse()
  10. } catch (e) {
  11. if (process.env.NODE_ENV !== 'production') {
  12. if (e.name === 'CssSyntaxError' && opts && opts.from) {
  13. if (/\.scss$/i.test(opts.from)) {
  14. e.message +=
  15. '\nYou tried to parse SCSS with ' +
  16. 'the standard CSS parser; ' +
  17. 'try again with the postcss-scss parser'
  18. } else if (/\.sass/i.test(opts.from)) {
  19. e.message +=
  20. '\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 +=
  25. '\nYou tried to parse Less with ' +
  26. 'the standard CSS parser; ' +
  27. 'try again with the postcss-less parser'
  28. }
  29. }
  30. }
  31. throw e
  32. }
  33. return parser.root
  34. }
  35. module.exports = parse
  36. parse.default = parse
  37. Container.registerParse(parse)