wc-compiler 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/wcc.dist.cjs CHANGED
@@ -59,6 +59,18 @@ class Element extends Node$1 {
59
59
  this.attributes = {};
60
60
  }
61
61
 
62
+ attachShadow(options) {
63
+ this.shadowRoot = new ShadowRoot(options);
64
+
65
+ return this.shadowRoot;
66
+ }
67
+
68
+ // https://github.com/mfreed7/declarative-shadow-dom/blob/master/README.md#serialization
69
+ // eslint-disable-next-line
70
+ getInnerHTML() {
71
+ return this.shadowRoot ? this.shadowRoot.innerHTML : this.innerHTML;
72
+ }
73
+
62
74
  setAttribute(name, value) {
63
75
  this.attributes[name] = value;
64
76
  }
@@ -96,19 +108,7 @@ class Document extends Node$1 {
96
108
  // https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement
97
109
  // EventTarget <- Node <- Element <- HTMLElement
98
110
  class HTMLElement extends Element {
99
- attachShadow(options) {
100
- this.shadowRoot = new ShadowRoot(options);
101
-
102
- return this.shadowRoot;
103
- }
104
-
105
111
  connectedCallback() { }
106
-
107
- // https://github.com/mfreed7/declarative-shadow-dom/blob/master/README.md#serialization
108
- // eslint-disable-next-line
109
- getInnerHTML(options = {}) {
110
- return this.shadowRoot.innerHTML;
111
- }
112
112
  }
113
113
 
114
114
  // https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
@@ -27658,6 +27658,7 @@ function getParser(moduleURL) {
27658
27658
  };
27659
27659
  }
27660
27660
 
