Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,18 @@ function build_locations(nodes) {
}

/**
* In dev, returns a statement that assigns source locations to the elements of the current template.
* It must run after the template's content has been created, so that during hydration the elements
* belonging to child components (which already have their own locations) can be skipped
* In dev, returns a statement that assigns source locations after the template's content has been
* hydrated. Child components, snippets and `{@html ...}` tags then have their own locations, so we
* can skip their elements.
* @param {ComponentClientTransformState} state
* @param {Expression} template_name
* @param {Expression} node
* @returns {Statement | undefined}
*/
export function build_locations_statement(state, node) {
if (!dev) return;

const locations = build_locations(state.template.nodes);
if (locations.elements.length === 0) return;

return b.stmt(
b.call(
'$.add_locations',
node,
b.member(b.id(state.analysis.name), '$.FILENAME', true),
locations
)
);
export function build_hydration_locations_statement(state, template_name, node) {
if (dev && state.template.nodes.some((node) => node.type === 'element')) {
return b.stmt(b.call(b.member(template_name, 'hydrate_assign_locations'), node));
}
}

/**
Expand All @@ -71,13 +62,14 @@ export function transform_template(state, name, flags = 0) {

const expression = tree ? state.template.as_tree() : state.template.as_html();

const key = tree
? null
: get_template_key(
/** @type {TemplateLiteral} */ (expression),
state.metadata.namespace,
flags
);
const key =
tree || dev
? null
: get_template_key(
/** @type {TemplateLiteral} */ (expression),
state.metadata.namespace,
flags
);

