fix: noop on the server for custom renderer components

pull/18359/head
paoloricciuti 3 months ago
parent dfd18af3c3
commit c58c5565c0

@ -6,7 +6,7 @@ import { walk } from 'zimmerframe';
import { set_scope } from '../../scope.js';
import { extract_identifiers } from '../../../utils/ast.js';
import * as b from '#compiler/builders';
import { component_name, dev, filename } from '../../../state.js';
import { component_name, custom_renderer, dev, filename } from '../../../state.js';
import { render_stylesheet } from '../css/index.js';
import { AssignmentExpression } from './visitors/AssignmentExpression.js';
import { AwaitBlock } from './visitors/AwaitBlock.js';
@ -90,6 +90,19 @@ const template_visitors = {
* @returns {ESTree.Program}
*/
export function server_component(analysis, options) {
// Components compiled with a custom renderer have no meaningful server
// representation — they only render on the client through the custom
// renderer — so we compile them down to a no-op.
if (custom_renderer) {
const component_function = b.function_declaration(b.id(analysis.name), [], b.block([]));
return {
type: 'Program',
sourceType: 'module',
body: [b.export_default(component_function)]
};
}
/** @type {ComponentServerTransformState} */
const state = {
analysis,

@ -0,0 +1,13 @@
import { test } from '../../test';
export default test({
compileOptions: {
experimental: {
// The actual renderer module is irrelevant for this snapshot — we only
// care that the server output is a no-op while the client output still
// imports/uses the custom renderer. Using a fixed path keeps the
// snapshot stable across machines.
customRenderer: 'my-custom-renderer'
}
}
});

@ -0,0 +1,17 @@
import $renderer from 'my-custom-renderer';
import 'svelte/internal/disclose-version';
import * as $ from 'svelte/internal/client';
var root = $.from_tree([['div', { class: 'greeting' }, ' ']]);
export default function Main($$anchor, $$props) {
var $$pop_renderer = $.push_renderer($renderer);
let name = $.prop($$props, 'name', 3, 'world');
var div = root();
var text = $.child(div);
$.reset(div);
$.template_effect(() => $.set_text(text, `Hello ${name() ?? ''}!`));
$.append($$anchor, div);
$$pop_renderer();
}

@ -0,0 +1,5 @@
<script>
let { name = 'world' } = $props();
</script>
<div class="greeting">Hello {name}!</div>
Loading…
Cancel
Save