1 | import path from 'path';
|
2 | import { relativeImport, normalizePath, sortBy, readPackageJson, dashToPascalCase } from './utils';
|
3 | import { createComponentDefinition } from './generate-angular-component';
|
4 | import { generateAngularDirectivesFile } from './generate-angular-directives-file';
|
5 | import generateValueAccessors from './generate-value-accessors';
|
6 | export 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 | }
|
18 | function getFilteredComponents(excludeComponents = [], cmps) {
|
19 | return sortBy(cmps, (cmp) => cmp.tagName).filter((c) => !excludeComponents.includes(c.tagName) && !c.internal);
|
20 | }
|
21 | async 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 | }
|
36 | export 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 */
|
42 | import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, EventEmitter, NgZone } from '@angular/core';
|
43 | import { ProxyCmp, proxyOutputs } from './angular-component-lib/utils';\n`;
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
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 |
|
59 |
|
60 |
|
61 |
|
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 | }
|
81 | const GENERATED_DTS = 'components.d.ts';
|
82 | const IMPORT_TYPES = 'Components';
|