All files / src/compiler/phases/3-transform index.js

94.07% Statements 143/152
90.47% Branches 19/21
100% Functions 3/3
93.79% Lines 136/145

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 1462x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4429x 112x 112x 112x 112x 112x 112x 112x 112x 112x 4317x 4317x 4317x 4429x 4429x 4429x 4429x 4429x 4317x 4317x 4317x 4317x 4317x 4317x 4317x 4317x 4317x 4317x 4317x 4317x 4317x 4317x 4317x 4317x 4317x 4317x 4317x 4317x 4317x 4429x 4429x 4429x 4429x 4429x 4429x 4429x 4429x 4429x 4429x 4429x 4429x 4429x 2x 2x 2x 2x 2x 2x 2x 2x 10x                   10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 2x 2x 2x 2x 2x 2x 2x 4439x 4439x 286x 286x 286x 286x 286x 286x 4227x 933x 933x 933x 933x 933x 933x 933x 933x 933x 933x 933x 933x 933x 933x 933x 933x 286x 286x 286x  
import { print } from 'esrap';
import { VERSION } from '../../../version.js';
import { server_component, server_module } from './server/transform-server.js';
import { client_component, client_module } from './client/transform-client.js';
import { getLocator } from 'locate-character';
import { render_stylesheet } from './css/index.js';
import { merge_with_preprocessor_map, get_source_name } from '../../utils/mapped_code.js';
 
/**
 * @param {import('../types').ComponentAnalysis} analysis
 * @param {string} source
 * @param {import('#compiler').ValidatedCompileOptions} options
 * @returns {import('#compiler').CompileResult}
 */
export function transform_component(analysis, source, options) {
	if (options.generate === false) {
		return {
			js: /** @type {any} */ (null),
			css: null,
			warnings: transform_warnings(source, options.filename, analysis.warnings),
			metadata: {
				runes: analysis.runes
			}
		};
	}
 
	const program =
		options.generate === 'server'
			? server_component(analysis, options)
			: client_component(source, analysis, options);
 
	const basename = (options.filename ?? 'Component').split(/[/\\]/).at(-1);
	if (program.body.length > 0) {
		program.body[0].leadingComments = [
			{
				type: 'Line',
				value: ` ${basename} (Svelte v${VERSION})`
			},
			{
				type: 'Line',
				value: ' Note: compiler output will change before 5.0 is released!'
			}
		];
	}
 
	const js_source_name = get_source_name(options.filename, options.outputFilename, 'input.svelte');
	const js = print(program, {
		// include source content; makes it easier/more robust looking up the source map code
		sourceMapContent: source,
		sourceMapSource: js_source_name
	});
	merge_with_preprocessor_map(js, options, js_source_name);
 
	const css =
		analysis.css.ast && !analysis.inject_styles
			? render_stylesheet(source, analysis, options)
			: null;
 
	return {
		js,
		css,
		warnings: transform_warnings(source, options.filename, analysis.warnings), // TODO apply preprocessor sourcemap
		metadata: {
			runes: analysis.runes
		}
	};
}
 
/**
 * @param {import('../types').Analysis} analysis
 * @param {string} source
 * @param {import('#compiler').ValidatedModuleCompileOptions} options
 * @returns {import('#compiler').CompileResult}
 */
export function transform_module(analysis, source, options) {
	if (options.generate === false) {
		return {
			js: /** @type {any} */ (null),
			css: null,
			warnings: transform_warnings(source, analysis.name, analysis.warnings),
			metadata: {
				runes: true
			}
		};
	}
 
	const program =
		options.generate === 'server'
			? server_module(analysis, options)
			: client_module(analysis, options);
 
	const basename = (options.filename ?? 'Module').split(/[/\\]/).at(-1);
	if (program.body.length > 0) {
		program.body[0].leadingComments = [
			{
				type: 'Block',
				value: ` ${basename} generated by Svelte v${VERSION} `
			}
		];
	}
 
	return {
		js: print(program, {}),
		css: null,
		warnings: transform_warnings(source, analysis.name, analysis.warnings),
		metadata: {
			runes: true
		}
	};
}
 
/**
 * @param {string} source
 * @param {string | undefined} name
 * @param {import('../types').RawWarning[]} warnings
 * @returns {import('#compiler').Warning[]}
 */
function transform_warnings(source, name, warnings) {
	if (warnings.length === 0) return [];
 
	const locate = getLocator(source, { offsetLine: 1 });
 
	/** @type {import('#compiler').Warning[]} */
	const result = [];
 
	for (const warning of warnings) {
		const start =
			warning.position &&
			/** @type {import('locate-character').Location} */ (locate(warning.position[0]));
 
		const end =
			warning.position &&
			/** @type {import('locate-character').Location} */ (locate(warning.position[1]));
 
		result.push({
			start,
			end,
			filename: name,
			message: warning.message,
			code: warning.code
		});
	}
 
	return result;
}