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

add $$host #4534

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 9 additions & 0 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ export default class Component {
if (variable) {
variable.referenced = true;
} else if (is_reserved_keyword(name)) {
if (name === '$$host' && !this.compile_options.customElement) {
throw new Error(`$$host is for custom element. Did you forget the 'customElement: true' compile option?`);
}
this.add_var({
name,
injected: true,
Expand Down Expand Up @@ -655,6 +658,12 @@ export default class Component {
name,
injected: true,
});
if (name === '$$host' && !this.compile_options.customElement) {
this.error(node as any, {
code: 'illegal-host',
message: `$$host is for custom element. Did you forget the 'customElement: true' compile option?`
});
}
} else if (name[0] === '$') {
if (name === '$' || name[1] === '$') {
this.error(node as any, {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ export default function dom(

body.push(b`
function ${definition}(${args}) {
${component.var_lookup.has('$$host') ? 'const $$host = $$self' : null}
${rest}
${reactive_store_declarations}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/compile/utils/reserved_keywords.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const reserved_keywords = new Set(["$$props", "$$restProps"]);
export const reserved_keywords = new Set(["$$props", "$$restProps", "$$host"]);

export function is_reserved_keyword(name) {
return reserved_keywords.has(name);
Expand Down
26 changes: 20 additions & 6 deletions test/custom-elements/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,24 @@ describe('custom-elements', function() {
}

before(async () => {
svelte = loadSvelte();
server = await create_server();
browser = await puppeteer.launch();
try {
svelte = loadSvelte();
server = await create_server();
browser = await puppeteer.launch();
console.log('Success!');
} catch (error) {
console.log('Error', error);
}
});

after(async () => {
server.close();
await browser.close();
try {
server.close();
await browser.close();
console.log('Success!');
} catch (error) {
console.log('Error', error);
}
});

fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
Expand Down Expand Up @@ -105,7 +115,11 @@ describe('custom-elements', function() {

const page = await browser.newPage();

page.on('console', (type, ...args) => {
page.on('console', async (consoleMessage) => {
const type = consoleMessage.type();
const args = await Promise.all(
consoleMessage.args().map(arg => arg.jsonValue())
)
console[type](...args);
});

Expand Down
11 changes: 11 additions & 0 deletions test/custom-elements/samples/$$host/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<svelte:options tag="custom-element"/>

<script>
export function getHost() {
return $$host;
}
export const host = $$host;
</script>

{typeof $$host}
12 changes: 12 additions & 0 deletions test/custom-elements/samples/$$host/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as assert from 'assert';
import './main.svelte';

export default async function (target) {
target.innerHTML = '<custom-element></custom-element>';
const el = target.querySelector('custom-element');

assert.equal(el.getHost(), el);
assert.equal(el.host, el);

assert.equal(el.shadowRoot.textContent, 'object');
}
4 changes: 4 additions & 0 deletions test/runtime/samples/$$host-2/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
compileOptions: {},
error: "$$host is for custom element. Did you forget the 'customElement: true' compile option?"
};
1 change: 1 addition & 0 deletions test/runtime/samples/$$host-2/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{$$host}
4 changes: 4 additions & 0 deletions test/runtime/samples/$$host/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
compileOptions: {},
error: "$$host is for custom element. Did you forget the 'customElement: true' compile option?"
};
3 changes: 3 additions & 0 deletions test/runtime/samples/$$host/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script>
export const host = $$host;
</script>