Skip to content

Commit

Permalink
feat: extract functions into utils
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianrothe committed Jul 23, 2023
1 parent d079756 commit ea8b0c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -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')

0 comments on commit ea8b0c3

Please sign in to comment.