All files / src/compiler validate-options.js

84.93% Statements 265/312
76.36% Branches 42/55
91.66% Functions 11/12
84.88% Lines 264/311

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 3122x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4711x           4711x 4711x 4711x     4711x 4711x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x         2x         2x 2x     2x 2x 2x 2x 2x 94x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 34x 34x 34x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 70x 70x 23430x     23430x 70x 70x 2x 2x 2x 2x 10x 10x 1x 1x 1x 1x 10x 2x 2x 2x 2x 2x 42x 42x 14059x 14059x 42x 42x 2x 2x 2x 2x 2x 2x 14x 14x 4687x 4687x 14x 14x 2x 2x 2x 2x 2x 2x 42x 42x 9400x 9400x 9400x 9400x     9400x 9400x 29749x                     29749x 4721x 9400x 131301x 131301x 131301x 9399x 9399x 42x 42x 2x 2x 2x 2x 2x 2x 266x 266x 89125x 266x 266x 2x 2x 2x 2x 2x                 2x 2x 2x 2x 2x 2x 56x 56x 13058x     13058x 13058x     13058x 13058x 56x 56x 2x 2x 2x 2x 2x 140x 140x 7862x     7862x 140x 140x 2x 2x 2x 2x 2x 14x 14x 2x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 2x 2x 2x 2x 2x 14x 14x 286x     286x 14x 14x 2x 2x 1x 1x 1x  
import { error } from './errors.js';
 
/**
 * @template [Input=any]
 * @template [Output=Input]
 * @typedef {(input: Input, keypath: string) => Required<Output>} Validator
 */
 
const common = {
	filename: string(undefined),
 
	dev: boolean(false),
 
	generate: validator('client', (input, keypath) => {
		if (input === 'dom' || input === 'ssr') {
			warn(
				'`generate: "dom"` and `generate: "ssr"` options have been renamed to "client" and "server" respectively'
			);
			return input === 'dom' ? 'client' : 'server';
		}
 
		// TODO deprecate `false` in favour of `analyze`/`analyzeModule` https://github.com/sveltejs/svelte-octane/issues/655
		if (input !== 'client' && input !== 'server' && input !== false) {
			throw_error(`${keypath} must be "client", "server" or false`);
		}
 
		return input;
	})
};
 
export const validate_module_options =
	/** @type {Validator<import('#compiler').ModuleCompileOptions, import('#compiler').ValidatedModuleCompileOptions>} */ (
		object({
			...common
		})
	);
 
export const validate_component_options =
	/** @type {Validator<import('#compiler').CompileOptions, import('#compiler').ValidatedCompileOptions>} */ (
		object({
			...common,
 
			accessors: boolean(false),
 
			css: validator('external', (input) => {
				if (input === true || input === false) {
					throw_error(
						'The boolean options have been removed from the css option. Use "external" instead of false and "injected" instead of true'
					);
				}
				if (input === 'none') {
					throw_error(
						'css: "none" is no longer a valid option. If this was crucial for you, please open an issue on GitHub with your use case.'
					);
				}
 
				if (input !== 'external' && input !== 'injected') {
					throw_error(`css should be either "external" (default, recommended) or "injected"`);
				}
 
				return input;
			}),
 
			cssHash: fun(({ css, hash }) => {
				return `svelte-${hash(css)}`;
			}),
 
			// TODO this is a sourcemap option, would be good to put under a sourcemap namespace
			cssOutputFilename: string(undefined),
 
			customElement: boolean(false),
 
			discloseVersion: boolean(true),
 
			immutable: deprecate(
				'The immutable option has been deprecated. It has no effect in runes mode.',
				boolean(false)
			),
 
			legacy: object({
				componentApi: boolean(false)
			}),
 
			loopGuardTimeout: warn_removed('The loopGuardTimeout option has been removed.'),
 
			name: string(undefined),
 
			namespace: list(['html', 'svg', 'foreign']),
 
			outputFilename: string(undefined),
 
			preserveComments: boolean(false),
 
			preserveWhitespace: boolean(false),
 
			runes: boolean(undefined),
 
			hmr: boolean(false),
 
			sourcemap: validator(undefined, (input) => {
				// Source maps can take on a variety of values, including string, JSON, map objects from magic-string and source-map,
				// so there's no good way to check type validity here
				return input;
			}),
 
			enableSourcemap: warn_removed(
				'The enableSourcemap option has been removed. Source maps are always generated now, and tooling can choose to ignore them.'
			),
			hydratable: warn_removed(
				'The hydratable option has been removed. Svelte components are always hydratable now.'
			),
			format: removed(
				'The format option has been removed in Svelte 4, the compiler only outputs ESM now. Remove "format" from your compiler options. ' +
					'If you did not set this yourself, bump the version of your bundler plugin (vite-plugin-svelte/rollup-plugin-svelte/svelte-loader)'
			),
			tag: removed(
				'The tag option has been removed in Svelte 5. Use `<svelte:options customElement="tag-name" />` inside the component instead. ' +
					'If that does not solve your use case, please open an issue on GitHub with details.'
			),
			sveltePath: removed(
				'The sveltePath option has been removed in Svelte 5. ' +
					'If this option was crucial for you, please open an issue on GitHub with your use case.'
			),
			// These two were primarily created for svelte-preprocess (https://github.com/sveltejs/svelte/pull/6194),
			// but with new TypeScript compilation modes strictly separating types it's not necessary anymore
			errorMode: removed(
				'The errorMode option has been removed. If you are using this through svelte-preprocess with TypeScript, ' +
					'use the https://www.typescriptlang.org/tsconfig#verbatimModuleSyntax setting instead'
			),
			varsReport: removed(
				'The vars option has been removed. If you are using this through svelte-preprocess with TypeScript, ' +
					'use the https://www.typescriptlang.org/tsconfig#verbatimModuleSyntax setting instead'
			)
		})
	);
 
