Skip to content

Runes mode enforcement is currently too hamfisted #987

@GrygrFlzr

Description

@GrygrFlzr

Followup to #944 #952 and #984
Currently, the default config generated by sv cli uses this function:

dynamicCompileOptions: ({ filename }) =>
			filename.includes('node_modules') ? undefined : { runes: true }

This currently just checks for the existence of node_modules in the filename path for determining if the compiler should use runes mode.
As a result:

  1. It does not respect any existing <svelte:options runes={false}> in .svelte files
    • edit: corrected, see comments
  2. It unintentionally forces runes mode for generated files in the .svelte-kit folder
    • edit: partially incorrect due to (1) being incorrect
  3. It unintentionally skips a path like src/lib/leafnode_modules, or all paths if the username happens to be node_modules

A more complete solution could be instead to scope the changes to the src folder, and also skip .svelte files that contain <svelte:options as a "I know what I'm doing" hint.
Example function:

import { extname, join, relative } from 'node:path';
const srcDir = join(import.meta.dirname, 'src');
/** @type {import('@sveltejs/vite-plugin-svelte').PluginOptions['dynamicCompileOptions']} */
const dynamicCompileOptions = ({ filename, code }) => {
	const isOutOfSrc = relative(srcDir, filename).startsWith('..');
	// .svelte.ts and .svelte.js definitely require runes mode
	const isSFCWithOptionDeclared =
		extname(filename) === 'svelte' && code.includes('<svelte:options');

	return isOutOfSrc || isSFCWithOptionDeclared ? undefined : { runes: true };
};

However, I think sv cli is not the right place to apply this anyway, because if e.g. we want to update the function, that requires all users with generated configs to manually change their config; it is an inherently fragile execution of the concept.

Even the function I provided would be fragile against the user configuring a custom kit.files.src path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions