wc-compiler 0.18.1 → 0.19.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/package.json +4 -4
- package/src/jsx-loader.js +122 -33
package/package.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wc-compiler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"description": "Experimental native Web Components compiler.",
|
|
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
|
-
"
|
|
15
|
-
"
|
|
14
|
+
"types": "./src/index.d.ts",
|
|
15
|
+
"import": "./src/wcc.js"
|
|
16
16
|
},
|
|
17
17
|
"./register": "./src/register.js",
|
|
18
18
|
"./src/jsx-loader.js": "./src/jsx-loader.js",
|
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
|
-
|
|
128
|
-
|
|
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) =>
|
|
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
|
|
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
|
|
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 [${[...
|
|
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
|
-
${
|
|
451
|
+
${trackingAttrs
|
|
389
452
|
.map((attr) => {
|
|
390
453
|
return `
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
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
|
-
|
|
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
|
|