if (key !== null) {
const existing = state.templates.get(key);
Expand All @@ -99,6 +91,19 @@ export function transform_template(state, name, flags = 0) {
call = b.call(`$.with_script`, call);
}

if (dev) {
const locations = build_locations(nodes);

if (locations.elements.length > 0) {
call = b.call(
'$.add_locations',
call,
b.member(b.id(state.analysis.name), '$.FILENAME', true),
locations
);
}
}

const id = state.scope.root.unique(name);
state.hoisted.push(b.var(id, call));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import * as b from '#compiler/builders';
import { TEMPLATE_FRAGMENT, TEMPLATE_USE_IMPORT_NODE } from '../../../../../constants.js';
import { clean_nodes, infer_namespace } from '../../utils.js';
import { build_locations_statement, transform_template } from '../transform-template/index.js';
import {
build_hydration_locations_statement,
transform_template
} from '../transform-template/index.js';
import { Template } from '../transform-template/template.js';
import { process_children } from './shared/fragment.js';
import { build_render_statement, Memoizer } from './shared/utils.js';
Expand Down Expand Up @@ -60,7 +63,7 @@ export function Fragment(node, context) {
let close = undefined;

/** @type {Statement | undefined} */
let locations = undefined;
let hydrate_locations = undefined;

/** @type {ComponentClientTransformState} */
const state = {
Expand Down Expand Up @@ -100,8 +103,8 @@ export function Fragment(node, context) {

const template_name = transform_template(state, 'root', flags);

hydrate_locations = build_hydration_locations_statement(state, template_name, id);
state.init.unshift(b.var(id, b.call(template_name)));
locations = build_locations_statement(state, id);
close = b.stmt(b.call('$.append', b.id('$$anchor'), id));
} else if (is_single_child_not_needing_template) {
context.visit(trimmed[0], state);
Expand Down Expand Up @@ -147,9 +150,9 @@ export function Fragment(node, context) {

const template_name = transform_template(state, 'root', flags);

hydrate_locations = build_hydration_locations_statement(state, template_name, id);
state.init.unshift(b.var(id, b.call(template_name)));

locations = build_locations_statement(state, id);
close = b.stmt(b.call('$.append', b.id('$$anchor'), id));
}
}
Expand All @@ -173,8 +176,8 @@ export function Fragment(node, context) {

body.push(...state.after_update);

if (locations !== undefined) {
body.push(locations);
if (hydrate_locations !== undefined) {
body.push(hydrate_locations);
}

if (close !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import { process_children, is_static_element } from './shared/fragment.js';
import { build_render_statement, build_template_chunk, Memoizer } from './shared/utils.js';
import { visit_event_attribute } from './shared/events.js';
import { Template } from '../transform-template/template.js';
import { build_locations_statement, transform_template } from '../transform-template/index.js';
import {
build_hydration_locations_statement,
transform_template
} from '../transform-template/index.js';
import { TEMPLATE_FRAGMENT } from '../../../../../constants.js';

/**
Expand Down Expand Up @@ -402,9 +405,13 @@ export function RegularElement(node, context) {
}
);

// Transform the template to $.from_html(...) and hoist it (deduplicating identical templates)
// Transform the template to $.from_html(...) and hoist it (deduplicating in production)
const template_name = transform_template(select_state, `${name}_content`, TEMPLATE_FRAGMENT);
const locations = build_locations_statement(select_state, fragment_id);
const hydrate_locations = build_hydration_locations_statement(
select_state,
template_name,
fragment_id
);

// Build the rich content function body
// The anchor is the child of the element (a hydration marker during hydration)
Expand All @@ -414,7 +421,7 @@ export function RegularElement(node, context) {
...select_state.init,
...(select_state.update.length > 0 ? [build_render_statement(select_state)] : []),
...select_state.after_update,
...(locations ? [locations] : []),
...(hydrate_locations ? [hydrate_locations] : []),
b.stmt(b.call('$.append', anchor_id, fragment_id))
]);

Expand Down
32 changes: 25 additions & 7 deletions packages/svelte/src/internal/client/dev/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,34 @@ import { hydrating } from '../dom/hydration.js';
import { component_context, dev_stack } from '../context.js';

/**
* Called once the template's content has been created. At that point the elements belonging
* to child components, snippets and `{@html ...}` tags (which during hydration aren't
* delimited by hydration markers) already have their own locations, so we skip them
* @param {DocumentFragment | Element} dom
* Assign locations immediately after cloning, before blocks or child components insert elements.
* During hydration, the compiler calls the factory's `hydrate_assign_locations` method after the
* template's children have their own locations and can be skipped.
* @param {() => DocumentFragment | Element} fn
* @param {string} filename
* @param {SourceLocation[]} locations
*/
export function add_locations(dom, filename, locations) {
var node = hydrating ? dom : dom.nodeType === DOCUMENT_FRAGMENT_NODE ? dom.firstChild : dom;
assign_locations(node, filename, locations);
export function add_locations(fn, filename, locations) {
var create = () => {
var dom = fn();

if (!hydrating) {
assign_locations(
dom.nodeType === DOCUMENT_FRAGMENT_NODE ? dom.firstChild : dom,
filename,
locations
);
}

return dom;
};

create.hydrate_assign_locations = (/** @type {Node} */ node) => {
// Pass the node explicitly because the same factory can be called repeatedly (e.g. in each blocks).
if (hydrating) assign_locations(node, filename, locations);
};

return create;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Stands in for a component that wasn't compiled with `dev: true`
// (or isn't a Svelte component at all), so its elements have no `__svelte_meta`
/** @param {Comment} anchor */
export default function Foreign(anchor) {
anchor.before(document.createElement('section'));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { test } from '../../test';

export default test({
// the stand-in component can't hydrate
mode: ['client'],
compileOptions: {
dev: true
},

test({ target, assert }) {
const [output] = target.querySelectorAll('output');
assert.htmlEqual(output.innerHTML, '<section></section><p>after</p>');

const [section] = output.querySelectorAll('section');
const [p] = output.querySelectorAll('p');

// The foreign component's element must not take the location of the following element
// @ts-expect-error
assert.equal(section.__svelte_meta, null);
// @ts-expect-error
assert.deepEqual(p.__svelte_meta?.loc, { file: 'main.svelte', line: 7, column: 1 });
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script lang="ts">
import Foreign from './Foreign.js';
</script>

<output>
<Foreign />
<p>after</p>
</output>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { tick } from 'svelte';
import { test } from '../../test';

export default test({
// the promise never resolves, so there is nothing to server-render
mode: ['client'],
skip_no_async: true,
compileOptions: {
dev: true
},

async test({ target, assert }) {
await tick();

const [output] = target.querySelectorAll('output');
assert.htmlEqual(output.innerHTML, '<section>loading</section><p>after</p>');

const [p] = output.querySelectorAll('p');

// The raw `pending` snippet's root has no location of its own,
// so it must not take the location of the following element
// @ts-expect-error
assert.deepEqual(p.__svelte_meta?.loc, { file: 'main.svelte', line: 13, column: 1 });
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script lang="ts">
import { createRawSnippet } from 'svelte';

const pending = createRawSnippet(() => ({
render: () => '<section>loading</section>'
}));
</script>

<output>
<svelte:boundary {pending}>
{await new Promise(() => {})}
</svelte:boundary>
<p>after</p>
</output>
Loading