diff --git a/src/transformer.js b/src/transformer.js index 75c4f89..d0b3568 100644 --- a/src/transformer.js +++ b/src/transformer.js @@ -4,13 +4,10 @@ import { pathToFileURL } from 'url' import { compile, preprocess as sveltePreprocess } from 'svelte/compiler' import { getSvelteConfig } from './svelteconfig.js' +import { dynamicImport, IS_COMMON_JS, isSvelte3 } from './utils.js' -const dynamicImport = async (filename) => import(pathToFileURL(filename).toString()) const currentFileExtension = (global.__dirname !== undefined ? extname(__filename) : extname(pathToFileURL(import.meta.url).toString())).replace('.', '') -const IS_SVELTE_3 = svelte.VERSION.startsWith('3') -const IS_COMMON_JS = typeof module !== 'undefined' - /** * Jest will only call this method when running in ESM mode. */ @@ -83,14 +80,14 @@ const processSync = (source, filename, jestOptions) => { const compiler = (format, options = {}, filename, processedCode, processedMap) => { const opts = { filename: basename(filename), - css: IS_SVELTE_3 ? true : 'injected', + css: isSvelte3(options.svelteVersion) ? true : 'injected', accessors: true, dev: true, sourcemap: processedMap, ...options.compilerOptions } - if (IS_SVELTE_3) { + if (isSvelte3(options.svelteVersion)) { opts.format = format } diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..a82b7a3 --- /dev/null +++ b/src/utils.js @@ -0,0 +1,7 @@ +import { pathToFileURL } from 'url' + +export const dynamicImport = async (filename) => import(pathToFileURL(filename).toString()) + +export const IS_COMMON_JS = typeof module !== 'undefined' + +export const isSvelte3 = (version = '4') => version.startsWith('3')