Skip to content

Commit e60384f

Browse files
committed
add $$host
1 parent 91d758e commit e60384f

File tree

10 files changed

+51
-2
lines changed

10 files changed

+51
-2
lines changed

src/compiler/compile/Component.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ export default class Component {
187187
if (variable) {
188188
variable.referenced = true;
189189
} else if (is_reserved_keyword(name)) {
190+
if (name === '$$host' && !this.compile_options.customElement) {
191+
throw new Error(`$$host is for custom element. Did you forget the 'customElement: true' compile option?`);
192+
}
190193
this.add_var({
191194
name,
192195
injected: true,
@@ -655,6 +658,12 @@ export default class Component {
655658
name,
656659
injected: true,
657660
});
661+
if (name === '$$host' && !this.compile_options.customElement) {
662+
this.error(node as any, {
663+
code: 'illegal-host',
664+
message: `$$host is for custom element. Did you forget the 'customElement: true' compile option?`
665+
});
666+
}
658667
} else if (name[0] === '$') {
659668
if (name === '$' || name[1] === '$') {
660669
this.error(node as any, {

src/compiler/compile/render_dom/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ export default function dom(
412412

413413
body.push(b`
414414
function ${definition}(${args}) {
415+
${component.var_lookup.has('$$host') ? 'const $$host = $$self' : null}
415416
${rest}
416417
417418
${reactive_store_declarations}

src/compiler/compile/utils/reserved_keywords.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const reserved_keywords = new Set(["$$props", "$$restProps"]);
1+
export const reserved_keywords = new Set(["$$props", "$$restProps", "$$host"]);
22

33
export function is_reserved_keyword(name) {
44
return reserved_keywords.has(name);

test/custom-elements/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ describe('custom-elements', function() {
105105

106106
const page = await browser.newPage();
107107

108-
page.on('console', (type, ...args) => {
108+
page.on('console', async (consoleMessage) => {
109+
const type = consoleMessage.type();
110+
const args = await Promise.all(
111+
consoleMessage.args().map(arg => arg.jsonValue())
112+
)
109113
console[type](...args);
110114
});
111115

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<svelte:options tag="custom-element"/>
2+
3+
<script>
4+
export function getHost() {
5+
return $$host;
6+
}
7+
8+
export const host = $$host;
9+
</script>
10+
11+
{typeof $$host}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as assert from 'assert';
2+
import './main.svelte';
3+
4+
export default async function (target) {
5+
target.innerHTML = '<custom-element></custom-element>';
6+
const el = target.querySelector('custom-element');
7+
8+
assert.equal(el.getHost(), el);
9+
assert.equal(el.host, el);
10+
11+
assert.equal(el.shadowRoot.textContent, 'object');
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
compileOptions: {},
3+
error: "$$host is for custom element. Did you forget the 'customElement: true' compile option?"
4+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{$$host}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
compileOptions: {},
3+
error: "$$host is for custom element. Did you forget the 'customElement: true' compile option?"
4+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script>
2+
export const host = $$host;
3+
</script>

0 commit comments

Comments
 (0)