wc-compiler 0.18.1 → 0.20.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/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2022 Project Evergreen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- <img src="https://merry-caramel-524e61.netlify.app/assets/wcc-logo.png" width="30%"/>
1
+ <img src="https://wcc.dev/assets/wcc-logo.svg" width="30%"/>
2
2
 
3
3
  # Web Components Compiler (WCC)
4
4
 
@@ -8,7 +8,7 @@
8
8
  [![NodeJS compatibility](https://img.shields.io/node/v/wc-compiler.svg)](https://nodejs.org/en/about/previous-releases")
9
9
  [![Discord Chat](https://img.shields.io/badge/chat-discord-blue?style=flat&logo=discord)](https://www.greenwoodjs.dev/discord/)
10
10
 
11
- > _Experimental Web Components compiler. It's Web Components all the way down!_ 🐢
11
+ > _Native Web Components compiler. It's Web Components all the way down!_ 🐢
12
12
 
13
13
  ## How It Works
14
14
 
@@ -81,7 +81,7 @@ $ npm install wc-compiler --save-dev
81
81
 
82
82
  ## Documentation
83
83
 
84
- See our [website](https://merry-caramel-524e61.netlify.app/) for API docs and examples.
84
+ See our [website](https://www.wcc.dev) for API docs and examples.
85
85
 
86
86
  ## Motivation
87
87
 
package/package.json CHANGED
@@ -1,21 +1,22 @@
1
1
  {
2
2
  "name": "wc-compiler",
3
- "version": "0.18.1",
4
- "description": "Experimental native Web Components compiler.",
3
+ "version": "0.20.0",
4
+ "description": "WCC supports server-rendering of standard Web Components, generating HTML directly from your custom element definitions.",
5
5
  "repository": {
6
6
  "type": "git",
7
- "url": "https://github.com/ProjectEvergreen/wcc.git"
7
+ "url": "git+https://github.com/ProjectEvergreen/wcc.git"
8
8
  },
9
9
  "type": "module",
10
10
  "main": "src/wcc.js",
11
11
  "types": "./src/index.d.ts",
12
12
  "exports": {
13
13
  ".": {
14
- "import": "./src/wcc.js",
15
- "types": "./src/index.d.ts"
14
+ "types": "./src/index.d.ts",
15
+ "import": "./src/wcc.js"
16
16
  },
17
17
  "./register": "./src/register.js",
18
- "./src/jsx-loader.js": "./src/jsx-loader.js",
18
+ "./dom-shim": "./src/dom-shim.js",
19
+ "./jsx-loader": "./src/jsx-loader.js",
19
20
  "./jsx-runtime": "./src/jsx-runtime.ts"
20
21
  },
21
22
  "author": "Owen Buckley <owen@thegreenhouse.io>",
@@ -36,22 +37,22 @@
36
37
  "access": "public"
37
38
  },
38
39
  "scripts": {
39
- "clean": "rimraf ./dist",
40
- "lint": "npm run lint:js && npm run lint:ls",
40
+ "dev": "greenwood develop",
41
+ "build": "NODE_OPTIONS='--import @greenwood/cli/register' greenwood build",
42
+ "serve": "greenwood serve",
43
+ "lint": "npm run lint:js && npm run lint:ls && npm run lint:css",
41
44
  "lint:js": "eslint",
42
45
  "lint:ls": "ls-lint",
43
- "lint:types": "tsc",
46
+ "lint:css": "stylelint \"./docs/**/*.css\"",
47
+ "check:types": "tsc",
44
48
  "format": "prettier . --write",
45
49
  "format:check": "prettier . --check",
46
- "docs:dev": "concurrently \"nodemon --watch src --watch docs -e js,md,css,html,jsx,ts,tsx ./build.js\" \"http-server ./dist --open\"",
47
- "docs:build": "node ./build.js",
48
- "docs:serve": "npm run clean && npm run docs:build && http-server ./dist --open",
49
- "sandbox": "npm run clean && concurrently \"nodemon --experimental-strip-types --loader ./test-loader.js --watch src --watch sandbox -e js,md,css,html,jsx,ts ./sandbox.js\" \"http-server ./dist --open\" \"livereload ./dist\"",
50
- "start": "npm run docs:serve",
51
50
  "test": "mocha --exclude \"./test/cases/jsx*/**\" --exclude \"./test/cases/ts*/**\" --exclude \"./test/cases/custom-extension/**\" \"./test/**/**/*.spec.js\"",
52
51
  "test:jsx": "c8 node --import ./test-register.js --experimental-strip-types ./node_modules/mocha/bin/mocha \"./test/**/**/*.spec.js\"",
53
52
  "test:tdd": "npm run test -- --watch",
54
53
  "test:tdd:jsx": "npm run test:jsx -- --watch",
54
+ "test:docs": "vitest run --coverage",
55
+ "test:docs:tdd": "vitest",
55
56
  "prepare": "husky"
56
57
  },
57
58
  "dependencies": {
@@ -63,38 +64,45 @@
63
64
  "sucrase": "^3.35.0"
64
65
  },
65
66
  "devDependencies": {
67
+ "@double-great/stylelint-a11y": "^3.0.2",
66
68
  "@eslint/js": "^9.39.1",
69
+ "@greenwood/cli": "^0.34.0-alpha.4",
70
+ "@greenwood/plugin-css-modules": "^0.34.0-alpha.4",
71
+ "@greenwood/plugin-import-jsx": "^0.34.0-alpha.4",
72
+ "@greenwood/plugin-import-raw": "^0.34.0-alpha.4",
73
+ "@greenwood/plugin-markdown": "^0.34.0-alpha.4",
67
74
  "@ls-lint/ls-lint": "^2.3.1",
68
75
  "@mapbox/rehype-prism": "^0.8.0",
69
76
  "@types/mocha": "^10.0.10",
70
77
  "@types/node": "^22.13.4",
78
+ "@vitest/browser-playwright": "^4.0.18",
79
+ "@vitest/coverage-v8": "^4.0.18",
71
80
  "c8": "^7.11.2",
72
81
  "chai": "^4.3.6",
73
- "concurrently": "^7.1.0",
74
82
  "eslint": "^9.39.1",
75
83
  "eslint-config-prettier": "^10.1.8",
76
84
  "eslint-plugin-no-only-tests": "^3.3.0",
85
+ "geist": "^1.2.0",
77
86
  "globals": "^15.10.0",
78
- "http-server": "^14.1.0",
79
87
  "husky": "^9.1.7",
80
88
  "jsdom": "^19.0.0",
81
89
  "lint-staged": "^16.2.6",
82
- "livereload": "^0.9.3",
83
90
  "mocha": "^9.2.2",
84
- "nodemon": "^2.0.15",
91
+ "open-props": "^1.7.4",
92
+ "playwright": "^1.58.2",
85
93
  "prettier": "^3.6.2",
94
+ "prism-themes": "^1.9.0",
86
95
  "prismjs": "^1.28.0",
87
- "rehype-autolink-headings": "^6.1.1",
88
- "rehype-raw": "^6.1.1",
89
- "rehype-slug": "^5.0.1",
90
- "rehype-stringify": "^9.0.3",
91
- "remark-parse": "^10.0.1",
92
- "remark-rehype": "^10.1.0",
93
- "remark-toc": "^8.0.1",
94
- "rimraf": "^3.0.2",
95
- "simple.css": "^0.1.3",
96
- "typescript": "^5.8.2",
96
+ "rehype-autolink-headings": "^4.0.0",
97
+ "rehype-external-links": "^3.0.0",
98
+ "rehype-slug": "^3.0.0",
99
+ "remark-gfm": "^4.0.0",
100
+ "remark-github": "^10.0.1",
101
+ "stylelint": "^16.10.0",
102
+ "stylelint-config-recommended": "^14.0.1",
103
+ "typescript": "^5.9.3",
97
104
  "typescript-eslint": "^8.46.2",
98
- "unified": "^10.1.2"
105
+ "vite": "^7.3.1",
106
+ "vitest": "^4.0.18"
99
107
  }
100
108
  }
package/src/jsx-loader.js CHANGED
@@ -77,7 +77,7 @@ function applyDomDepthSubstitutions(tree, currentDepth = 1, hasShadowRoot = fals
77
77
  return tree;
78
78
  }
79
79
 
80
- function parseJsxElement(element, moduleContents = '') {
80
+ function parseJsxElement(element, moduleContents = '', inferredObservability = false) {
81
81
  try {
82
82
  const { type } = element;
83
83
 
@@ -124,8 +124,14 @@ function parseJsxElement(element, moduleContents = '') {
124
124
 
125
125
  if (left.object.type === 'ThisExpression') {
126
126
  if (left.property.type === 'Identifier') {
127
- // very naive (fine grained?) reactivity
128
- string += ` ${name}="__this__.${left.property.name}${expression.operator}${right.raw}; __this__.render();"`;
127
+ if (inferredObservability) {
128
+ // very naive (fine grained?) reactivity
129
+ // string += ` ${name}="__this__.${left.property.name}${expression.operator}${right.raw}; __this__.update(\\'${left.property.name}\\', null, __this__.${left.property.name});"`;
130
+ string += ` ${name}="__this__.${left.property.name}${expression.operator}${right.raw}; __this__.setAttribute(\\'${left.property.name}\\', __this__.${left.property.name});"`;
131
+ } else {
132
+ // implicit reactivity using this.render
133
+ string += ` ${name}="__this__.${left.property.name}${expression.operator}${right.raw}; __this__.render();"`;
134
+ }
129
135
  }
130
136
  }
131
137
  }
@@ -160,6 +166,11 @@ function parseJsxElement(element, moduleContents = '') {
160
166
  default:
161
167
  break;
162
168
  }
169
+
170
+ // only apply this when dealing with `this` references
171
+ if (inferredObservability) {
172
+ string += ` data-wcc-${expression.name}="${name}" data-wcc-ins="attr"`;
173
+ }
163
174
  }
164
175
  } else {
165
176
  // xxx >
@@ -171,7 +182,9 @@ function parseJsxElement(element, moduleContents = '') {
171
182
  string += openingElement.selfClosing ? ' />' : '>';
172
183
 
173
184
  if (element.children.length > 0) {
174
- element.children.forEach((child) => parseJsxElement(child, moduleContents));
185
+ element.children.forEach((child) =>
186
+ parseJsxElement(child, moduleContents, inferredObservability),
187
+ );
175
188
  }
176
189
 
177
190
  string += `</${tagName}>`;
@@ -186,6 +199,13 @@ function parseJsxElement(element, moduleContents = '') {
186
199
 
187
200
  if (type === 'Identifier') {
188
201
  // You have {count} TODOs left to complete
202
+ if (inferredObservability) {
203
+ const { name } = element.expression;
204
+
205
+ string = `${string.slice(0, string.lastIndexOf('>'))} data-wcc-${name}="\${this.${name}}" data-wcc-ins="text">`;
206
+ }
207
+ // TODO be able to remove this extra data attribute
208
+ // string = `${string.slice(0, string.lastIndexOf('>'))} data-wcc-${name} data-wcc-ins="text">`;
189
209
  string += `$\{${element.expression.name}}`;
190
210
  } else if (type === 'MemberExpression') {
191
211
  const { object } = element.expression.object;
@@ -233,10 +253,16 @@ function findThisReferences(context, statement) {
233
253
  // const { description } = this.todo;
234
254
  references.push(init.property.name);
235
255
  } else if (init.type === 'ThisExpression' && id && id.properties) {
236
- // const { description } = this.todo;
256
+ // const { id, description } = this;
237
257
  id.properties.forEach((property) => {
238
258
  references.push(property.key.name);
239
259
  });
260
+ } else {
261
+ // TODO we are just blindly tracking anything here.
262
+ // everything should ideally be mapped to actual this references, to create a strong chain of direct reactivity
263
+ // instead of tracking any declaration as a derived tracking attr
264
+ // for convenience here, we push the entire declaration here, instead of the name like for direct this references (see above)
265
+ references.push(declaration);
240
266
  }
241
267
  });
242
268
  }
@@ -262,6 +288,35 @@ export function parseJsx(moduleURL) {
262
288
  });
263
289
  string = '';
264
290
 
291
+ // TODO: would be nice to do this one pass, but first we need to know if `inferredObservability` is set first
292
+ walk.simple(
293
+ tree,
294
+ {
295
+ ExportNamedDeclaration(node) {
296
+ const { declaration } = node;
297
+
298
+ if (
299
+ declaration &&
300
+ declaration.type === 'VariableDeclaration' &&
301
+ declaration.kind === 'const' &&
302
+ declaration.declarations.length === 1
303
+ ) {
304
+ // @ts-ignore
305
+ if (declaration.declarations[0].id.name === 'inferredObservability') {
306
+ // @ts-ignore
307
+ inferredObservability = Boolean(node.declaration.declarations[0].init.raw);
308
+ }
309
+ }
310
+ },
311
+ },
312
+ {
313
+ // https://github.com/acornjs/acorn/issues/829#issuecomment-1172586171
314
+ ...walk.base,
315
+ // @ts-ignore
316
+ JSXElement: () => {},
317
+ },
318
+ );
319
+
265
320
  walk.simple(
266
321
  tree,
267
322
  {
@@ -286,7 +341,7 @@ export function parseJsx(moduleURL) {
286
341
  ];
287
342
  // @ts-ignore
288
343
  } else if (n.type === 'ReturnStatement' && n.argument.type === 'JSXElement') {
289
- const html = parseJsxElement(n.argument, moduleContents);
344
+ const html = parseJsxElement(n.argument, moduleContents, inferredObservability);
290
345
  const elementTree = getParse(html)(html);
291
346
  const elementRoot = hasShadowRoot ? 'this.shadowRoot' : 'this';
292
347
 
@@ -325,22 +380,6 @@ export function parseJsx(moduleURL) {
325
380
  }
326
381
  }
327
382
  },
328
- ExportNamedDeclaration(node) {
329
- const { declaration } = node;
330
-
331
- if (
332
- declaration &&
333
- declaration.type === 'VariableDeclaration' &&
334
- declaration.kind === 'const' &&
335
- declaration.declarations.length === 1
336
- ) {
337
- // @ts-ignore
338
- if (declaration.declarations[0].id.name === 'inferredObservability') {
339
- // @ts-ignore
340
- inferredObservability = Boolean(node.declaration.declarations[0].init.raw);
341
- }
342
- }
343
- },
344
383
  },
345
384
  {
346
385
  // https://github.com/acornjs/acorn/issues/829#issuecomment-1172586171
@@ -350,11 +389,10 @@ export function parseJsx(moduleURL) {
350
389
  },
351
390
  );
352
391
 
353
- // TODO - signals: use constructor, render, HTML attributes? some, none, or all?
354
392
  if (inferredObservability && observedAttributes.length > 0 && !hasOwnObservedAttributes) {
355
393
  let insertPoint;
356
394
  for (const line of tree.body) {
357
- // test for class MyComponent vs export default class MyComponent
395
+ // TODO: test for class MyComponent vs export default class MyComponent
358
396
  // @ts-ignore
359
397
  if (
360
398
  line.type === 'ClassDeclaration' ||
@@ -366,11 +404,36 @@ export function parseJsx(moduleURL) {
366
404
  }
367
405
 
368
406
  let newModuleContents = generate(tree);
369
-
370
- // TODO better way to determine value type?
407
+ const trackingAttrs = observedAttributes.filter((attr) => typeof attr === 'string');
408
+ // TODO ideally derivedAttrs would explicitly reference trackingAttrs
409
+ // and if there are no derivedAttrs, do not include the derivedGetters / derivedSetters code in the compiled output
410
+ const derivedAttrs = observedAttributes.filter((attr) => typeof attr !== 'string');
411
+ const derivedGetters = derivedAttrs
412
+ .map((attr) => {
413
+ return `
414
+ get_${attr.id.name}(${trackingAttrs.join(',')}) {
415
+ return ${moduleContents.slice(attr.init.start, attr.init.end)}
416
+ }
417
+ `;
418
+ })
419
+ .join('\n');
420
+ const derivedSetters = derivedAttrs
421
+ .map((attr) => {
422
+ const name = attr.id.name;
423
+
424
+ return `
425
+ const old_${name} = this.get_${name}(oldValue);
426
+ const new_${name} = this.get_${name}(newValue);
427
+ this.update('${name}', old_${name}, new_${name});
428
+ `;
429
+ })
430
+ .join('\n');
431
+
432
+ // TODO: better way to determine value type, e,g. array, int, object, etc?
433
+ // TODO: better way to test for shadowRoot presence when running querySelectorAll
371
434
  newModuleContents = `${newModuleContents.slice(0, insertPoint)}
372
435
  static get observedAttributes() {
373
- return [${[...observedAttributes].map((attr) => `'${attr}'`).join(',')}]
436
+ return [${[...trackingAttrs].map((attr) => `'${attr}'`).join()}]
374
437
  }
375
438
 
376
439
  attributeChangedCallback(name, oldValue, newValue) {
@@ -385,21 +448,47 @@ export function parseJsx(moduleURL) {
385
448
  }
386
449
  if (newValue !== oldValue) {
387
450
  switch(name) {
388
- ${observedAttributes
451
+ ${trackingAttrs
389
452
  .map((attr) => {
390
453
  return `
391
- case '${attr}':
392
- this.${attr} = getValue(newValue);
393
- break;
394
- `;
454
+ case '${attr}':
455
+ this.${attr} = getValue(newValue);
456
+ break;
457
+ `;
395
458
  })
396
459
  .join('\n')}
397
460
  }
461
+ this.update(name, oldValue, newValue);
462
+ }
463
+ }
398
464
 
399
- this.render();
465
+ update(name, oldValue, newValue) {
466
+ const attr = \`data-wcc-\${name}\`;
467
+ const selector = \`[\${attr}]\`;
468
+
469
+ (this?.shadowRoot || this).querySelectorAll(selector).forEach((el) => {
470
+ // handle empty strings as a value for the purposes of attribute change detection
471
+ const needle = oldValue === '' ? '' : oldValue ?? el.getAttribute(attr);
472
+
473
+ switch(el.getAttribute('data-wcc-ins')) {
474
+ case 'text':
475
+ el.textContent = el.textContent.replace(needle, newValue);
476
+ break;
477
+ case 'attr':
478
+ if (el.hasAttribute(el.getAttribute(attr))) {
479
+ el.setAttribute(el.getAttribute(attr), newValue);
480
+ }
481
+ break;
482
+ }
483
+ })
484
+
485
+ if ([${[...trackingAttrs].map((attr) => `'${attr}'`).join()}].includes(name)) {
486
+ ${derivedSetters}
400
487
  }
401
488
  }
402
489
 
490
+ ${derivedGetters}
491
+
403
492
  ${newModuleContents.slice(insertPoint)}
404
493
  `;
405
494
 
package/src/jsx.d.ts CHANGED
@@ -1,20 +1,31 @@
1
1
  // to support `style` attributes, we override `CSSStyleDeclaration` with `string`
2
2
  type IsCSSStyleDeclaration<T> = T extends CSSStyleDeclaration ? string : T;
3
3
 
4
- // create a utility type to extract the attributes from any given element's DOM interface.
4
+ // creates a utility type to extract the attributes from any given element's DOM interface from `HTMLElementTagNameMap`
5
5
  type ElementAttributes<E extends HTMLElement> = {
6
- // Extract all properties from the element, including inherited ones.
6
+ // Extract all properties from the element, including inherited ones
7
7
  [A in keyof E]?: E[A] extends (...args: any) => any ? any : IsCSSStyleDeclaration<E[A]>;
8
8
  } & {
9
9
  class?: string;
10
10
  };
11
11
 
12
- // map each HTML tag to a union of its attributes and the global attributes.
12
+ type PopoverTargetAction = 'show' | 'hide' | 'toggle';
13
+ type PopoverTargetAttributes = {
14
+ // have to manage this manually, can't seem to get this from TypeScript itself (not sure if just skill issue? :D)
15
+ // https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1790
16
+ // it should be there per https://github.com/mdn/browser-compat-data/pull/21875
17
+ // https://github.com/ProjectEvergreen/wcc/issues/236
18
+ // per the spec, this should only apply to <button> and <input> elements.
19
+ popovertarget?: string;
20
+ popovertargetaction?: PopoverTargetAction;
21
+ };
22
+
23
+ // map each HTML tag to its attributes
13
24
  type IntrinsicElementsFromDom = {
14
- [E in keyof HTMLElementTagNameMap]: ElementAttributes<HTMLElementTagNameMap[E]>;
25
+ [E in keyof HTMLElementTagNameMap]: ElementAttributes<HTMLElementTagNameMap[E]> &
26
+ (E extends 'button' | 'input' ? PopoverTargetAttributes : {});
15
27
  };
16
28
 
17
- // declare the global JSX namespace with your generated intrinsic elements.
18
29
  declare namespace JSX {
19
30
  interface IntrinsicElements extends IntrinsicElementsFromDom {}
20
31
  }