@stencil/angular-output-target 0.6.1-dev.11657573317.16e0205c → 0.6.1-dev.11662151255.17ea4066
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/README.md +2 -2
- package/angular-component-lib/utils.ts +9 -15
- package/dist/generate-angular-component.d.ts +22 -2
- package/dist/generate-angular-component.js +117 -94
- package/dist/generate-angular-directives-file.js +1 -1
- package/dist/generate-angular-modules.d.ts +6 -0
- package/dist/generate-angular-modules.js +17 -0
- package/dist/generate-value-accessors.js +3 -6
- package/dist/index.cjs.js +263 -122
- package/dist/index.js +263 -122
- package/dist/output-angular.js +77 -20
- package/dist/plugin.d.ts +1 -1
- package/dist/plugin.js +8 -5
- package/dist/types.d.ts +18 -2
- package/dist/utils.d.ts +27 -1
- package/dist/utils.js +45 -0
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ export const config: Config = {
|
|
|
27
27
|
outputTargets: [
|
|
28
28
|
angularOutputTarget({
|
|
29
29
|
componentCorePackage: 'component-library',
|
|
30
|
-
|
|
30
|
+
proxyDeclarationFile: '../component-library-angular/src/directives/proxies.ts',
|
|
31
31
|
directivesArrayFile: '../component-library-angular/src/directives/index.ts',
|
|
32
32
|
}),
|
|
33
33
|
{
|
|
@@ -43,7 +43,7 @@ export const config: Config = {
|
|
|
43
43
|
| Property | Description |
|
|
44
44
|
| ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
45
45
|
| `componentCorePackage` | The NPM package name of your Stencil component library. This package is used as a dependency for your Angular wrappers. |
|
|
46
|
-
| `
|
|
46
|
+
| `proxyDeclarationFile` | The output file of all the component wrappers generated by the output target. This file path should point to a location within your Angular library/project. |
|
|
47
47
|
| `directivesArrayFile` | The output file of a constant of all the generated component wrapper classes. Used for easily declaring and exporting the generated components from an `NgModule`. This file path should point to a location within your Angular library/project. |
|
|
48
48
|
| `valueAccessorConfigs` | The configuration object for how individual web components behave with Angular control value accessors. |
|
|
49
49
|
| `excludeComponents` | An array of tag names to exclude from generating component wrappers for. This is helpful when have a custom framework implementation of a specific component or need to extend the base component wrapper behavior. |
|
|
@@ -4,46 +4,40 @@ import { fromEvent } from 'rxjs';
|
|
|
4
4
|
|
|
5
5
|
export const proxyInputs = (Cmp: any, inputs: string[]) => {
|
|
6
6
|
const Prototype = Cmp.prototype;
|
|
7
|
-
inputs.forEach(item => {
|
|
7
|
+
inputs.forEach((item) => {
|
|
8
8
|
Object.defineProperty(Prototype, item, {
|
|
9
9
|
get() {
|
|
10
10
|
return this.el[item];
|
|
11
11
|
},
|
|
12
12
|
set(val: any) {
|
|
13
13
|
this.z.runOutsideAngular(() => (this.el[item] = val));
|
|
14
|
-
}
|
|
14
|
+
},
|
|
15
15
|
});
|
|
16
16
|
});
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
export const proxyMethods = (Cmp: any, methods: string[]) => {
|
|
20
20
|
const Prototype = Cmp.prototype;
|
|
21
|
-
methods.forEach(methodName => {
|
|
21
|
+
methods.forEach((methodName) => {
|
|
22
22
|
Prototype[methodName] = function () {
|
|
23
23
|
const args = arguments;
|
|
24
|
-
return this.z.runOutsideAngular(() =>
|
|
25
|
-
this.el[methodName].apply(this.el, args)
|
|
26
|
-
);
|
|
24
|
+
return this.z.runOutsideAngular(() => this.el[methodName].apply(this.el, args));
|
|
27
25
|
};
|
|
28
26
|
});
|
|
29
27
|
};
|
|
30
28
|
|
|
31
29
|
export const proxyOutputs = (instance: any, el: any, events: string[]) => {
|
|
32
|
-
events.forEach(eventName => instance[eventName] = fromEvent(el, eventName));
|
|
33
|
-
}
|
|
30
|
+
events.forEach((eventName) => (instance[eventName] = fromEvent(el, eventName)));
|
|
31
|
+
};
|
|
34
32
|
|
|
35
33
|
export const defineCustomElement = (tagName: string, customElement: any) => {
|
|
36
|
-
if (
|
|
37
|
-
customElement !== undefined &&
|
|
38
|
-
typeof customElements !== 'undefined' &&
|
|
39
|
-
!customElements.get(tagName)
|
|
40
|
-
) {
|
|
34
|
+
if (customElement !== undefined && typeof customElements !== 'undefined' && !customElements.get(tagName)) {
|
|
41
35
|
customElements.define(tagName, customElement);
|
|
42
36
|
}
|
|
43
|
-
}
|
|
37
|
+
};
|
|
44
38
|
|
|
45
39
|
// tslint:disable-next-line: only-arrow-functions
|
|
46
|
-
export function ProxyCmp(opts: { defineCustomElementFn?: () => void
|
|
40
|
+
export function ProxyCmp(opts: { defineCustomElementFn?: () => void; inputs?: any; methods?: any }) {
|
|
47
41
|
const decorator = function (cls: any) {
|
|
48
42
|
const { defineCustomElementFn, inputs, methods } = opts;
|
|
49
43
|
|
|
@@ -1,2 +1,22 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { ComponentCompilerEvent } from '@stencil/core/internal';
|
|
2
|
+
/**
|
|
3
|
+
* Creates an Angular component declaration from formatted Stencil compiler metadata.
|
|
4
|
+
*
|
|
5
|
+
* @param tagName The tag name of the component.
|
|
6
|
+
* @param inputs The inputs of the Stencil component (e.g. ['myInput']).
|
|
7
|
+
* @param outputs The outputs/events of the Stencil component. (e.g. ['myOutput']).
|
|
8
|
+
* @param methods The methods of the Stencil component. (e.g. ['myMethod']).
|
|
9
|
+
* @param includeImportCustomElements Whether to define the component as a custom element.
|
|
10
|
+
* @returns The component declaration as a string.
|
|
11
|
+
*/
|
|
12
|
+
export declare const createAngularComponentDefinition: (tagName: string, inputs: readonly string[], outputs: readonly string[], methods: readonly string[], includeImportCustomElements?: boolean) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Creates the component interface type definition.
|
|
15
|
+
* @param tagNameAsPascal The tag name as PascalCase.
|
|
16
|
+
* @param events The events to generate the interface properties for.
|
|
17
|
+
* @param componentCorePackage The component core package.
|
|
18
|
+
* @param includeImportCustomElements Whether to include the import for the custom element definition.
|
|
19
|
+
* @param customElementsDir The custom elements directory.
|
|
20
|
+
* @returns The component interface type definition as a string.
|
|
21
|
+
*/
|
|
22
|
+
export declare const createComponentTypeDefinition: (tagNameAsPascal: string, events: readonly ComponentCompilerEvent[], componentCorePackage: string, includeImportCustomElements?: boolean, customElementsDir?: string | undefined) => string;
|
|
@@ -1,101 +1,124 @@
|
|
|
1
|
-
import { dashToPascalCase,
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { createComponentEventTypeImports, dashToPascalCase, formatToQuotedList } from './utils';
|
|
2
|
+
/**
|
|
3
|
+
* Creates an Angular component declaration from formatted Stencil compiler metadata.
|
|
4
|
+
*
|
|
5
|
+
* @param tagName The tag name of the component.
|
|
6
|
+
* @param inputs The inputs of the Stencil component (e.g. ['myInput']).
|
|
7
|
+
* @param outputs The outputs/events of the Stencil component. (e.g. ['myOutput']).
|
|
8
|
+
* @param methods The methods of the Stencil component. (e.g. ['myMethod']).
|
|
9
|
+
* @param includeImportCustomElements Whether to define the component as a custom element.
|
|
10
|
+
* @returns The component declaration as a string.
|
|
11
|
+
*/
|
|
12
|
+
export const createAngularComponentDefinition = (tagName, inputs, outputs, methods, includeImportCustomElements = false) => {
|
|
13
|
+
const tagNameAsPascal = dashToPascalCase(tagName);
|
|
14
|
+
const hasInputs = inputs.length > 0;
|
|
11
15
|
const hasOutputs = outputs.length > 0;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
const hasMethods = methods.length > 0;
|
|
17
|
+
// Formats the input strings into comma separated, single quoted values.
|
|
18
|
+
const formattedInputs = formatToQuotedList(inputs);
|
|
19
|
+
// Formats the output strings into comma separated, single quoted values.
|
|
20
|
+
const formattedOutputs = formatToQuotedList(outputs);
|
|
21
|
+
// Formats the method strings into comma separated, single quoted values.
|
|
22
|
+
const formattedMethods = formatToQuotedList(methods);
|
|
23
|
+
const proxyCmpOptions = [];
|
|
24
|
+
if (includeImportCustomElements) {
|
|
25
|
+
const defineCustomElementFn = `define${tagNameAsPascal}`;
|
|
26
|
+
proxyCmpOptions.push(`\n defineCustomElementFn: ${defineCustomElementFn}`);
|
|
20
27
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
importLocation = `${normalizePath(componentCorePackage)}${dirPath}`;
|
|
36
|
-
}
|
|
37
|
-
outputsInterface.add(`import type { ${reference} as ${remappedReference} } from '${importLocation}';`);
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
const componentEvents = [
|
|
42
|
-
'' // Empty first line
|
|
43
|
-
];
|
|
44
|
-
// Generate outputs
|
|
45
|
-
outputs.forEach((output, index) => {
|
|
46
|
-
componentEvents.push(` /**
|
|
47
|
-
* ${output.docs.text} ${output.docs.tags.map((tag) => `@${tag.name} ${tag.text}`)}
|
|
48
|
-
*/`);
|
|
49
|
-
/**
|
|
50
|
-
* The original attribute contains the original type defined by the devs.
|
|
51
|
-
* This regexp normalizes the reference, by removing linebreaks,
|
|
52
|
-
* replacing consecutive spaces with a single space, and adding a single space after commas.
|
|
53
|
-
**/
|
|
54
|
-
const outputTypeRemapped = Object.entries(outputReferenceRemap).reduce((type, [src, dst]) => {
|
|
55
|
-
return type
|
|
56
|
-
.replace(new RegExp(`^${src}$`, 'g'), `${dst}`)
|
|
57
|
-
.replace(new RegExp(`([^\\w])${src}([^\\w])`, 'g'), (v, p1, p2) => [p1, dst, p2].join(''));
|
|
58
|
-
}, output.complexType.original
|
|
59
|
-
.replace(/\n/g, ' ')
|
|
60
|
-
.replace(/\s{2,}/g, ' ')
|
|
61
|
-
.replace(/,\s*/g, ', '));
|
|
62
|
-
componentEvents.push(` ${output.name}: EventEmitter<CustomEvent<${outputTypeRemapped.trim()}>>;`);
|
|
63
|
-
if (index === outputs.length - 1) {
|
|
64
|
-
// Empty line to push end `}` to new line
|
|
65
|
-
componentEvents.push('\n');
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
const lines = [
|
|
69
|
-
'',
|
|
70
|
-
`${[...outputsInterface].join('\n')}
|
|
71
|
-
export declare interface ${tagNameAsPascal} extends Components.${tagNameAsPascal} {${componentEvents.length > 1 ? componentEvents.join('\n') : ''}}
|
|
72
|
-
|
|
73
|
-
${getProxyCmp(cmpMeta.tagName, includeImportCustomElements, inputs, methods)}
|
|
28
|
+
if (hasInputs) {
|
|
29
|
+
proxyCmpOptions.push(`\n inputs: [${formattedInputs}]`);
|
|
30
|
+
}
|
|
31
|
+
if (hasMethods) {
|
|
32
|
+
proxyCmpOptions.push(`\n methods: [${formattedMethods}]`);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Notes on the generated output:
|
|
36
|
+
* - We disable @angular-eslint/no-inputs-metadata-property, so that
|
|
37
|
+
* Angular does not complain about the inputs property. The output target
|
|
38
|
+
* uses the inputs property to define the inputs of the component instead of
|
|
39
|
+
* having to use the @Input decorator (and manually define the type and default value).
|
|
40
|
+
*/
|
|
41
|
+
const output = `@ProxyCmp({${proxyCmpOptions.join(',')}\n})
|
|
74
42
|
@Component({
|
|
75
|
-
${
|
|
43
|
+
selector: '${tagName}',
|
|
44
|
+
template: '<ng-content></ng-content>',
|
|
45
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
46
|
+
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
|
|
47
|
+
inputs: [${formattedInputs}],
|
|
76
48
|
})
|
|
77
|
-
export class ${tagNameAsPascal} {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
lines.push(` constructor(c: ChangeDetectorRef, r: ElementRef, protected z: NgZone) {
|
|
49
|
+
export class ${tagNameAsPascal} {
|
|
50
|
+
protected el: HTMLElement;
|
|
51
|
+
constructor(c: ChangeDetectorRef, r: ElementRef, protected z: NgZone) {
|
|
81
52
|
c.detach();
|
|
82
|
-
this.el = r.nativeElement
|
|
83
|
-
|
|
84
|
-
|
|
53
|
+
this.el = r.nativeElement;${hasOutputs
|
|
54
|
+
? `
|
|
55
|
+
proxyOutputs(this, this.el, [${formattedOutputs}]);`
|
|
56
|
+
: ''}
|
|
57
|
+
}
|
|
58
|
+
}`;
|
|
59
|
+
return output;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Sanitizes and formats the component event type.
|
|
63
|
+
* @param componentClassName The class name of the component (e.g. 'MyComponent')
|
|
64
|
+
* @param event The Stencil component event.
|
|
65
|
+
* @returns The sanitized event type as a string.
|
|
66
|
+
*/
|
|
67
|
+
const formatOutputType = (componentClassName, event) => {
|
|
68
|
+
/**
|
|
69
|
+
* The original attribute contains the original type defined by the devs.
|
|
70
|
+
* This regexp normalizes the reference, by removing linebreaks,
|
|
71
|
+
* replacing consecutive spaces with a single space, and adding a single space after commas.
|
|
72
|
+
*/
|
|
73
|
+
return Object.entries(event.complexType.references)
|
|
74
|
+
.filter(([_, refObject]) => refObject.location === 'local' || refObject.location === 'import')
|
|
75
|
+
.reduce((type, [src, dst]) => {
|
|
76
|
+
const renamedType = `I${componentClassName}${type}`;
|
|
77
|
+
return renamedType
|
|
78
|
+
.replace(new RegExp(`^${src}$`, 'g'), `${dst}`)
|
|
79
|
+
.replace(new RegExp(`([^\\w])${src}([^\\w])`, 'g'), (v, p1, p2) => [p1, dst, p2].join(''));
|
|
80
|
+
}, event.complexType.original
|
|
81
|
+
.replace(/\n/g, ' ')
|
|
82
|
+
.replace(/\s{2,}/g, ' ')
|
|
83
|
+
.replace(/,\s*/g, ', '));
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Creates a formatted comment block based on the JS doc comment.
|
|
87
|
+
* @param doc The compiler jsdoc.
|
|
88
|
+
* @returns The formatted comment block as a string.
|
|
89
|
+
*/
|
|
90
|
+
const createDocComment = (doc) => {
|
|
91
|
+
if (doc.text.trim().length === 0 && doc.tags.length === 0) {
|
|
92
|
+
return '';
|
|
85
93
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
94
|
+
return `/**
|
|
95
|
+
* ${doc.text}${doc.tags.length > 0 ? ' ' : ''}${doc.tags.map((tag) => `@${tag.name} ${tag.text}`)}
|
|
96
|
+
*/`;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Creates the component interface type definition.
|
|
100
|
+
* @param tagNameAsPascal The tag name as PascalCase.
|
|
101
|
+
* @param events The events to generate the interface properties for.
|
|
102
|
+
* @param componentCorePackage The component core package.
|
|
103
|
+
* @param includeImportCustomElements Whether to include the import for the custom element definition.
|
|
104
|
+
* @param customElementsDir The custom elements directory.
|
|
105
|
+
* @returns The component interface type definition as a string.
|
|
106
|
+
*/
|
|
107
|
+
export const createComponentTypeDefinition = (tagNameAsPascal, events, componentCorePackage, includeImportCustomElements = false, customElementsDir) => {
|
|
108
|
+
const typeDefinition = `${createComponentEventTypeImports(tagNameAsPascal, events, {
|
|
109
|
+
componentCorePackage,
|
|
110
|
+
includeImportCustomElements,
|
|
111
|
+
customElementsDir,
|
|
112
|
+
})}
|
|
113
|
+
|
|
114
|
+
export interface ${tagNameAsPascal} extends Components.${tagNameAsPascal} {
|
|
115
|
+
${events
|
|
116
|
+
.map((event) => {
|
|
117
|
+
const comment = createDocComment(event.docs);
|
|
118
|
+
return `${comment.length > 0 ? ` ${comment}` : ''}
|
|
119
|
+
${event.name}: EventEmitter<CustomEvent<${formatOutputType(tagNameAsPascal, event)}>>;`;
|
|
120
|
+
})
|
|
121
|
+
.join('\n')}
|
|
122
|
+
}`;
|
|
123
|
+
return typeDefinition;
|
|
89
124
|
};
|
|
90
|
-
function getProxyCmp(tagName, includeCustomElement, inputs, methods) {
|
|
91
|
-
const hasInputs = inputs.length > 0;
|
|
92
|
-
const hasMethods = methods.length > 0;
|
|
93
|
-
const proxMeta = [
|
|
94
|
-
`defineCustomElementFn: ${includeCustomElement ? 'define' + dashToPascalCase(tagName) : 'undefined'}`
|
|
95
|
-
];
|
|
96
|
-
if (hasInputs)
|
|
97
|
-
proxMeta.push(`inputs: ['${inputs.join(`', '`)}']`);
|
|
98
|
-
if (hasMethods)
|
|
99
|
-
proxMeta.push(`methods: ['${methods.join(`', '`)}']`);
|
|
100
|
-
return `@ProxyCmp({\n ${proxMeta.join(',\n ')}\n})`;
|
|
101
|
-
}
|
|
@@ -4,7 +4,7 @@ export function generateAngularDirectivesFile(compilerCtx, components, outputTar
|
|
|
4
4
|
if (!outputTarget.directivesArrayFile) {
|
|
5
5
|
return Promise.resolve();
|
|
6
6
|
}
|
|
7
|
-
const proxyPath = relativeImport(outputTarget.directivesArrayFile, outputTarget.
|
|
7
|
+
const proxyPath = relativeImport(outputTarget.directivesArrayFile, outputTarget.proxyDeclarationFile, '.ts');
|
|
8
8
|
const directives = components
|
|
9
9
|
.map((cmpMeta) => dashToPascalCase(cmpMeta.tagName))
|
|
10
10
|
.map((className) => `d.${className}`)
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an Angular module declaration for a component wrapper.
|
|
3
|
+
* @param componentTagName The tag name of the Stencil component.
|
|
4
|
+
* @returns The Angular module declaration as a string.
|
|
5
|
+
*/
|
|
6
|
+
export declare const generateAngularModuleForComponent: (componentTagName: string) => string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { dashToPascalCase } from './utils';
|
|
2
|
+
/**
|
|
3
|
+
* Creates an Angular module declaration for a component wrapper.
|
|
4
|
+
* @param componentTagName The tag name of the Stencil component.
|
|
5
|
+
* @returns The Angular module declaration as a string.
|
|
6
|
+
*/
|
|
7
|
+
export const generateAngularModuleForComponent = (componentTagName) => {
|
|
8
|
+
const tagNameAsPascal = dashToPascalCase(componentTagName);
|
|
9
|
+
const componentClassName = `${tagNameAsPascal}`;
|
|
10
|
+
const moduleClassName = `${tagNameAsPascal}Module`;
|
|
11
|
+
const moduleDefinition = `@NgModule({
|
|
12
|
+
declarations: [${componentClassName}],
|
|
13
|
+
exports: [${componentClassName}]
|
|
14
|
+
})
|
|
15
|
+
export class ${moduleClassName} { }`;
|
|
16
|
+
return moduleDefinition;
|
|
17
|
+
};
|
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
import { EOL } from 'os';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
export default async function generateValueAccessors(compilerCtx, components, outputTarget, config) {
|
|
4
|
-
if (!Array.isArray(outputTarget.valueAccessorConfigs) ||
|
|
5
|
-
outputTarget.valueAccessorConfigs.length === 0) {
|
|
4
|
+
if (!Array.isArray(outputTarget.valueAccessorConfigs) || outputTarget.valueAccessorConfigs.length === 0) {
|
|
6
5
|
return;
|
|
7
6
|
}
|
|
8
|
-
const targetDir = path.dirname(outputTarget.
|
|
7
|
+
const targetDir = path.dirname(outputTarget.proxyDeclarationFile);
|
|
9
8
|
const normalizedValueAccessors = outputTarget.valueAccessorConfigs.reduce((allAccessors, va) => {
|
|
10
|
-
const elementSelectors = Array.isArray(va.elementSelectors)
|
|
11
|
-
? va.elementSelectors
|
|
12
|
-
: [va.elementSelectors];
|
|
9
|
+
const elementSelectors = Array.isArray(va.elementSelectors) ? va.elementSelectors : [va.elementSelectors];
|
|
13
10
|
const type = va.type;
|
|
14
11
|
let allElementSelectors = [];
|
|
15
12
|
let allEventTargets = [];
|