process-style.js 937 B

123456789101112131415161718192021222324252627282930313233343536
  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. }
  22. const cssNames = cssExtract.extractClasses(cssCode)
  23. const obj = {}
  24. for (let i = 0, l = cssNames.length; i < l; i++) {
  25. obj[cssNames[i]] = cssNames[i]
  26. }
  27. return obj
  28. }