/**
 * @param {string} msg
 * @returns {Validator}
 */
function removed(msg) {
	return (input) => {
		if (input !== undefined) {
			error(null, 'removed-compiler-option', msg);
		}
		return /** @type {any} */ (undefined);
	};
}
 
const warned = new Set();
 
/** @param {string} message */
function warn(message) {
	if (!warned.has(message)) {
		warned.add(message);
		// eslint-disable-next-line no-console
		console.warn(message);
	}
}
 
/**
 * @param {string} message
 * @returns {Validator}
 */
function warn_removed(message) {
	return (input) => {
		if (input !== undefined) warn(message);
		return /** @type {any} */ (undefined);
	};
}
 
/**
 * @param {string} message
 * @param {Validator} validator
 * @returns {Validator}
 */
function deprecate(message, validator) {
	return (input, keypath) => {
		if (input !== undefined) warn(message);
		return validator(input, keypath);
	};
}
 
/**
 * @param {Record<string, Validator>} children
 * @param {boolean} [allow_unknown]
 * @returns {Validator}
 */
function object(children, allow_unknown = false) {
	return (input, keypath) => {
		/** @type {Record<string, any>} */
		const output = {};
 
		if ((input && typeof input !== 'object') || Array.isArray(input)) {
			throw_error(`${keypath} should be an object`);
		}
 
		for (const key in input) {
			if (!(key in children)) {
				if (allow_unknown) {
					output[key] = input[key];
				} else {
					error(
						null,
						'invalid-compiler-option',
						`Unexpected option ${keypath ? `${keypath}.${key}` : key}`
					);
				}
			}
		}
 
		for (const key in children) {
			const validator = children[key];
			output[key] = validator(input && input[key], keypath ? `${keypath}.${key}` : key);
		}
 
		return output;
	};
}
 
/**
 * @param {any} fallback
 * @param {(value: any, keypath: string) => any} fn
 * @returns {Validator}
 */
function validator(fallback, fn) {
	return (input, keypath) => {
		return input === undefined ? fallback : fn(input, keypath);
	};
}
 
/**
 * @param {number} fallback
 * @returns {Validator}
 */
function number(fallback) {
	return validator(fallback, (input, keypath) => {
		if (typeof input !== 'number') {
			throw_error(`${keypath} should be a number, if specified`);
		}
		return input;
	});
}
 
/**
 * @param {string | undefined} fallback
 * @param {boolean} allow_empty
 * @returns {Validator}
 */
function string(fallback, allow_empty = true) {
	return validator(fallback, (input, keypath) => {
		if (typeof input !== 'string') {
			throw_error(`${keypath} should be a string, if specified`);
		}
 
		if (!allow_empty && input === '') {
			throw_error(`${keypath} cannot be empty`);
		}
 
		return input;
	});
}
 
/**
 * @param {boolean | undefined} fallback
 * @returns {Validator}
 */
function boolean(fallback) {
	return validator(fallback, (input, keypath) => {
		if (typeof input !== 'boolean') {
			throw_error(`${keypath} should be true or false, if specified`);
		}
		return input;
	});
}
 
/**
 * @param {Array<boolean | string | number>} options
 * @returns {Validator}
 */
function list(options, fallback = options[0]) {
	return validator(fallback, (input, keypath) => {
		if (!options.includes(input)) {
			// prettier-ignore
			const msg = options.length > 2
				? `${keypath} should be one of ${options.slice(0, -1).map(input => `"${input}"`).join(', ')} or "${options[options.length - 1]}"`
				: `${keypath} should be either "${options[0]}" or "${options[1]}"`;
 
			throw_error(msg);
		}
		return input;
	});
}
 
/**
 * @param {(...args: any) => any} fallback
 * @returns {Validator}
 */
function fun(fallback) {
	return validator(fallback, (input, keypath) => {
		if (typeof input !== 'function') {
			throw_error(`${keypath} should be a function, if specified`);
		}
		return input;
	});
}
 
/** @param {string} msg */
function throw_error(msg) {
	error(null, 'invalid-compiler-option', msg);
}