@stencil/angular-output-target 0.6.1-dev.11655483906.1e51ded4 → 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.
@@ -1,15 +1,16 @@
1
1
  import path from 'path';
2
- import { relativeImport, normalizePath, sortBy, readPackageJson, dashToPascalCase, formatCustomEventInterfaceName } from './utils';
3
- import { createComponentDefinition } from './generate-angular-component';
2
+ import { relativeImport, normalizePath, sortBy, readPackageJson, dashToPascalCase, createImportStatement, } from './utils';
3
+ import { createAngularComponentDefinition, createComponentTypeDefinition } from './generate-angular-component';
4
4
  import { generateAngularDirectivesFile } from './generate-angular-directives-file';
5
5
  import generateValueAccessors from './generate-value-accessors';
6
+ import { generateAngularModuleForComponent } from './generate-angular-modules';
6
7
  export async function angularDirectiveProxyOutput(compilerCtx, outputTarget, components, config) {
7
8
  const filteredComponents = getFilteredComponents(outputTarget.excludeComponents, components);
8
9
  const rootDir = config.rootDir;
9
10
  const pkgData = await readPackageJson(config, rootDir);
10
11
  const finalText = generateProxies(filteredComponents, pkgData, outputTarget, config.rootDir);
11
12
  await Promise.all([
12
- compilerCtx.fs.writeFile(outputTarget.directivesProxyFile, finalText),
13
+ compilerCtx.fs.writeFile(outputTarget.proxyDeclarationFile, finalText),
13
14
  copyResources(config, outputTarget),
14
15
  generateAngularDirectivesFile(compilerCtx, filteredComponents, outputTarget),
15
16
  generateValueAccessors(compilerCtx, filteredComponents, outputTarget, config),
@@ -23,7 +24,7 @@ async function copyResources(config, outputTarget) {
23
24
  throw new Error('stencil is not properly initialized at this step. Notify the developer');
24
25
  }
25
26
  const srcDirectory = path.join(__dirname, '..', 'angular-component-lib');
26
- const destDirectory = path.join(path.dirname(outputTarget.directivesProxyFile), 'angular-component-lib');
27
+ const destDirectory = path.join(path.dirname(outputTarget.proxyDeclarationFile), 'angular-component-lib');
27
28
  return config.sys.copy([
28
29
  {
29
30
  src: srcDirectory,
@@ -34,14 +35,34 @@ async function copyResources(config, outputTarget) {
34
35
  ], srcDirectory);
35
36
  }
36
37
  export function generateProxies(components, pkgData, outputTarget, rootDir) {
38
+ var _a;
37
39
  const distTypesDir = path.dirname(pkgData.types);
38
40
  const dtsFilePath = path.join(rootDir, distTypesDir, GENERATED_DTS);
39
- const componentsTypeFile = relativeImport(outputTarget.directivesProxyFile, dtsFilePath, '.d.ts');
41
+ const componentsTypeFile = relativeImport(outputTarget.proxyDeclarationFile, dtsFilePath, '.d.ts');
42
+ const createSingleComponentAngularModules = (_a = outputTarget.createSingleComponentAngularModules) !== null && _a !== void 0 ? _a : false;
43
+ /**
44
+ * The collection of named imports from @angular/core.
45
+ */
46
+ const angularCoreImports = [
47
+ 'ChangeDetectionStrategy',
48
+ 'ChangeDetectorRef',
49
+ 'Component',
50
+ 'ElementRef',
51
+ 'EventEmitter',
52
+ 'NgZone',
53
+ ];
54
+ /**
55
+ * The collection of named imports from the angular-component-lib/utils.
56
+ */
57
+ const componentLibImports = ['ProxyCmp', 'proxyOutputs'];
58
+ if (createSingleComponentAngularModules) {
59
+ angularCoreImports.push('NgModule');
60
+ }
40
61
  const imports = `/* tslint:disable */
41
62
  /* auto-generated angular directive proxies */
42
- import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, EventEmitter, NgZone } from '@angular/core';
43
- import { ProxyCmp, proxyOutputs } from './angular-component-lib/utils';\n`;
44
- const importLocation = getImportPackageName(outputTarget, componentsTypeFile);
63
+ ${createImportStatement(angularCoreImports, '@angular/core')}
64
+
65
+ ${createImportStatement(componentLibImports, './angular-component-lib/utils')}\n`;
45
66
  /**
46
67
  * Generate JSX import type from correct location.
47
68
  * When using custom elements build, we need to import from
@@ -49,21 +70,15 @@ import { ProxyCmp, proxyOutputs } from './angular-component-lib/utils';\n`;
49
70
  * otherwise we risk bundlers pulling in lazy loaded imports.
50
71
  */
51
72
  const generateTypeImports = () => {
73
+ let importLocation = outputTarget.componentCorePackage
74
+ ? normalizePath(outputTarget.componentCorePackage)
75
+ : normalizePath(componentsTypeFile);
76
+ importLocation += outputTarget.includeImportCustomElements
77
+ ? `/${outputTarget.customElementsDir || 'components'}`
78
+ : '';
52
79
  return `import ${outputTarget.includeImportCustomElements ? 'type ' : ''}{ ${IMPORT_TYPES} } from '${importLocation}';\n`;
53
80
  };
54
- /**
55
- * Components that have custom events have uniquely generated interfaces as of
56
- * Stencil 2.16.0. We import these interfaces so they can be used in the generate
57
- * functions for the @Output() events.
58
- */
59
- const customEventInterfaces = components.filter(c => c.events.filter(e => !e.internal).length > 0).map(c => {
60
- return formatCustomEventInterfaceName(c.tagName);
61
- });
62
- const customEventImports = `import type { ${customEventInterfaces.join(', ')} } from '${importLocation}';\n`;
63
- const typeImports = [
64
- generateTypeImports(),
65
- customEventImports,
66
- ];
81
+ const typeImports = generateTypeImports();
67
82
  let sourceImports = '';
68
83
  /**
69
84
  * Build an array of Custom Elements build imports and namespace them
@@ -72,30 +87,53 @@ import { ProxyCmp, proxyOutputs } from './angular-component-lib/utils';\n`;
72
87
  * IonButton React Component that takes in the Web Component as a parameter.
73
88
  */
74
89
  if (outputTarget.includeImportCustomElements && outputTarget.componentCorePackage !== undefined) {
75
- const cmpImports = components.map(component => {
90
+ const cmpImports = components.map((component) => {
76
91
  const pascalImport = dashToPascalCase(component.tagName);
77
- return `import { defineCustomElement as define${pascalImport} } from '${normalizePath(outputTarget.componentCorePackage)}/${outputTarget.customElementsDir ||
78
- 'components'}/${component.tagName}.js';`;
92
+ return `import { defineCustomElement as define${pascalImport} } from '${normalizePath(outputTarget.componentCorePackage)}/${outputTarget.customElementsDir || 'components'}/${component.tagName}.js';`;
79
93
  });
80
94
  sourceImports = cmpImports.join('\n');
81
95
  }
82
- const final = [
83
- imports,
84
- typeImports.join('\n'),
85
- sourceImports,
86
- components
87
- .map(createComponentDefinition(outputTarget.componentCorePackage, distTypesDir, rootDir, outputTarget.includeImportCustomElements, outputTarget.customElementsDir))
88
- .join('\n'),
89
- ];
96
+ if (createSingleComponentAngularModules) {
97
+ // Generating Angular modules is only supported in the dist-custom-elements build
98
+ if (!outputTarget.includeImportCustomElements) {
99
+ throw new Error('Generating single component Angular modules requires the "includeImportCustomElements" option to be set to true.');
100
+ }
101
+ }
102
+ const proxyFileOutput = [];
103
+ const filterInternalProps = (prop) => !prop.internal;
104
+ const mapPropName = (prop) => prop.name;
105
+ const { includeImportCustomElements, componentCorePackage, customElementsDir } = outputTarget;
106
+ for (let cmpMeta of components) {
107
+ const tagNameAsPascal = dashToPascalCase(cmpMeta.tagName);
108
+ const inputs = [];
109
+ if (cmpMeta.properties) {
110
+ inputs.push(...cmpMeta.properties.filter(filterInternalProps).map(mapPropName));
111
+ }
112
+ if (cmpMeta.virtualProperties) {
113
+ inputs.push(...cmpMeta.virtualProperties.map(mapPropName));
114
+ }
115
+ inputs.sort();
116
+ const outputs = [];
117
+ if (cmpMeta.events) {
118
+ outputs.push(...cmpMeta.events.filter(filterInternalProps).map(mapPropName));
119
+ }
120
+ const methods = [];
121
+ if (cmpMeta.methods) {
122
+ methods.push(...cmpMeta.methods.filter(filterInternalProps).map(mapPropName));
123
+ }
124
+ /**
125
+ * For each component, we need to generate:
126
+ * 1. The @Component decorated class
127
+ * 2. Optionally the @NgModule decorated class (if createSingleComponentAngularModules is true)
128
+ * 3. The component interface (using declaration merging for types).
129
+ */
130
+ const componentDefinition = createAngularComponentDefinition(cmpMeta.tagName, inputs, outputs, methods, includeImportCustomElements);
131
+ const moduleDefinition = generateAngularModuleForComponent(cmpMeta.tagName);
132
+ const componentTypeDefinition = createComponentTypeDefinition(tagNameAsPascal, cmpMeta.events, componentCorePackage, includeImportCustomElements, customElementsDir);
133
+ proxyFileOutput.push(componentDefinition, '\n', createSingleComponentAngularModules ? moduleDefinition : '', '\n', componentTypeDefinition, '\n');
134
+ }
135
+ const final = [imports, typeImports, sourceImports, ...proxyFileOutput];
90
136
  return final.join('\n') + '\n';
91
137
  }
92
- /**
93
- * Returns the path to import the file from.
94
- */
95
- const getImportPackageName = (outputTarget, componentsTypeFile) => {
96
- let importLocation = outputTarget.componentCorePackage ? normalizePath(outputTarget.componentCorePackage) : normalizePath(componentsTypeFile);
97
- importLocation += outputTarget.includeImportCustomElements ? `/${outputTarget.customElementsDir || 'components'}` : '';
98
- return importLocation;
99
- };
100
138
  const GENERATED_DTS = 'components.d.ts';
101
139
  const IMPORT_TYPES = 'Components';
package/dist/plugin.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import type { Config, OutputTargetCustom } from '@stencil/core/internal';
2
2
  import type { OutputTargetAngular } from './types';
3
3
  export declare const angularOutputTarget: (outputTarget: OutputTargetAngular) => OutputTargetCustom;
4
- export declare function normalizeOutputTarget(config: Config, outputTarget: any): OutputTargetAngular;
4
+ export declare function normalizeOutputTarget(config: Config, outputTarget: OutputTargetAngular): OutputTargetAngular;
package/dist/plugin.js CHANGED
@@ -14,15 +14,18 @@ export const angularOutputTarget = (outputTarget) => ({
14
14
  },
15
15
  });
16
16
  export function normalizeOutputTarget(config, outputTarget) {
17
- const results = Object.assign(Object.assign({}, outputTarget), { excludeComponents: outputTarget.excludeComponents || [], valueAccessorConfig: outputTarget.valueAccessorConfig || [] });
17
+ const results = Object.assign(Object.assign({}, outputTarget), { excludeComponents: outputTarget.excludeComponents || [], valueAccessorConfigs: outputTarget.valueAccessorConfigs || [] });
18
18
  if (config.rootDir == null) {
19
19
  throw new Error('rootDir is not set and it should be set by stencil itself');
20
20
  }
21
- if (outputTarget.directivesProxyFile == null) {
22
- throw new Error('directivesProxyFile is required');
21
+ if (outputTarget.directivesProxyFile !== undefined) {
22
+ throw new Error('directivesProxyFile has been removed. Use proxyDeclarationFile instead.');
23
23
  }
24
- if (outputTarget.directivesProxyFile && !path.isAbsolute(outputTarget.directivesProxyFile)) {
25
- results.directivesProxyFile = normalizePath(path.join(config.rootDir, outputTarget.directivesProxyFile));
24
+ if (outputTarget.proxyDeclarationFile == null) {
25
+ throw new Error('proxyDeclarationFile is required. Please set it in the Stencil config.');
26
+ }
27
+ if (outputTarget.proxyDeclarationFile && !path.isAbsolute(outputTarget.proxyDeclarationFile)) {
28
+ results.proxyDeclarationFile = normalizePath(path.join(config.rootDir, outputTarget.proxyDeclarationFile));
26
29
  }
27
30
  if (outputTarget.directivesArrayFile && !path.isAbsolute(outputTarget.directivesArrayFile)) {
28
31
  results.directivesArrayFile = normalizePath(path.join(config.rootDir, outputTarget.directivesArrayFile));
package/dist/types.d.ts CHANGED
@@ -1,12 +1,28 @@
1
1
  export interface OutputTargetAngular {
2
- componentCorePackage?: string;
3
- directivesProxyFile: string;
2
+ /**
3
+ * The package name of the component library.
4
+ * This is used to generate the import statements.
5
+ */
6
+ componentCorePackage: string;
7
+ /**
8
+ * @deprecated Use `proxyDeclarationFile` instead. This property has been replaced.
9
+ */
10
+ directivesProxyFile?: string;
4
11
  directivesArrayFile?: string;
5
12
  directivesUtilsFile?: string;
13
+ /**
14
+ * The path to the proxy file that will be generated. This can be an absolute path
15
+ * or a relative path from the root directory of the Stencil library.
16
+ */
17
+ proxyDeclarationFile: string;
6
18
  valueAccessorConfigs?: ValueAccessorConfig[];
7
19
  excludeComponents?: string[];
8
20
  includeImportCustomElements?: boolean;
9
21
  customElementsDir?: string;
22
+ /**
23
+ * `true` to generate a single component Angular module for each component.
24
+ */
25
+ createSingleComponentAngularModules?: boolean;
10
26
  }
11
27
  export declare type ValueAccessorTypes = 'text' | 'radio' | 'select' | 'number' | 'boolean';
12
28
  export interface ValueAccessorConfig {
package/dist/utils.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Config } from '@stencil/core/internal';
1
+ import { ComponentCompilerEvent, Config } from '@stencil/core/internal';
2
2
  import type { PackageJSON } from './types';
3
3
  export declare const toLowerCase: (str: string) => string;
4
4
  export declare const dashToPascalCase: (str: string) => string;
@@ -8,10 +8,28 @@ export declare function relativeImport(pathFrom: string, pathTo: string, ext?: s
8
8
  export declare function isRelativePath(path: string): boolean | "";
9
9
  export declare function readPackageJson(config: Config, rootDir: string): Promise<PackageJSON>;
10
10
  /**
11
- * Returns the formatted custom event interface name for the component.
12
- * With a tag of "my-cmp", it will return "MyCmpCustomEvent".
11
+ * Formats an array of strings to a string of quoted, comma separated values.
12
+ * @param list The list of unformatted strings to format
13
+ * @returns The formatted array of strings. (e.g. ['foo', 'bar']) => `'foo', 'bar'`
14
+ */
15
+ export declare const formatToQuotedList: (list: readonly string[]) => string;
16
+ /**
17
+ * Creates an import statement for a list of named imports from a module.
18
+ * @param imports The list of named imports.
19
+ * @param module The module to import from.
13
20
  *
14
- * @param componentTagName The tag selector of the component (i.e. my-cmp)
15
- * @returns The formatted custom event interface name.
21
+ * @returns The import statement as a string.
22
+ */
23
+ export declare const createImportStatement: (imports: string[], module: string) => string;
24
+ /**
25
+ * Creates the collection of import statements for a component based on the component's events type dependencies.
26
+ * @param componentTagName The tag name of the component (pascal case).
27
+ * @param events The events compiler metadata.
28
+ * @param options The options for generating the import statements (e.g. whether to import from the custom elements directory).
29
+ * @returns The import statements as an array of strings.
16
30
  */
17
- export declare function formatCustomEventInterfaceName(componentTagName: string): string;
31
+ export declare const createComponentEventTypeImports: (componentTagName: string, events: readonly ComponentCompilerEvent[], options: {
32
+ componentCorePackage: string;
33
+ includeImportCustomElements?: boolean;
34
+ customElementsDir?: string;
35
+ }) => string;
package/dist/utils.js CHANGED
@@ -75,15 +75,50 @@ export async function readPackageJson(config, rootDir) {
75
75
  return pkgData;
76
76
  }
77
77
  /**
78
- * Returns the formatted custom event interface name for the component.
79
- * With a tag of "my-cmp", it will return "MyCmpCustomEvent".
78
+ * Formats an array of strings to a string of quoted, comma separated values.
79
+ * @param list The list of unformatted strings to format
80
+ * @returns The formatted array of strings. (e.g. ['foo', 'bar']) => `'foo', 'bar'`
81
+ */
82
+ export const formatToQuotedList = (list) => list.map((item) => `'${item}'`).join(', ');
83
+ /**
84
+ * Creates an import statement for a list of named imports from a module.
85
+ * @param imports The list of named imports.
86
+ * @param module The module to import from.
80
87
  *
81
- * @param componentTagName The tag selector of the component (i.e. my-cmp)
82
- * @returns The formatted custom event interface name.
88
+ * @returns The import statement as a string.
83
89
  */
84
- export function formatCustomEventInterfaceName(componentTagName) {
85
- return `${dashToPascalCase(componentTagName)}CustomEvent`;
86
- }
90
+ export const createImportStatement = (imports, module) => {
91
+ if (imports.length === 0) {
92
+ return '';
93
+ }
94
+ return `import { ${imports.join(', ')} } from '${module}';`;
95
+ };
96
+ /**
97
+ * Creates the collection of import statements for a component based on the component's events type dependencies.
98
+ * @param componentTagName The tag name of the component (pascal case).
99
+ * @param events The events compiler metadata.
100
+ * @param options The options for generating the import statements (e.g. whether to import from the custom elements directory).
101
+ * @returns The import statements as an array of strings.
102
+ */
103
+ export const createComponentEventTypeImports = (componentTagName, events, options) => {
104
+ const { componentCorePackage, includeImportCustomElements, customElementsDir } = options;
105
+ const imports = [];
106
+ const namedImports = new Set();
107
+ const importPathName = normalizePath(componentCorePackage) + (includeImportCustomElements ? `/${customElementsDir || 'components'}` : '');
108
+ events.forEach((event) => {
109
+ Object.entries(event.complexType.references).forEach(([typeName, refObject]) => {
110
+ if (refObject.location === 'local' || refObject.location === 'import') {
111
+ const newTypeName = `I${componentTagName}${typeName}`;
112
+ // Prevents duplicate imports for the same type.
113
+ if (!namedImports.has(newTypeName)) {
114
+ imports.push(`import type { ${typeName} as ${newTypeName} } from '${importPathName}';`);
115
+ namedImports.add(newTypeName);
116
+ }
117
+ }
118
+ });
119
+ });
120
+ return imports.join('\n');
121
+ };
87
122
  const EXTENDED_PATH_REGEX = /^\\\\\?\\/;
88
123
  const NON_ASCII_REGEX = /[^\x00-\x80]+/;
89
124
  const SLASH_REGEX = /\\/g;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stencil/angular-output-target",
3
- "version": "0.6.1-dev.11655483906.1e51ded4",
3
+ "version": "0.6.1-dev.11662151255.17ea4066",
4
4
  "description": "Angular output target for @stencil/core components.",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.js",
@@ -20,6 +20,9 @@
20
20
  "watch": "tsc --watch",
21
21
  "rollup": "rollup -c",
22
22
  "version": "npm run build",
23
+ "prettier": "npm run prettier.base -- --write",
24
+ "prettier.base": "prettier \"./({angular-component-lib,src,test,__tests__}/**/*.{ts,tsx,js,jsx})|*.{ts,tsx,js,jsx}\"",
25
+ "prettier.dry-run": "npm run prettier.base -- --list-different",
23
26
  "release": "np",
24
27
  "test": "jest --passWithNoTests",
25
28
  "test.watch": "jest --watch"
@@ -39,7 +42,7 @@
39
42
  "@angular/forms": "8.2.14"
40
43
  },
41
44
  "peerDependencies": {
42
- "@stencil/core": "^2.16.0"
45
+ "@stencil/core": "^2.9.0"
43
46
  },
44
47
  "jest": {
45
48
  "transform": {
@@ -55,5 +58,5 @@
55
58
  ],
56
59
  "testURL": "http://localhost"
57
60
  },
58
- "gitHead": "e51ded46a46fbcd7d0f191a9dc237bef362aab9f"
61
+ "gitHead": "7ea40666153180bc16a62602feba11b767d8e396"
59
62
  }