Skip to content

Commit dda232c

Browse files
example of intercepting a custom element definition
1 parent bbb01e0 commit dda232c

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

lib/runtime.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Proxy ?
2+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
3+
4+
// A
5+
// function customDefine(tagName, BaseClass) {
6+
// console.debug('intercepted customElement.define', { tagName, BaseClass });
7+
// }
8+
9+
// new Proxy(customElements.__proto__.define, customDefine)
10+
11+
// new Proxy(customElements.__proto__, {
12+
// define: customDefine
13+
// });
14+
15+
const backupDefine = customElements.define.bind(window.customElements);
16+
17+
window.customElements.define = (tagName, BaseClass) => {
18+
console.debug('intercepted customElement.define', { tagName, BaseClass });
19+
20+
if(BaseClass.__secret) {
21+
console.debug('hmmmm... wonder what could we do here????');
22+
BaseClass.__secret();
23+
}
24+
25+
backupDefine(tagName, BaseClass);
26+
}

ssr.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ app.get('/*', async (request, reply) => {
4949
<html>
5050
<head>
5151
<title>WCC - SSR</title>
52+
53+
<script src="./lib/runtime.js"></script>
54+
5255
${
5356
eagerJs.map(script => {
5457
return `<script type="module" src="${script.moduleURL.pathname.replace(process.cwd(), '')}"></script>`;

www/components/test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class TestComponent extends HTMLElement {
2+
constructor() {
3+
super()
4+
5+
// TODO have wcc skip over mode / attachShadow, innerHTML, connectedCallback, etc
6+
this.attachShadow({ mode: 'open' });
7+
}
8+
9+
static __secret() {
10+
console.debug('sssshhh! this is a secret :)')
11+
}
12+
}
13+
14+
export { TestComponent }
15+
16+
customElements.define('wcc-test', TestComponent)

www/pages/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import '../components/counter.js';
22
import '../components/footer.js';
33
import '../components/header.js';
4+
import '../components/test.js';
45

56
export default class HomePage extends HTMLElement {
67
constructor() {
@@ -29,6 +30,8 @@ export default class HomePage extends HTMLElement {
2930
3031
<wcc-header></wcc-header>
3132
33+
<wcc-test></wcc-test>
34+
3235
<h1>Home Page</h1>
3336
<wcc-counter></wcc-counter>
3437
<wcc-counter count="5"></wcc-counter>

0 commit comments

Comments
 (0)