Home / Function/ migrate() — svelte Function Reference

migrate() — svelte Function Reference

Architecture documentation for the migrate() function in index.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  ffed8565_a534_8183_a11f_bcffae15897e["migrate()"]
  cab41022_1b55_3b7a_06c6_b90763bbea47["index.js"]
  ffed8565_a534_8183_a11f_bcffae15897e -->|defined in| cab41022_1b55_3b7a_06c6_b90763bbea47
  e7276e70_1452_b660_f65a_7accd86f2a27["reset()"]
  ffed8565_a534_8183_a11f_bcffae15897e -->|calls| e7276e70_1452_b660_f65a_7accd86f2a27
  6d2356f8_1610_44a9_5698_878d5092688f["parse()"]
  ffed8565_a534_8183_a11f_bcffae15897e -->|calls| 6d2356f8_1610_44a9_5698_878d5092688f
  78a6ba9a_5003_f569_a638_76e4f1977809["analyze_component()"]
  ffed8565_a534_8183_a11f_bcffae15897e -->|calls| 78a6ba9a_5003_f569_a638_76e4f1977809
  11646a18_8daa_784f_c157_e7c537873a8e["guess_indent()"]
  ffed8565_a534_8183_a11f_bcffae15897e -->|calls| 11646a18_8daa_784f_c157_e7c537873a8e
  9148edcb_b5eb_bf41_8087_a1d28bac7f4d["unique()"]
  ffed8565_a534_8183_a11f_bcffae15897e -->|calls| 9148edcb_b5eb_bf41_8087_a1d28bac7f4d
  383a4823_60d9_d399_2d48_c9bad8d5ed7f["pop()"]
  ffed8565_a534_8183_a11f_bcffae15897e -->|calls| 383a4823_60d9_d399_2d48_c9bad8d5ed7f
  627dc2f8_4dbc_5bb1_8f54_cee503e17098["get()"]
  ffed8565_a534_8183_a11f_bcffae15897e -->|calls| 627dc2f8_4dbc_5bb1_8f54_cee503e17098
  c12e0147_3f27_cf17_5878_e54ffdc328d5["extract_identifiers()"]
  ffed8565_a534_8183_a11f_bcffae15897e -->|calls| c12e0147_3f27_cf17_5878_e54ffdc328d5
  7b7a865f_d980_e5dd_7be6_9dbf5e90264f["get_node_range()"]
  ffed8565_a534_8183_a11f_bcffae15897e -->|calls| 7b7a865f_d980_e5dd_7be6_9dbf5e90264f
  505bbcf7_7b25_33d0_b8fd_9e3920004c1c["migrate_css()"]
  ffed8565_a534_8183_a11f_bcffae15897e -->|calls| 505bbcf7_7b25_33d0_b8fd_9e3920004c1c
  style ffed8565_a534_8183_a11f_bcffae15897e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/migrate/index.js lines 124–459

export function migrate(source, { filename, use_ts } = {}) {
	let og_source = source;
	try {
		has_migration_task = false;
		// Blank CSS, could contain SCSS or similar that needs a preprocessor.
		// Since we don't care about CSS in this migration, we'll just ignore it.
		/** @type {Array<[number, string]>} */
		const style_contents = [];
		source = source.replace(regex_style_tags, (_, start, content, end, idx) => {
			style_contents.push([idx + start.length, content]);
			return start + style_placeholder + end;
		});

		reset({ warning: () => false, filename });

		let parsed = parse(source);

		const { customElement: customElementOptions, ...parsed_options } = parsed.options || {};

		/** @type {ValidatedCompileOptions} */
		const combined_options = {
			...validate_component_options({}, ''),
			...parsed_options,
			customElementOptions,
			filename: filename ?? UNKNOWN_FILENAME,
			experimental: {
				async: true
			}
		};

		const str = new MagicString(source);
		const analysis = analyze_component(parsed, source, combined_options);
		const indent = guess_indent(source);

		str.replaceAll(/(<svelte:options\s.*?\s?)accessors\s?/g, (_, $1) => $1);

		for (const content of style_contents) {
			str.overwrite(content[0], content[0] + style_placeholder.length, content[1]);
		}

		/** @type {State} */
		let state = {
			scope: analysis.instance.scope,
			analysis,
			filename,
			str,
			indent,
			props: [],
			props_insertion_point: parsed.instance?.content.start ?? 0,
			has_props_rune: false,
			has_type_or_fallback: false,
			end: source.length,
			names: {
				props: analysis.root.unique('props').name,
				rest: analysis.root.unique('rest').name,

				// event stuff
				run: analysis.root.unique('run').name,
				handlers: analysis.root.unique('handlers').name,
				stopImmediatePropagation: analysis.root.unique('stopImmediatePropagation').name,
				preventDefault: analysis.root.unique('preventDefault').name,
				stopPropagation: analysis.root.unique('stopPropagation').name,
				once: analysis.root.unique('once').name,
				self: analysis.root.unique('self').name,
				trusted: analysis.root.unique('trusted').name,
				createBubbler: analysis.root.unique('createBubbler').name,
				bubble: analysis.root.unique('bubble').name,
				passive: analysis.root.unique('passive').name,
				nonpassive: analysis.root.unique('nonpassive').name
			},
			legacy_imports: new Set(),
			script_insertions: new Set(),
			derived_components: new Map(),
			derived_conflicting_slots: new Map(),
			derived_labeled_statements: new Set(),
			has_svelte_self: false,
			uses_ts:
				// Some people could use jsdoc but have a tsconfig.json, so double-check file for jsdoc indicators
				(use_ts && !source.includes('@type {')) ||
				!!parsed.instance?.attributes.some(
					(attr) => attr.name === 'lang' && /** @type {any} */ (attr).value[0].data === 'ts'

Domain

Subdomains

Frequently Asked Questions

What does migrate() do?
migrate() is a function in the svelte codebase, defined in packages/svelte/src/compiler/migrate/index.js.
Where is migrate() defined?
migrate() is defined in packages/svelte/src/compiler/migrate/index.js at line 124.
What does migrate() call?
migrate() calls 10 function(s): analyze_component, extract_identifiers, get, get_node_range, guess_indent, migrate_css, parse, pop, and 2 more.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free