@genesislcap/ts-builder 14.424.1-alpha-cfc2887.0 → 14.425.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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react-wrapper-generator.d.ts","sourceRoot":"","sources":["../src/react-wrapper-generator.ts"],"names":[],"mappings":"AAyCA,KAAK,cAAc,GACf;IAAE,SAAS,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACjC;IAAE,SAAS,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"react-wrapper-generator.d.ts","sourceRoot":"","sources":["../src/react-wrapper-generator.ts"],"names":[],"mappings":"AAyCA,KAAK,cAAc,GACf;IAAE,SAAS,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACjC;IAAE,SAAS,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAqwBzC,wBAAsB,qBAAqB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAqChF"}
|
|
@@ -383,7 +383,7 @@ function renderImportLines(usedImports) {
|
|
|
383
383
|
.map(([spec, ids]) => `import type { ${[...ids].sort().join(', ')} } from '${spec}';`);
|
|
384
384
|
}
|
|
385
385
|
function generateReactWrapperJs(entries, format) {
|
|
386
|
-
const valid = entries.filter((e) => e.declaration.name && e.modulePath);
|
|
386
|
+
const valid = entries.filter((e) => e.declaration.name && e.declaration.tagName && e.modulePath);
|
|
387
387
|
if (!valid.length)
|
|
388
388
|
return '';
|
|
389
389
|
const esm = format === 'esm';
|
|
@@ -397,10 +397,10 @@ function generateReactWrapperJs(entries, format) {
|
|
|
397
397
|
if (!esm)
|
|
398
398
|
lines.push("'use strict';", '');
|
|
399
399
|
if (esm) {
|
|
400
|
-
lines.push("import
|
|
400
|
+
lines.push("import React from 'react';");
|
|
401
401
|
}
|
|
402
402
|
else {
|
|
403
|
-
lines.push("const
|
|
403
|
+
lines.push("const React = require('react');");
|
|
404
404
|
}
|
|
405
405
|
for (const [modulePath, pathEntries] of [...groupEntriesByPath(valid).entries()].sort()) {
|
|
406
406
|
const sorted = [...pathEntries].sort((a, b) => a.declaration.name.localeCompare(b.declaration.name));
|
|
@@ -412,22 +412,44 @@ function generateReactWrapperJs(entries, format) {
|
|
|
412
412
|
lines.push(`const { ${sorted.map((e) => `${e.declaration.name}: ${e.declaration.name}WC`).join(', ')} } = require('${jsPath}');`);
|
|
413
413
|
}
|
|
414
414
|
}
|
|
415
|
-
|
|
415
|
+
const anyHasEvents = valid.some((e) => buildWrapperEventEntries(e.declaration).length > 0);
|
|
416
|
+
if (anyHasEvents) {
|
|
417
|
+
lines.push('', 'function _mergeRefs(...refs) {', ' return (value) => {', ' for (const ref of refs) {', " if (typeof ref === 'function') ref(value);", ' else if (ref != null) ref.current = value;', ' }', ' };', '}');
|
|
418
|
+
}
|
|
419
|
+
lines.push('');
|
|
416
420
|
for (const { declaration } of valid) {
|
|
417
421
|
const name = declaration.name;
|
|
422
|
+
const tagName = declaration.tagName;
|
|
418
423
|
const events = buildWrapperEventEntries(declaration);
|
|
419
424
|
const prefix = esm ? 'export const' : 'const';
|
|
420
|
-
|
|
421
|
-
|
|
425
|
+
lines.push(`${prefix} ${name} = React.forwardRef(function ${name}(props, ref) {`);
|
|
426
|
+
if (events.length) {
|
|
427
|
+
lines.push(` const { ${events.map((e) => e.handlerName).join(', ')}, children, ...rest } = props;`);
|
|
428
|
+
lines.push(' const _innerRef = React.useRef(null);');
|
|
429
|
+
for (const { handlerName } of events) {
|
|
430
|
+
lines.push(` const _${handlerName}Ref = React.useRef(${handlerName});`);
|
|
431
|
+
lines.push(` _${handlerName}Ref.current = ${handlerName};`);
|
|
432
|
+
}
|
|
433
|
+
lines.push(' React.useLayoutEffect(() => {');
|
|
434
|
+
lines.push(' const el = _innerRef.current;');
|
|
435
|
+
lines.push(' if (!el) return;');
|
|
436
|
+
for (const { handlerName, eventName } of events) {
|
|
437
|
+
lines.push(` const _${handlerName}Fn = (e) => _${handlerName}Ref.current?.(e);`);
|
|
438
|
+
lines.push(` el.addEventListener('${eventName}', _${handlerName}Fn);`);
|
|
439
|
+
}
|
|
440
|
+
lines.push(' return () => {');
|
|
441
|
+
for (const { handlerName, eventName } of events) {
|
|
442
|
+
lines.push(` el.removeEventListener('${eventName}', _${handlerName}Fn);`);
|
|
443
|
+
}
|
|
444
|
+
lines.push(' };');
|
|
445
|
+
lines.push(' }, []);');
|
|
446
|
+
lines.push(` return React.createElement(customElements.getName(${name}WC) ?? '${tagName}', { ...rest, ref: _mergeRefs(_innerRef, ref) }, children);`);
|
|
422
447
|
}
|
|
423
448
|
else {
|
|
424
|
-
lines.push(
|
|
425
|
-
lines.push(
|
|
426
|
-
for (const { handlerName, eventName } of events)
|
|
427
|
-
lines.push(` ${handlerName}: '${eventName}',`);
|
|
428
|
-
lines.push(' },');
|
|
429
|
-
lines.push('});');
|
|
449
|
+
lines.push(' const { children, ...rest } = props;');
|
|
450
|
+
lines.push(` return React.createElement(customElements.getName(${name}WC) ?? '${tagName}', { ...rest, ref }, children);`);
|
|
430
451
|
}
|
|
452
|
+
lines.push('});');
|
|
431
453
|
lines.push('');
|
|
432
454
|
}
|
|
433
455
|
if (!esm) {
|
|
@@ -440,7 +462,7 @@ function generateReactWrapperJs(entries, format) {
|
|
|
440
462
|
}
|
|
441
463
|
function generateReactWrapperDts(entries, typeImportState) {
|
|
442
464
|
var _a;
|
|
443
|
-
const valid = entries.filter((e) => e.declaration.name && e.modulePath);
|
|
465
|
+
const valid = entries.filter((e) => e.declaration.name && e.declaration.tagName && e.modulePath);
|
|
444
466
|
if (!valid.length)
|
|
445
467
|
return '';
|
|
446
468
|
const wrapperTypeState = cloneTypeImportStateForWrapper(typeImportState);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@genesislcap/ts-builder",
|
|
3
3
|
"description": "Typescript builder",
|
|
4
|
-
"version": "14.
|
|
4
|
+
"version": "14.425.0",
|
|
5
5
|
"license": "SEE LICENSE IN license.txt",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@custom-elements-manifest/analyzer": "^0.8.2",
|
|
20
|
-
"@genesislcap/build-kit": "14.
|
|
20
|
+
"@genesislcap/build-kit": "14.425.0",
|
|
21
21
|
"consola": "^3.0.2",
|
|
22
22
|
"copyfiles": "^2.4.1",
|
|
23
23
|
"pkg-types": "^1.0.2"
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "c49bb94fbc3a74e84f18c7445398af116e1a58bb"
|
|
34
34
|
}
|
|
@@ -469,7 +469,7 @@ function renderImportLines(usedImports: Map<string, Set<string>>): string[] {
|
|
|
469
469
|
}
|
|
470
470
|
|
|
471
471
|
function generateReactWrapperJs(entries: CEMElementEntry[], format: 'esm' | 'cjs'): string {
|
|
472
|
-
const valid = entries.filter((e) => e.declaration.name && e.modulePath);
|
|
472
|
+
const valid = entries.filter((e) => e.declaration.name && e.declaration.tagName && e.modulePath);
|
|
473
473
|
if (!valid.length) return '';
|
|
474
474
|
|
|
475
475
|
const esm = format === 'esm';
|
|
@@ -484,15 +484,9 @@ function generateReactWrapperJs(entries: CEMElementEntry[], format: 'esm' | 'cjs
|
|
|
484
484
|
if (!esm) lines.push("'use strict';", '');
|
|
485
485
|
|
|
486
486
|
if (esm) {
|
|
487
|
-
lines.push(
|
|
488
|
-
"import { provideReactWrapper } from '@microsoft/fast-react-wrapper';",
|
|
489
|
-
"import React from 'react';",
|
|
490
|
-
);
|
|
487
|
+
lines.push("import React from 'react';");
|
|
491
488
|
} else {
|
|
492
|
-
lines.push(
|
|
493
|
-
"const { provideReactWrapper } = require('@microsoft/fast-react-wrapper');",
|
|
494
|
-
"const React = require('react');",
|
|
495
|
-
);
|
|
489
|
+
lines.push("const React = require('react');");
|
|
496
490
|
}
|
|
497
491
|
|
|
498
492
|
for (const [modulePath, pathEntries] of [...groupEntriesByPath(valid).entries()].sort()) {
|
|
@@ -505,21 +499,58 @@ function generateReactWrapperJs(entries: CEMElementEntry[], format: 'esm' | 'cjs
|
|
|
505
499
|
}
|
|
506
500
|
}
|
|
507
501
|
|
|
508
|
-
|
|
502
|
+
const anyHasEvents = valid.some((e) => buildWrapperEventEntries(e.declaration).length > 0);
|
|
503
|
+
if (anyHasEvents) {
|
|
504
|
+
lines.push(
|
|
505
|
+
'',
|
|
506
|
+
'function _mergeRefs(...refs) {',
|
|
507
|
+
' return (value) => {',
|
|
508
|
+
' for (const ref of refs) {',
|
|
509
|
+
" if (typeof ref === 'function') ref(value);",
|
|
510
|
+
' else if (ref != null) ref.current = value;',
|
|
511
|
+
' }',
|
|
512
|
+
' };',
|
|
513
|
+
'}',
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
lines.push('');
|
|
509
518
|
|
|
510
519
|
for (const { declaration } of valid) {
|
|
511
520
|
const name = declaration.name!;
|
|
521
|
+
const tagName = declaration.tagName!;
|
|
512
522
|
const events = buildWrapperEventEntries(declaration);
|
|
513
523
|
const prefix = esm ? 'export const' : 'const';
|
|
514
|
-
|
|
515
|
-
|
|
524
|
+
|
|
525
|
+
lines.push(`${prefix} ${name} = React.forwardRef(function ${name}(props, ref) {`);
|
|
526
|
+
|
|
527
|
+
if (events.length) {
|
|
528
|
+
lines.push(` const { ${events.map((e) => e.handlerName).join(', ')}, children, ...rest } = props;`);
|
|
529
|
+
lines.push(' const _innerRef = React.useRef(null);');
|
|
530
|
+
for (const { handlerName } of events) {
|
|
531
|
+
lines.push(` const _${handlerName}Ref = React.useRef(${handlerName});`);
|
|
532
|
+
lines.push(` _${handlerName}Ref.current = ${handlerName};`);
|
|
533
|
+
}
|
|
534
|
+
lines.push(' React.useLayoutEffect(() => {');
|
|
535
|
+
lines.push(' const el = _innerRef.current;');
|
|
536
|
+
lines.push(' if (!el) return;');
|
|
537
|
+
for (const { handlerName, eventName } of events) {
|
|
538
|
+
lines.push(` const _${handlerName}Fn = (e) => _${handlerName}Ref.current?.(e);`);
|
|
539
|
+
lines.push(` el.addEventListener('${eventName}', _${handlerName}Fn);`);
|
|
540
|
+
}
|
|
541
|
+
lines.push(' return () => {');
|
|
542
|
+
for (const { handlerName, eventName } of events) {
|
|
543
|
+
lines.push(` el.removeEventListener('${eventName}', _${handlerName}Fn);`);
|
|
544
|
+
}
|
|
545
|
+
lines.push(' };');
|
|
546
|
+
lines.push(' }, []);');
|
|
547
|
+
lines.push(` return React.createElement(customElements.getName(${name}WC) ?? '${tagName}', { ...rest, ref: _mergeRefs(_innerRef, ref) }, children);`);
|
|
516
548
|
} else {
|
|
517
|
-
lines.push(
|
|
518
|
-
lines.push(
|
|
519
|
-
for (const { handlerName, eventName } of events) lines.push(` ${handlerName}: '${eventName}',`);
|
|
520
|
-
lines.push(' },');
|
|
521
|
-
lines.push('});');
|
|
549
|
+
lines.push(' const { children, ...rest } = props;');
|
|
550
|
+
lines.push(` return React.createElement(customElements.getName(${name}WC) ?? '${tagName}', { ...rest, ref }, children);`);
|
|
522
551
|
}
|
|
552
|
+
|
|
553
|
+
lines.push('});');
|
|
523
554
|
lines.push('');
|
|
524
555
|
}
|
|
525
556
|
|
|
@@ -533,7 +564,7 @@ function generateReactWrapperJs(entries: CEMElementEntry[], format: 'esm' | 'cjs
|
|
|
533
564
|
}
|
|
534
565
|
|
|
535
566
|
function generateReactWrapperDts(entries: CEMElementEntry[], typeImportState: TypeImportState): string {
|
|
536
|
-
const valid = entries.filter((e) => e.declaration.name && e.modulePath);
|
|
567
|
+
const valid = entries.filter((e) => e.declaration.name && e.declaration.tagName && e.modulePath);
|
|
537
568
|
if (!valid.length) return '';
|
|
538
569
|
|
|
539
570
|
const wrapperTypeState = cloneTypeImportStateForWrapper(typeImportState);
|