Skip to content

Commit

Permalink
feat(svelte5): add support for Svelte 5 modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mcous committed May 15, 2024
1 parent 90ea37c commit ed57c66
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
20 changes: 17 additions & 3 deletions src/transformer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { execSync } from 'child_process'
import { basename, extname } from 'path'
import { pathToFileURL } from 'url'
import { compile, preprocess as sveltePreprocess } from 'svelte/compiler'
import * as SvelteCompiler from 'svelte/compiler'

import { getSvelteConfig } from './svelteconfig.js'
import { dynamicImport, IS_COMMON_JS, isSvelte3 } from './utils.js'
import { dynamicImport, IS_COMMON_JS, isSvelte3, isSvelteModule } from './utils.js'

const currentFileExtension = (global.__dirname !== undefined ? extname(__filename) : extname(pathToFileURL(import.meta.url).toString())).replace('.', '')

Expand All @@ -29,7 +29,7 @@ const processAsync = async (source, filename, jestOptions) => {

const svelteConfigPath = getSvelteConfig(rootMode, filename, preprocess)
const svelteConfig = await dynamicImport(svelteConfigPath)
const processed = await sveltePreprocess(
const processed = await SvelteCompiler.preprocess(
source,
svelteConfig.default.preprocess || {},
{ filename }
Expand Down Expand Up @@ -91,6 +91,8 @@ const compiler = (format, options = {}, filename, processedCode, processedMap) =
opts.format = format
}

const compile = isSvelteModule(filename) ? compileModule : compileComponent

let result
try {
result = compile(processedCode, opts)
Expand All @@ -115,6 +117,18 @@ const compiler = (format, options = {}, filename, processedCode, processedMap) =
}
}

const compileComponent = (processedCode, opts) => {
return SvelteCompiler.compile(processedCode, opts)
}

const compileModule = (processedCode, opts) => {
return SvelteCompiler.compileModule(processedCode, {
filename: opts.filename,
dev: opts.dev,
generate: opts.ssr ? 'server' : 'client',
})
}

export default {
process: processSync,
processAsync
Expand Down
13 changes: 11 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { pathToFileURL } from 'url'
import { VERSION } from 'svelte/compiler'
import * as SvelteCompiler from 'svelte/compiler'

export const dynamicImport = async (filename) => import(pathToFileURL(filename).toString())

export const IS_COMMON_JS = typeof module !== 'undefined'

export const isSvelte3 = (version = VERSION) => version.startsWith('3')
export const isSvelte3 = (version = SvelteCompiler.VERSION) => version.startsWith('3')

export const DEFAULT_SVELTE_MODULE_INFIX = ['.svelte.']

export const DEFAULT_SVELTE_MODULE_EXT = ['.js', '.ts']

export const isSvelteModule = (filename) =>
typeof SvelteCompiler.compileModule === 'function' &&
DEFAULT_SVELTE_MODULE_INFIX.some((infix) => filename.includes(infix)) &&
DEFAULT_SVELTE_MODULE_EXT.some((ext) => filename.endsWith(ext));

0 comments on commit ed57c66

Please sign in to comment.