process-style.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. const getVueJestConfig = require('./get-vue-jest-config')
  2. const cssExtract = require('extract-from-css')
  3. module.exports = function processStyle (stylePart, filePath, jestConfig = {}) {
  4. const vueJestConfig = getVueJestConfig(jestConfig)
  5. if (!stylePart || vueJestConfig.experimentalCSSCompile === false) {
  6. return {}
  7. }
  8. const processStyleByLang = lang => require('./compilers/' + lang + '-compiler')(stylePart.content, filePath, jestConfig)
  9. let cssCode = stylePart.content
  10. switch (stylePart.lang) {
  11. case 'styl':
  12. case 'stylus':
  13. cssCode = processStyleByLang('stylus')
  14. break
  15. case 'scss':
  16. cssCode = processStyleByLang('scss')
  17. break
  18. case 'sass':
  19. cssCode = processStyleByLang('sass')
  20. break
  21. case 'pcss':
  22. case 'postcss':
  23. cssCode = processStyleByLang('postcss')
  24. break
  25. }
  26. const cssNames = cssExtract.extractClasses(cssCode)
  27. const obj = {}
  28. for (let i = 0, l = cssNames.length; i < l; i++) {
  29. obj[cssNames[i]] = cssNames[i]
  30. }
  31. return obj
  32. }