babel-compiler.js 809 B

123456789101112131415161718192021222324252627282930
  1. const babel = require('babel-core')
  2. const loadBabelConfig = require('../load-babel-config.js')
  3. module.exports = function compileBabel (scriptContent, inputSourceMap, inlineConfig, vueJestConfig, filePath) {
  4. const babelConfig = inlineConfig || loadBabelConfig(vueJestConfig, filePath)
  5. if (!babelConfig) {
  6. return {
  7. code: scriptContent,
  8. sourceMap: inputSourceMap
  9. }
  10. }
  11. const sourceMapOptions = {
  12. sourceMaps: true,
  13. inputSourceMap: inputSourceMap
  14. }
  15. const babelOptions = Object.assign(sourceMapOptions, babelConfig)
  16. // babel throws error if filename is undefined, "unknown" is default
  17. if (!babelOptions.filename) babelOptions.filename = 'unknown'
  18. const res = babel.transform(scriptContent, babelOptions)
  19. return {
  20. code: res.code,
  21. sourceMap: res.map
  22. }
  23. }