Internals
WARNING
Single File Component (SFC) support for Administration extensions is experimental. It is available since Shopware 6.7.16.0. The APIs described on this page can still change without a deprecation. Try it out and tell us what you find, but do not ship an extension built on it to customers or to the Shopware Store until the API is declared stable (planned for 6.9).
DANGER
This page describes internals. Nothing on it is API: the generated code and every name in it change without a deprecation, and an extension must not depend on them. Write against the API reference instead.
Vue has no extension mechanism of the kind this page describes. Nothing in Vue lets one file replace a binding of another, or lets a plugin drop markup into the middle of another component's template.
Shopware adds that mechanism at build time. Before Vue sees your .vue file, a transform rewrites it into ordinary Composition API code that talks to the Administration's extension system, and hands that to Vue. Vue compiles a normal component and never learns that an extension system exists.
What follows is how that rewrite works, and what the block components do once the result is running.
The short version
At build time
- A Vite plugin picks up the
.vuefile and decides from its name whether it is a base component or an override, and which component it belongs to. - The transform rewrites the script. A base component's bindings are put behind a middleware, so an override can replace them. An override's whole body becomes a callback for the extension system to call.
- The transform rewrites the template, adding the bindings
sw-blockneeds to carry state between the two.
At runtime
- Every override component is mounted once at boot. That registers its
setup()and each of its<sw-block extends>slots. It renders none of them. - When a base component is set up, its own
setup()runs first, then thesetup()of each override, one after another, all inside the base component's own setup. - Each
sw-blockof the base renders the last contribution registered for its component and name, and each<sw-block-parent />inside it renders the one before, recursively down to the base's own content. - Because an override's markup is rendered by the base component, and its
setup()has been folded into the base component's setup too, the bindings Vue would normally connect to a template cannot reach it. The transform routes them through a generated#default="{ ... }"destructuring instead, which is why it still feels like ordinary Vue from the outside.
How a .vue file becomes a component
Two Vite plugins and one transform, all of them in src/Administration/Resources/app/administration/build/.
The filename is the identity
There is no registration argument to get wrong, because the transform reads the identity off the path:
| File | Mode | Component |
|---|---|---|
swag-margin-hint.vue | base | swag-margin-hint |
swag-margin-hint/index.vue | base | swag-margin-hint |
sw-product-detail-base.override.vue | override | sw-product-detail-base |
swag-margin-hint/index.override.vue | override | swag-margin-hint |
That name is baked into the generated code as a string literal, which is why renaming the file changes what the component is called and what an override targets.
Registration is not symmetric
An override needs no registration at all. shopware-vite-plugin-override-component walks your extension for *.override.vue files, sorts them, and rewrites your entry file to import and register each one:
/* Start: auto generated by Shopware */
import _swOverride0 from './component/swag-margin-hint/swag-margin-hint.override.vue';
Shopware.Component.registerOverrideComponent(_swOverride0);
/* End: auto generated by Shopware */The files are sorted because a directory walk follows filesystem order, which differs between machines, and the import numbering follows the walk. Sorting keeps the generated entry identical across builds.
A new base component is different. Nothing scans for it. Usually an import is enough; to use its tag by name, you register it yourself:
Shopware.Component.register('swag-margin-hint', async () => {
const component = (await import('./swag-margin-hint.vue')).default;
return { ...component, _renderedBySfcTemplate: true } as never;
});_renderedBySfcTemplate is needed because a production build inlines the render function into setup(), where the component factory does not find it, and refuses to build the component without the flag.
A base .vue file that replaces an existing component needs no registration either - the component is already registered, and the filename is what makes your file the one that is loaded.
The transform runs behind a virtual file
shopware-vite-plugin-shopware-setup runs with enforce: 'pre', ahead of Vue's own SFC plugin. It redirects the import to a virtual id - <realpath>.shopware-setup.vue - and serves the rewritten source from there.
The detour keeps sourcemaps correct. Vue compiles the rewritten file under a path of its own, while the sourcemap the transform emits points every generated line back at the line you wrote, so a stack trace and a breakpoint land in your file rather than in generated code.
The same transform, three times
The transform is exposed through a CommonJS entry point, because it has three consumers that all need it synchronously:
| Consumer | What it gives you |
|---|---|
| The Vite build | The code that actually runs |
| The Jest transformer | The Administration's own tests; extension tests are not supported yet, see the roadmap |
The valid-shopware-setup ESLint rule | The same error in your editor, before you build |
So an invalid file fails the same way in all three places, with the same message. There is no separate linter that can drift out of sync with the compiler.
The base transform
A base component keeps its body. Nothing is hoisted, nothing is wrapped, every Vue macro stays where you put it. The transform only renames and appends.
Everything below is the output of the real transform on this file, with blank lines collapsed:
<!-- swag-margin-hint.vue -->
<template>
<div class="swag-margin-hint">
<sw-block name="swag_margin_hint_banner">
<mt-banner :variant="variant">{{ message }}</mt-banner>
</sw-block>
</div>
</template>
<script setup lang="ts">
import { computed } from 'vue';
const { warnBelow = 0.2 } = defineProps<{ warnBelow?: number }>();
const margin = computed(() => 0.15);
const variant = computed(() => (margin.value < warnBelow ? 'critical' : 'positive'));
const message = computed(() => `Margin ${Math.round(margin.value * 100)}%`);
swDefinePublic({ margin, variant, message });
</script>1. Every top-level binding is renamed
const __swSetupAuthor_margin = computed(() => 0.15);
const __swSetupAuthor_variant = computed(() => (__swSetupAuthor_margin.value < warnBelow ? 'critical' : 'positive'));Your margin becomes __swSetupAuthor_margin, and every reference to it is renamed with it. The __swSetup prefix is reserved - the transform rejects it in author code - which is what makes a collision impossible.
warnBelow is untouched. It comes from defineProps(), which stays native, so props never take part in the rename.
2. The macro becomes a footer
const {
margin,
variant,
message,
} = Shopware.Component.attachOverrides({
name: 'swag-margin-hint',
public: {
margin: __swSetupAuthor_margin,
variant: __swSetupAuthor_variant,
message: __swSetupAuthor_message,
},
private: {},
});The swDefinePublic() call itself is deleted. What it leaves behind is the public map, and the defineExpose() described below. Every other top-level binding lands in private:
// a binding you did not publish
private: {
count: __swSetupAuthor_count,
},The difference between the two maps is what an override may replace. Both are handed over, because a private binding still has to be readable by the template - it just cannot be overridden.
Then the original names are declared again, by destructuring the return. So the template still writes variant and message, but reads them through the extension system rather than from your const directly.
That destructuring is the middleware from the short version: your file is untouched above the footer, every name it exposes now passes through a layer in between, and that layer is what an override replaces.
This is also why a top-level binding must not share a declared prop's name. The extension system strips declared prop keys from what it returns, so the destructured binding would be undefined.
The footer ends with a generated defineExpose():
defineExpose({
...Shopware.Component.getExposedProps(),
margin,
variant,
message,
});It exposes the published names, after any override has replaced them, plus the component's props as read-only refs. So a parent that holds a template ref to the component reads the same things from it that it could read from an Options API component. That is why you never write defineExpose() yourself - the build rejects it.
3. The template gains two attributes
<sw-block sw-internal-component-name='swag-margin-hint' :data="$dataScope" name="swag_margin_hint_banner">sw-internal-component-name is the component the file belongs to, taken from its filename. A block is identified by that component name and its block name, the same way Twig scopes its blocks. So a block called swag_margin_hint_banner in another component never picks up overrides meant for this one.
$dataScope is the component's state, resolved for this instance. It is what sw-block passes to every contributor of the block - see Blocks at runtime.
Authoring either attribute yourself is rejected, and so is any other attribute or directive on sw-block. The transform owns them completely, so what a block receives is always exactly what it wired.
The whole file
swag-margin-hint.vue, after the transform
<template>
<div class="swag-margin-hint">
<sw-block sw-internal-component-name='swag-margin-hint' :data="$dataScope" name="swag_margin_hint_banner">
<mt-banner :variant="variant">{{ message }}</mt-banner>
</sw-block>
</div>
</template>
<script setup lang="ts">
import { computed } from 'vue';
const { warnBelow = 0.2 } = defineProps<{ warnBelow?: number }>();
const __swSetupAuthor_margin = computed(() => 0.15);
const __swSetupAuthor_variant = computed(() => (__swSetupAuthor_margin.value < warnBelow ? 'critical' : 'positive'));
const __swSetupAuthor_message = computed(() => `Margin ${Math.round(__swSetupAuthor_margin.value * 100)}%`);
const {
margin,
variant,
message,
} = Shopware.Component.attachOverrides({
name: 'swag-margin-hint',
public: {
margin: __swSetupAuthor_margin,
variant: __swSetupAuthor_variant,
message: __swSetupAuthor_message,
},
private: {},
});
defineExpose({
...Shopware.Component.getExposedProps(),
margin,
variant,
message,
});
</script>It is still a plain <script setup> component, and Vue compiles it with no knowledge that an extension system exists.
The override transform
An override is rewritten more heavily than a base component, because it is not a component in the sense you wrote it. It is a callback plus a slot.
The file:
<!-- swag-margin-hint.override.vue -->
<template>
<sw-block extends="swag_margin_hint_banner">
<sw-block-parent />
<p>{{ hint }}</p>
</sw-block>
</template>
<script setup lang="ts">
import { computed } from 'vue';
const previousState = useSwPreviousState();
const hint = computed(() => `${previousState.message.value} - check your purchasing conditions.`);
const message = computed(() => 'Replaced message');
swDefineOverride({ message });
</script>1. The body becomes a callback
Shopware.Component.overrideComponentSetup()('swag-margin-hint', (__swSetupPreviousState, __swSetupProps, __swSetupContext) => {
// your code
});Your whole <script setup> body moves inside that arrow function. It no longer runs when the file is loaded; it runs each time the component you override is set up.
2. The override composables are locals
const useSwPreviousState = () => __swSetupPreviousState;
const useSwProps = () => __swSetupProps;
const useSwContext = () => __swSetupContext;Three lines, generated at the top of the callback. That is the entire implementation.
It explains both of their properties at once. They need no import because they are declared right above your code, and they exist only in an override because only an override has a callback with those three parameters to close over.
3. The macro becomes the return
return {
message,
};swDefineOverride({ message }) disappears and its names become the returned object - the bindings the extension system installs over the base component's. A swDefineOverride({}) returns {}, which is the common case for an override that only contributes markup.
4. Your other bindings travel in the slot scope
hint is not declared as an override - it is a local your template uses. It cannot be read from the override's scope at render time, because the markup does not render there. It renders inside the base component. So the transform sends it along, namespaced per file:
const __swSetupNamespace = Symbol('swag-margin-hint.override');
return {
message,
__swOverride: {
[__swSetupNamespace]: {
hint,
},
},
};and generates the matching slot scope on the block:
<sw-block sw-internal-component-name='swag-margin-hint' extends="swag_margin_hint_banner" #default="{ __swOverride: { [__swSetupNamespace]: { hint } } }">
<sw-block-parent />
<p>{{ hint }}</p>
</sw-block>Your hint arrives in the template as a slot-scope local, destructured out of the argument the base component passes when it calls your slot. The Symbol is per file, which is why two plugins extending the same block never see each other's locals.
Two consequences follow from this, and both are enforced at build time:
- A write in the template silently does nothing.
@click="hint = 'x'"assigns to a slot-scope local that is thrown away on the next render. The identical line works in a base component, so the transform rejects it in an override rather than letting it fail silently: mutate from a method in your setup and call that. - Slot scope bindings are unwrapped. They arrive as values, not refs, because that is what slot destructuring does.
An override with no template locals gets no generated slot scope at all - there is nothing to send.
5. The template may only contain blocks
An override's template is rejected if anything other than <sw-block extends> appears at its top level. The reason: the override file is mounted in a hidden container, so any markup outside a block would render there - invisibly, with its setup references resolving against the hidden component instead of the component you meant to extend.
6. The block targets are announced
Shopware.Component.registerNativeExtensionTargets?.({
component: 'swag-margin-hint',
blocks: [
'swag_margin_hint_banner',
],
});The transform adds a second, plain <script> that lists every block the file extends. It runs when the file is loaded, before any component renders. A Twig component reads that list when its template is compiled, and wraps exactly those {% block %}s in an sw-block - see Twig interop. The ?. keeps a plugin built today from failing on an Administration that does not have the function.
The whole file
swag-margin-hint.override.vue, after the transform
<script lang="ts">
Shopware.Component.registerNativeExtensionTargets?.({
component: 'swag-margin-hint',
blocks: [
'swag_margin_hint_banner',
],
});
</script>
<template>
<sw-block sw-internal-component-name='swag-margin-hint' extends="swag_margin_hint_banner" #default="{ __swOverride: { [__swSetupNamespace]: { hint } } }">
<sw-block-parent />
<p>{{ hint }}</p>
</sw-block>
</template>
<script setup lang="ts">
import { computed } from 'vue';
const __swSetupNamespace = Symbol('swag-margin-hint.override');
Shopware.Component.overrideComponentSetup()('swag-margin-hint', (__swSetupPreviousState, __swSetupProps, __swSetupContext) => {
const useSwPreviousState = () => __swSetupPreviousState;
const useSwProps = () => __swSetupProps;
const useSwContext = () => __swSetupContext;
const previousState = useSwPreviousState();
const hint = computed(() => `${previousState.message.value} - check your purchasing conditions.`);
const message = computed(() => 'Replaced message');
return {
message,
__swOverride: {
[__swSetupNamespace]: {
hint,
},
},
};
});
</script>The file is still a component, and it is still mounted - but only so that the two registrations happen. The callback registers the state replacement; rendering the template registers the block content. Neither produces visible markup of its own.
Blocks at runtime
sw-block is an ordinary Vue component with two modes, and a module-level registry between them.
The registry
One reactive object, shared by the whole application, mapping a block to the slot functions registered for it:
const blockContext: Record<string, Slot[]> = reactive({});The key is the component name plus the block name, 'swag-margin-hint swag_margin_hint_banner', built from the sw-internal-component-name the transform stamped on the block. That is why an override has to be named after the component that renders the block.
An extension never touches it. The two modes of sw-block are its only writer and its only reader.
extends registers and renders nothing
if (props.extends) {
const extendsKey = componentBlockKey(props.swInternalComponentName, props.extends);
const overrideSlot: Slot = (data) => slots.default?.(data) ?? [];
addBlock(extendsKey, overrideSlot);
onBeforeUnmount(() => removeBlock(extendsKey, overrideSlot));
return { template: null };
}That is the whole contribution path. <sw-block extends> hands its default slot to the registry and renders nothing at its own position - which is why an override's markup appears where the base component declared the block, not where you wrote it.
It registers a small wrapper rather than the slot function itself. Vue hands the component a new slot function whenever the surrounding scope changes, and the wrapper always calls the current one.
name calls the chain
const nativeBlocks = getBlocks(componentBlockKey(props.swInternalComponentName, props.name));
const blocksAndParent = [
slots.default ?? (() => []),
...shimSlots,
...nativeBlocks,
];
const blocksNodes = blocksAndParent.map((block) => block?.(props.data));
const lastNode = blocksNodes.pop();
providedParents.value = blocksNodes;
return lastNode;Each contributor is a function, and the block calls it with props.data.
props.data is the :data="$dataScope" the transform generated - the base component's own state. So an override's markup is executed by the base component, during the base component's render, with the base component's state as its slot argument. Everything the override's template can see arrives through that one argument, its own locals included.
The chain is then resolved by position:
| Position | Who |
|---|---|
| First | The block's own default content |
| Middle | Legacy Twig overrides, through the shim |
| Last | Native <sw-block extends> contributions, in registration order |
The last entry is what the block renders. Everything before it is provided to the descendants under an injection key, for sw-block-parent to pick up.
So a Twig override of the same block always sits earlier in the chain than a native one, whatever order the plugins load in: the native override's <sw-block-parent /> renders it.
sw-block-parent pops one entry
const parents = inject(parentsInjectionKey, null);
const initialParents = parents?.value;
const initialParent = initialParents?.pop();
const parentIndex = initialParents ? initialParents.length : -1;Each <sw-block-parent /> claims one entry from the stack, once, in its own setup(), and remembers the index it claimed. Later renders read the current node at that index.
Claiming happens at creation, which is the reason for the rule: a <sw-block-parent /> must render unconditionally and exactly once per extending block. Put it in a v-if or a v-for and the claims no longer line up with the stack, and the chain renders the wrong content.
name is static
Changing the name prop after mount is not supported - the shim slots are built once in setup() and the registry binding is made once. A development build warns when the prop changes; a production build does not.
Twig interop
Most of the Administration still ships an index.js and a .html.twig, while converted components ship a .vue. Both directions of the mix work, and each is a shim.
A Twig base, extended by a .vue override
The component's Twig template is compiled with an sw-block wrapped around each {% block %} that a .vue override extends, so what was a TwigJS extension point becomes a native one:
{# what the component ships #}
{% block sw_product_detail_base_price_form %}
<sw-container>...</sw-container>
{% endblock %}<!-- what the factory hands to Vue -->
<sw-block name="sw_product_detail_base_price_form">
<sw-container>...</sw-container>
</sw-block>Your override then contributes to it like it would to any other block, with no sign that the other side is Twig:
<template>
<sw-block extends="sw_product_detail_base_price_form">
<sw-block-parent />
<p>Your markup</p>
</sw-block>
</template>The second half is state. The component's own state lives on an Options API instance, and your override's lives in a setup(). A bridge sits between them: it presents the Options API instance as the previous state your override reads, and applies what your override returns back onto the instance.
const previousState = useSwPreviousState();
previousState.product.value; // a `data` property of the Options API component
previousState.onSave(); // one of its `methods`So useSwPreviousState() gives you the data, computed and methods of a component that has never heard of the Composition API.
A .vue base, extended by a Twig override
The same in reverse. A base component declares its blocks natively:
<template>
<sw-block name="swag_margin_hint_banner">
<mt-banner :variant="variant">{{ message }}</mt-banner>
</sw-block>
</template>and a Twig override of that block
{% block swag_margin_hint_banner %}
{% parent %}
<p>Added from a Twig override</p>
{% endblock %}joins the chain as though it had been written as one:
<sw-block extends="swag_margin_hint_banner">
<sw-block-parent />
<p>Added from a Twig override</p>
</sw-block>{% parent %} becomes <sw-block-parent />, and the bridge presents the base component's setup state to the override as the this a Twig template expects.
The v-if shim
Wrapping a {% block %} in a component has one consequence worth naming. A Twig template is free to open a v-if in one block and close it with a v-else in another:
{% block sw_example_toolbar_save %}
<mt-button v-if="isEditing">Save</mt-button>
{% endblock %}
{% block sw_example_toolbar_hint %}
<span v-else>Read only</span>
{% endblock %}That worked because the blocks were text: the compiled template was one flat piece of markup, and the two branches ended up as siblings. Once each block is its own component, they are not siblings any more, and Vue only pairs a v-else with a v-if that directly precedes it.
So the conditionals are compiled into helper calls instead of into plain v-if directives. Each branch records its result in a chain shared by the block, and a later v-else asks the chain what the earlier branches decided rather than relying on being adjacent to them. The chain is segmented in render order - the block's own content, then Twig contributions, then native ones - so a branch can see the results of everything rendered before it, wherever that happened to be.
A condition split across Twig blocks therefore keeps working after the component is converted, with no change to the template that wrote it.
What the shims do not cover
They translate the shapes templates actually take, not every shape a template could take. Ordinary Twig comes through - blocks, {% parent %}, nesting, a condition split across blocks. A template that plays games with the block structure itself can still fall through, and there is no shim that will rescue it.
A block whose content mixes a named slot template with other markup cannot host a native extension point at all. Your .vue override of that block is ignored, and a development build warns with [TemplateFactory] The block "…" cannot host a native extension point: ….