UNPKG

4.53 kBJavaScriptView Raw
1import path from 'path';
2import { relativeImport, normalizePath, sortBy, readPackageJson, dashToPascalCase } from './utils';
3import { createComponentDefinition } from './generate-angular-component';
4import { generateAngularDirectivesFile } from './generate-angular-directives-file';
5import generateValueAccessors from './generate-value-accessors';
6export async function angularDirectiveProxyOutput(compilerCtx, outputTarget, components, config) {
7 const filteredComponents = getFilteredComponents(outputTarget.excludeComponents, components);
8 const rootDir = config.rootDir;
9 const pkgData = await readPackageJson(config, rootDir);
10 const finalText = generateProxies(filteredComponents, pkgData, outputTarget, config.rootDir);
11 await Promise.all([
12 compilerCtx.fs.writeFile(outputTarget.directivesProxyFile, finalText),
13 copyResources(config, outputTarget),
14 generateAngularDirectivesFile(compilerCtx, filteredComponents, outputTarget),
15 generateValueAccessors(compilerCtx, filteredComponents, outputTarget, config),
16 ]);
17}
18function getFilteredComponents(excludeComponents = [], cmps) {
19 return sortBy(cmps, (cmp) => cmp.tagName).filter((c) => !excludeComponents.includes(c.tagName) && !c.internal);
20}
21async function copyResources(config, outputTarget) {
22 if (!config.sys || !config.sys.copy || !config.sys.glob) {
23 throw new Error('stencil is not properly initialized at this step. Notify the developer');
24 }
25 const srcDirectory = path.join(__dirname, '..', 'angular-component-lib');
26 const destDirectory = path.join(path.dirname(outputTarget.directivesProxyFile), 'angular-component-lib');
27 return config.sys.copy([
28 {
29 src: srcDirectory,
30 dest: destDirectory,
31 keepDirStructure: false,
32 warn: false,
33 },
34 ], srcDirectory);
35}
36export function generateProxies(components, pkgData, outputTarget, rootDir) {
37 const distTypesDir = path.dirname(pkgData.types);
38 const dtsFilePath = path.join(rootDir, distTypesDir, GENERATED_DTS);
39 const componentsTypeFile = relativeImport(outputTarget.directivesProxyFile, dtsFilePath, '.d.ts');
40 const imports = `/* tslint:disable */
41/* auto-generated angular directive proxies */
42import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, EventEmitter, NgZone } from '@angular/core';
43import { ProxyCmp, proxyOutputs } from './angular-component-lib/utils';\n`;
44 /**
45 * Generate JSX import type from correct location.
46 * When using custom elements build, we need to import from
47 * either the "components" directory or customElementsDir
48 * otherwise we risk bundlers pulling in lazy loaded imports.
49 */
50 const generateTypeImports = () => {
51 let importLocation = outputTarget.componentCorePackage ? normalizePath(outputTarget.componentCorePackage) : normalizePath(componentsTypeFile);
52 importLocation += outputTarget.includeImportCustomElements ? `/${outputTarget.customElementsDir || 'components'}` : '';
53 return `import ${outputTarget.includeImportCustomElements ? 'type ' : ''}{ ${IMPORT_TYPES} } from '${importLocation}';\n`;
54 };
55 const typeImports = generateTypeImports();
56 let sourceImports = '';
57 /**
58 * Build an array of Custom Elements build imports and namespace them
59 * so that they do not conflict with the React wrapper names. For example,
60 * IonButton would be imported as IonButtonCmp so as to not conflict with the
61 * IonButton React Component that takes in the Web Component as a parameter.
62 */
63 if (outputTarget.includeImportCustomElements && outputTarget.componentCorePackage !== undefined) {
64 const cmpImports = components.map(component => {
65 const pascalImport = dashToPascalCase(component.tagName);
66 return `import { defineCustomElement as define${pascalImport} } from '${normalizePath(outputTarget.componentCorePackage)}/${outputTarget.customElementsDir ||
67 'components'}/${component.tagName}.js';`;
68 });
69 sourceImports = cmpImports.join('\n');
70 }
71 const final = [
72 imports,
73 typeImports,
74 sourceImports,
75 components
76 .map(createComponentDefinition(outputTarget.componentCorePackage, distTypesDir, rootDir, outputTarget.includeImportCustomElements, outputTarget.customElementsDir))
77 .join('\n'),
78 ];
79 return final.join('\n') + '\n';
80}
81const GENERATED_DTS = 'components.d.ts';
82const IMPORT_TYPES = 'Components';