@sveltejs/vite-plugin-svelte
Advanced tools
Comparing version
{ | ||
"name": "@sveltejs/vite-plugin-svelte", | ||
"version": "6.0.0", | ||
"version": "6.1.0", | ||
"license": "MIT", | ||
@@ -51,4 +51,4 @@ "author": "dominikg", | ||
"sass": "^1.89.2", | ||
"svelte": "^5.35.2", | ||
"vite": "^7.0.2" | ||
"svelte": "^5.36.1", | ||
"vite": "^7.0.4" | ||
}, | ||
@@ -55,0 +55,0 @@ "scripts": { |
import { buildModuleIdFilter, buildModuleIdParser } from '../utils/id.js'; | ||
import * as svelteCompiler from 'svelte/compiler'; | ||
import { logCompilerWarnings } from '../utils/log.js'; | ||
import { log, logCompilerWarnings } from '../utils/log.js'; | ||
import { toRollupError } from '../utils/error.js'; | ||
import { isSvelteWithAsync } from '../utils/svelte-version.js'; | ||
@@ -19,2 +20,8 @@ /** | ||
let idParser; | ||
/** | ||
* @type {import('svelte/compiler').ModuleCompileOptions} | ||
*/ | ||
let staticModuleCompileOptions; | ||
/** @type {import('vite').Plugin} */ | ||
@@ -29,2 +36,3 @@ const plugin = { | ||
idParser = buildModuleIdParser(options); | ||
staticModuleCompileOptions = filterNonModuleCompilerOptions(options.compilerOptions); | ||
}, | ||
@@ -38,8 +46,44 @@ transform: { | ||
} | ||
const filename = moduleRequest.filename; | ||
/** @type {import('svelte/compiler').CompileOptions} */ | ||
const compileOptions = { | ||
...staticModuleCompileOptions, | ||
dev: !this.environment.config.isProduction, | ||
generate: ssr ? 'server' : 'client', | ||
filename | ||
}; | ||
const dynamicCompileOptions = await options?.dynamicCompileOptions?.({ | ||
filename, | ||
code, | ||
compileOptions | ||
}); | ||
if (dynamicCompileOptions && log.debug.enabled) { | ||
log.debug( | ||
`dynamic compile options for ${filename}: ${JSON.stringify(dynamicCompileOptions)}`, | ||
undefined, | ||
'compileModule' | ||
); | ||
} | ||
const finalCompileOptions = dynamicCompileOptions | ||
? { | ||
...compileOptions, | ||
...dynamicCompileOptions | ||
} | ||
: compileOptions; | ||
if (dynamicCompileOptions?.experimental) { | ||
finalCompileOptions.experimental = { | ||
...compileOptions.experimental, | ||
...dynamicCompileOptions.experimental | ||
}; | ||
} | ||
const finalModuleCompileOptions = filterNonModuleCompilerOptions(finalCompileOptions); | ||
if (log.debug.enabled) { | ||
log.debug( | ||
`final ModuleCompileOptions for ${filename}: ${JSON.stringify(finalModuleCompileOptions)}`, | ||
undefined, | ||
'compileModule' | ||
); | ||
} | ||
try { | ||
const compileResult = svelteCompiler.compileModule(code, { | ||
dev: !this.environment.config.isProduction, | ||
generate: ssr ? 'server' : 'client', | ||
filename: moduleRequest.filename | ||
}); | ||
const compileResult = svelteCompiler.compileModule(code, finalModuleCompileOptions); | ||
logCompilerWarnings(moduleRequest, compileResult.warnings, options); | ||
@@ -55,1 +99,36 @@ return compileResult.js; | ||
} | ||
/** | ||
* | ||
* @param {import('svelte/compiler').CompileOptions} compilerOptions | ||
* @return {import('svelte/compiler').ModuleCompileOptions} | ||
*/ | ||
function filterNonModuleCompilerOptions(compilerOptions) { | ||
/** @type {Array<keyof import('svelte/compiler').ModuleCompileOptions>} */ | ||
const knownModuleCompileOptionNames = ['dev', 'generate', 'filename', 'rootDir', 'warningFilter']; | ||
if (isSvelteWithAsync) { | ||
knownModuleCompileOptionNames.push('experimental'); | ||
} | ||
// not typed but this is temporary until svelte itself ignores CompileOptions passed to compileModule | ||
const experimentalModuleCompilerOptionNames = ['async']; | ||
/** @type {import('svelte/compiler').ModuleCompileOptions} */ | ||
const filtered = filterByPropNames(compilerOptions, knownModuleCompileOptionNames); | ||
if (filtered.experimental) { | ||
filtered.experimental = filterByPropNames( | ||
filtered.experimental, | ||
experimentalModuleCompilerOptionNames | ||
); | ||
} | ||
return filtered; | ||
} | ||
/** | ||
* | ||
* @param {object} o | ||
* @param {string[]} names | ||
* @returns {object} | ||
*/ | ||
function filterByPropNames(o, names) { | ||
return Object.fromEntries(Object.entries(o).filter(([name]) => names.includes(name))); | ||
} |
@@ -135,2 +135,5 @@ import { buildExtendedLogMessage } from './log.js'; | ||
while ((m = styleRe.exec(originalCode))) { | ||
if (m[0]?.startsWith('<!--')) { | ||
continue; | ||
} | ||
// Warn missing lang attribute | ||
@@ -137,0 +140,0 @@ if (!m[1]?.includes('lang=')) { |
@@ -70,3 +70,3 @@ import path from 'node:path'; | ||
if (existingKnownConfigFiles.length === 0) { | ||
log.debug(`no svelte config found at ${root}`, undefined, 'config'); | ||
log.info(`no Svelte config found at ${root} - using default configuration.`); | ||
return; | ||
@@ -73,0 +73,0 @@ } else if (existingKnownConfigFiles.length > 1) { |
@@ -6,8 +6,23 @@ import { VERSION } from 'svelte/compiler'; | ||
*/ | ||
export const isSvelte5 = VERSION.startsWith('5.'); | ||
export const isSvelteWithAsync = gte(VERSION, '5.36.0'); | ||
/** | ||
* @type {boolean} | ||
* compare semver versions, does not include comparing tags (-next.xy is ignored) | ||
* | ||
* @param {string} a semver version | ||
* @param {string} b semver version | ||
* @return {boolean} true if a is greater or equal to b | ||
*/ | ||
export const isSvelte5WithHMRSupport = | ||
VERSION.startsWith('5.0.0-next.') && Number(VERSION.slice(11)) > 96; | ||
export function gte(a, b) { | ||
const aNum = a.split(/[.-]/, 3).map(Number); | ||
const bNum = b.split(/[.-]/, 3).map(Number); | ||
for (let i = 0; i < aNum.length; i++) { | ||
if (aNum[i] < bNum[i]) { | ||
return false; | ||
} | ||
if (aNum[i] > bNum[i]) { | ||
return true; | ||
} | ||
} | ||
return true; | ||
} |
126572
2.41%3870
2.44%