Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: functional template generation #15538

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
chore: simplify process_children
paoloricciuti committed Mar 20, 2025
commit 59902ccead473e054b158a4f806b60e34995d733
Original file line number Diff line number Diff line change
@@ -69,6 +69,7 @@ export function Fragment(node, context) {
template: [],
locations: [],
transform: { ...context.state.transform },
is_functional_template_mode: context.state.is_functional_template_mode,
metadata: {
context: {
template_needs_import_node: false,
@@ -125,40 +126,22 @@ export function Fragment(node, context) {
// special case — we can use `$.text` instead of creating a unique template
const id = b.id(context.state.scope.generate('text'));

process_children(
trimmed,
() => id,
false,
{
...context,
state
},
context.state.is_functional_template_mode
);
process_children(trimmed, () => id, false, {
...context,
state
});

body.push(b.var(id, b.call('$.text')));
close = b.stmt(b.call('$.append', b.id('$$anchor'), id));
} else {
if (is_standalone) {
// no need to create a template, we can just use the existing block's anchor
process_children(
trimmed,
() => b.id('$$anchor'),
false,
{ ...context, state },
context.state.is_functional_template_mode
);
process_children(trimmed, () => b.id('$$anchor'), false, { ...context, state });
} else {
/** @type {(is_text: boolean) => Expression} */
const expression = (is_text) => b.call('$.first_child', id, is_text && b.true);

process_children(
trimmed,
expression,
false,
{ ...context, state },
context.state.is_functional_template_mode
);
process_children(trimmed, expression, false, { ...context, state });

let flags = TEMPLATE_FRAGMENT;

Original file line number Diff line number Diff line change
@@ -375,7 +375,8 @@ export function RegularElement(node, context) {
locations: [],
scope: /** @type {Scope} */ (context.state.scopes.get(node.fragment)),
preserve_whitespace:
context.state.preserve_whitespace || node.name === 'pre' || node.name === 'textarea'
context.state.preserve_whitespace || node.name === 'pre' || node.name === 'textarea',
is_functional_template_mode: context.state.is_functional_template_mode
};

const { hoisted, trimmed } = clean_nodes(
@@ -430,16 +431,10 @@ export function RegularElement(node, context) {
arg = b.member(arg, 'content');
}

process_children(
trimmed,
(is_text) => b.call('$.child', arg, is_text && b.true),
true,
{
...context,
state: child_state
},
context.state.is_functional_template_mode
);
process_children(trimmed, (is_text) => b.call('$.child', arg, is_text && b.true), true, {
...context,
state: child_state
});

if (needs_reset) {
child_state.init.push(b.stmt(b.call('$.reset', context.state.node)));
Original file line number Diff line number Diff line change
@@ -15,15 +15,8 @@ import { build_template_chunk } from './utils.js';
* @param {(is_text: boolean) => Expression} initial
* @param {boolean} is_element
* @param {ComponentContext} context
* @param {boolean} [is_functional_template_mode]
*/
export function process_children(
nodes,
initial,
is_element,
{ visit, state },
is_functional_template_mode
) {
export function process_children(nodes, initial, is_element, { visit, state }) {
const within_bound_contenteditable = state.metadata.bound_contenteditable;
let prev = initial;
let skipped = 0;
@@ -74,7 +67,9 @@ export function process_children(
state.template.push({
kind: 'create_text',
args: [
sequence.map((node) => (is_functional_template_mode ? node.data : node.raw)).join('')
sequence
.map((node) => (state.is_functional_template_mode ? node.data : node.raw))
.join('')
]
});
return;
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ export function Fragment(node, context) {
context.state,
context.state.preserve_whitespace,
context.state.options.preserveComments,
// prevent template cloning should always be false on the server
// templating mode doesn't affect server builds
false
);

Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ export function RegularElement(node, context) {
},
state.preserve_whitespace,
state.options.preserveComments,
// prevent template cloning should always be false on the server
// templating mode doesn't affect server builds
false
);