sass-compiler.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. const ensureRequire = require('../ensure-require')
  2. const getVueJestConfig = require('../get-vue-jest-config')
  3. const logger = require('../logger')
  4. const applyModuleNameMapper = require('./helpers/module-name-mapper-helper')
  5. /**
  6. * This module is meant to compile sass
  7. *
  8. * @param {String} content - the content of the sass string that should be compiled
  9. * @param {String} filePath - the path of the file holding the sass
  10. * @param {Object} jestConfig - the complete jest config
  11. * @returns {String} styles - the compiled sass
  12. */
  13. module.exports = (content, filePath, jestConfig = {}) => {
  14. const vueJestConfig = getVueJestConfig(jestConfig)
  15. ensureRequire('sass', ['node-sass'])
  16. const sass = require('node-sass')
  17. try {
  18. return sass.renderSync({
  19. data: content,
  20. outputStyle: 'compressed',
  21. indentedSyntax: true,
  22. importer: (url, prev, done) => ({ file: applyModuleNameMapper(url, prev === 'stdin' ? filePath : prev, jestConfig) })
  23. }).css.toString()
  24. } catch (err) {
  25. if (!vueJestConfig.hideStyleWarn) {
  26. logger.warn(`There was an error rendering the SASS in ${filePath}. SASS is fully supported by vue-jest. Still some features might throw errors. Webpack aliases are a common cause of errors. If you use Webpack aliases, please use jest's suggested way via moduleNameMapper which is supported.`)
  27. logger.warn(`Error while compiling styles: ${err}`)
  28. }
  29. }
  30. return ''
  31. }