ninetails
7/16/2019 - 9:30 PM

Jest transformer for Svelte 3 with preprocessor

Jest transformer for Svelte 3 with preprocessor

const deasync = require('deasync')
const svelte = require('svelte/compiler')
const { stylus } = require('svelte-preprocess')

module.exports.process = (src, filename) => {
  let result, error
  svelte
    .preprocess(src, [stylus()], { filename })
    .then(({ code }) =>
      svelte.compile(code, {
        dev: true,
        format: 'cjs',
        filename
      })
    )
    .then(({ js: { code, map } }) => {
      result = { code, map }
    })
    .catch(err => {
      error = err
      console.error(err)
    })

  deasync.loopWhile(() => !result && !error) // race condition

  return result
}