parse-imports.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. var tokenizer = require('scss-tokenizer');
  2. function parseImports(content, isIndentedSyntax) {
  3. var tokens = tokenizer.tokenize(content);
  4. var results = [];
  5. var tmp = '';
  6. var inImport = false;
  7. var inParen = false;
  8. var prevToken = tokens[0];
  9. var i, token;
  10. for (i = 1; i < tokens.length; i++) {
  11. token = tokens[i];
  12. if (inImport && !inParen && token[0] === 'string') {
  13. results.push(token[1]);
  14. }
  15. else if (token[1] === 'import' && prevToken[1] === '@') {
  16. if (inImport && !isIndentedSyntax) {
  17. throw new Error('Encountered invalid @import syntax.');
  18. }
  19. inImport = true;
  20. }
  21. else if (inImport && !inParen && (token[0] === 'ident' || token[0] === '/')) {
  22. tmp += token[1];
  23. }
  24. else if (inImport && !inParen && (token[0] === 'space' || token[0] === 'newline')) {
  25. if (tmp !== '') {
  26. results.push(tmp);
  27. tmp = '';
  28. if (isIndentedSyntax) {
  29. inImport = false;
  30. }
  31. }
  32. }
  33. else if (inImport && token[0] === ';') {
  34. inImport = false;
  35. if (tmp !== '') {
  36. results.push(tmp);
  37. tmp = '';
  38. }
  39. }
  40. else if (inImport && token[0] === '(') {
  41. inParen = true;
  42. tmp = '';
  43. }
  44. else if (inParen && token[0] === ')') {
  45. inParen = false;
  46. }
  47. prevToken = token;
  48. }
  49. if (tmp !== '') {
  50. results.push(tmp);
  51. }
  52. return results;
  53. }
  54. module.exports = parseImports;