27661
+ // replace all instances of __this__ marker with relative reference to the custom element parent node
27661
27662
  function applyDomDepthSubstitutions(tree, currentDepth = 1, hasShadowRoot = false) {
27662
27663
  try {
27663
27664
  for (const node of tree.childNodes) {
@@ -27670,9 +27671,9 @@ function applyDomDepthSubstitutions(tree, currentDepth = 1, hasShadowRoot = fals
27670
27671
  const { value } = attrs[attr];
27671
27672
 
27672
27673
  if (value.indexOf('__this__.') >= 0) {
27673
- const root = hasShadowRoot ? 'parentNode.host' : 'parentElement';
27674
+ const root = hasShadowRoot ? '.getRootNode().host' : `${'.parentElement'.repeat(currentDepth)}`;
27674
27675
 
27675
- node.attrs[attr].value = value.replace(/__this__/g, `this${'.parentElement'.repeat(currentDepth - 1)}.${root}`);
27676
+ node.attrs[attr].value = value.replace(/__this__/g, `this${root}`);
27676
27677
  }
27677
27678
  }
27678
27679
  }
@@ -27885,8 +27886,26 @@ function parseJsx(moduleURL) {
27885
27886
 
27886
27887
  applyDomDepthSubstitutions(elementTree, undefined, hasShadowRoot);
27887
27888
 
27888
- const finalHtml = serialize(elementTree);
27889
- const transformed = parse$1(`${elementRoot}.innerHTML = \`${finalHtml}\`;`, {
27889
+ const serializedHtml = serialize(elementTree);
27890
+ // we have to Shadow DOM use cases here
27891
+ // 1. No shadowRoot, so we attachShadow and append the template
27892
+ // 2. If there is root from the attachShadow signal, so we just need to inject innerHTML, say in an htmx
27893
+ // could / should we do something else instead of .innerHTML
27894
+ // https://github.com/ProjectEvergreen/wcc/issues/138
27895
+ const renderHandler = hasShadowRoot
27896
+ ? `
27897
+ const template = document.createElement('template');
27898
+ template.innerHTML = \`${serializedHtml}\`;
27899
+
27900
+ if(!${elementRoot}) {
27901
+ this.attachShadow({ mode: 'open' });
27902
+ this.shadowRoot.appendChild(template.content.cloneNode(true));
27903
+ } else {
27904
+ this.shadowRoot.innerHTML = template.innerHTML;
27905
+ }
27906
+ `
27907
+ : `${elementRoot}.innerHTML = \`${serializedHtml}\`;`;
27908
+ const transformed = parse$1(renderHandler, {
27890
27909
  ecmaVersion: 'latest',
27891
27910
  sourceType: 'module'
27892
27911
  });
@@ -27920,15 +27939,7 @@ function parseJsx(moduleURL) {
27920
27939
  for (const line of tree.body) {
27921
27940
  // test for class MyComponent vs export default class MyComponent
27922
27941
  if (line.type === 'ClassDeclaration' || (line.declaration && line.declaration.type) === 'ClassDeclaration') {
27923
- const children = !line.declaration
27924
- ? line.body.body
27925
- : line.declaration.body.body;
27926
- for (const method of children) {
27927
- if (method.key.name === 'constructor') {
27928
- insertPoint = method.start - 1;
27929
- break;
27930
- }
27931
- }
27942
+ insertPoint = line.declaration.body.start + 1;
27932
27943
  }
27933
27944
  }
27934
27945
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wc-compiler",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "Experimental native Web Components compiler.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,11 +28,12 @@
28
28
  },
29
29
  "scripts": {
30
30
  "clean": "rimraf ./dist",
31
- "lint": "ls-lint && eslint \"*.*js\" \"./src/**/**/*.js*\" \"./docs/**/*.md\" \"./test/**/**/*.js*\"",
32
- "develop": "concurrently \"nodemon --watch src --watch docs -e js,md,css,html,jsx ./build.js\" \"http-server ./dist --open\"",
33
- "build": "node ./build.js",
34
- "serve": "node ./build.js && http-server ./dist --open",
35
- "start": "npm run develop",
31
+ "lint": "ls-lint && eslint \"*.*js\" \"./src/**/**/*.js*\" \"./sandbox/**/**/*.js*\" \"./docs/**/*.md\" \"./test/**/**/*.js*\"",
32
+ "docs:dev": "concurrently \"nodemon --watch src --watch docs -e js,md,css,html,jsx ./build.js\" \"http-server ./dist --open\"",
33
+ "docs:build": "node ./build.js",
34
+ "docs:serve": "npm run clean && npm run docs:build && http-server ./dist --open",
35
+ "sandbox": "npm run clean && concurrently \"nodemon --experimental-loader ./test-exp-loader.js --watch src --watch sandbox -e js,md,css,html,jsx ./sandbox.js\" \"http-server ./dist --open\" \"livereload ./dist\"",
36
+ "start": "npm run docs:serve",
36
37
  "test": "mocha --exclude \"./test/cases/jsx*/**\" --exclude \"./test/cases/custom-extension/**\" \"./test/**/**/*.spec.js\"",
37
38
  "test:exp": "c8 node --experimental-loader ./test-exp-loader.js ./node_modules/mocha/bin/mocha \"./test/**/**/*.spec.js\"",
38
39
  "test:tdd": "npm run test -- --watch",
@@ -61,8 +62,8 @@
61
62
  "eslint-plugin-no-only-tests": "^2.6.0",
62
63
  "http-server": "^14.1.0",
63
64
  "jsdom": "^19.0.0",
65
+ "livereload": "^0.9.3",
64
66
  "mocha": "^9.2.2",
65
- "node-fetch": "^3.2.6",
66
67
  "nodemon": "^2.0.15",
67
68
  "prismjs": "^1.28.0",
68
69
  "rehype-autolink-headings": "^6.1.1",
package/src/dom-shim.js CHANGED
@@ -31,6 +31,18 @@ class Element extends Node {
31
31
  this.attributes = {};
32
32
  }
33
33
 
34
+ attachShadow(options) {
35
+ this.shadowRoot = new ShadowRoot(options);
36
+
37
+ return this.shadowRoot;
38
+ }
39
+
40
+ // https://github.com/mfreed7/declarative-shadow-dom/blob/master/README.md#serialization
41
+ // eslint-disable-next-line
42
+ getInnerHTML() {
43
+ return this.shadowRoot ? this.shadowRoot.innerHTML : this.innerHTML;
44
+ }
45
+
34
46
  setAttribute(name, value) {
35
47
  this.attributes[name] = value;
36
48
  }
@@ -68,19 +80,7 @@ class Document extends Node {
68
80
  // https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement
69
81
  // EventTarget <- Node <- Element <- HTMLElement
70
82
  class HTMLElement extends Element {
71
- attachShadow(options) {
72
- this.shadowRoot = new ShadowRoot(options);
73
-
74
- return this.shadowRoot;
75
- }
76
-
77
83
  connectedCallback() { }
78
-
79
- // https://github.com/mfreed7/declarative-shadow-dom/blob/master/README.md#serialization
80
- // eslint-disable-next-line
81
- getInnerHTML(options = {}) {
82
- return this.shadowRoot.innerHTML;
83
- }
84
84
  }
85
85
 
86
86
  // https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
package/src/jsx-loader.js CHANGED
@@ -38,6 +38,7 @@ export function getParser(moduleURL) {
38
38
  };
39
39
  }
40
40
 
41
+ // replace all instances of __this__ marker with relative reference to the custom element parent node
41
42
  function applyDomDepthSubstitutions(tree, currentDepth = 1, hasShadowRoot = false) {
42
43
  try {
43
44
  for (const node of tree.childNodes) {
@@ -50,9 +51,9 @@ function applyDomDepthSubstitutions(tree, currentDepth = 1, hasShadowRoot = fals
50
51
  const { value } = attrs[attr];
51
52
 
52
53
  if (value.indexOf('__this__.') >= 0) {
53
- const root = hasShadowRoot ? 'parentNode.host' : 'parentElement';
54
+ const root = hasShadowRoot ? '.getRootNode().host' : `${'.parentElement'.repeat(currentDepth)}`;
54
55
 
55
- node.attrs[attr].value = value.replace(/__this__/g, `this${'.parentElement'.repeat(currentDepth - 1)}.${root}`);
56
+ node.attrs[attr].value = value.replace(/__this__/g, `this${root}`);
56
57
  }
57
58
  }
58
59
  }
@@ -265,8 +266,26 @@ export function parseJsx(moduleURL) {
265
266
 
266
267
  applyDomDepthSubstitutions(elementTree, undefined, hasShadowRoot);
267
268
 
268
- const finalHtml = serialize(elementTree);
269
- const transformed = acorn.parse(`${elementRoot}.innerHTML = \`${finalHtml}\`;`, {
269
+ const serializedHtml = serialize(elementTree);
270
+ // we have to Shadow DOM use cases here
271
+ // 1. No shadowRoot, so we attachShadow and append the template
272
+ // 2. If there is root from the attachShadow signal, so we just need to inject innerHTML, say in an htmx
273
+ // could / should we do something else instead of .innerHTML
274
+ // https://github.com/ProjectEvergreen/wcc/issues/138
275
+ const renderHandler = hasShadowRoot
276
+ ? `
277
+ const template = document.createElement('template');
278
+ template.innerHTML = \`${serializedHtml}\`;
279
+
280
+ if(!${elementRoot}) {
281
+ this.attachShadow({ mode: 'open' });
282
+ this.shadowRoot.appendChild(template.content.cloneNode(true));
283
+ } else {
284
+ this.shadowRoot.innerHTML = template.innerHTML;
285
+ }
286
+ `
287
+ : `${elementRoot}.innerHTML = \`${serializedHtml}\`;`;
288
+ const transformed = acorn.parse(renderHandler, {
270
289
  ecmaVersion: 'latest',
271
290
  sourceType: 'module'
272
291
  });
@@ -300,15 +319,7 @@ export function parseJsx(moduleURL) {
300
319
  for (const line of tree.body) {
301
320
  // test for class MyComponent vs export default class MyComponent
302
321
  if (line.type === 'ClassDeclaration' || (line.declaration && line.declaration.type) === 'ClassDeclaration') {
303
- const children = !line.declaration
304
- ? line.body.body
305
- : line.declaration.body.body;
306
- for (const method of children) {
307
- if (method.key.name === 'constructor') {
308
- insertPoint = method.start - 1;
309
- break;
310
- }
311
- }
322
+ insertPoint = line.declaration.body.start + 1;
312
323
  }
313
324
  }
314
325