/
validate-custom-element.ts
67 lines (60 loc) · 2.34 KB
/
validate-custom-element.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { isBoolean } from '@utils';
import { join } from 'path';
import type {
OutputTarget,
OutputTargetCopy,
OutputTargetDistCustomElements,
OutputTargetDistTypes,
ValidatedConfig,
} from '../../../declarations';
import { COPY, DIST_TYPES, isOutputTargetDistCustomElements } from '../../output-targets/output-utils';
import { getAbsolutePath } from '../config-utils';
import { validateCopy } from '../validate-copy';
/**
* Validate one or more `dist-custom-elements` output targets. Validation of an output target may involve back-filling
* fields that are omitted with sensible defaults and/or creating additional supporting output targets that were not
* explicitly defined by the user
* @param config the Stencil configuration associated with the project being compiled
* @param userOutputs the output target(s) specified by the user
* @returns the validated output target(s)
*/
export const validateCustomElement = (
config: ValidatedConfig,
userOutputs: ReadonlyArray<OutputTarget>
): ReadonlyArray<OutputTargetDistCustomElements | OutputTargetDistTypes | OutputTargetCopy> => {
const defaultDir = 'dist';
return userOutputs.filter(isOutputTargetDistCustomElements).reduce((outputs, o) => {
const outputTarget = {
...o,
dir: getAbsolutePath(config, o.dir || join(defaultDir, 'components')),
};
if (!isBoolean(outputTarget.empty)) {
outputTarget.empty = true;
}
if (!isBoolean(outputTarget.externalRuntime)) {
outputTarget.externalRuntime = true;
}
if (!isBoolean(outputTarget.generateTypeDeclarations)) {
outputTarget.generateTypeDeclarations = true;
}
// unlike other output targets, Stencil does not allow users to define the output location of types at this time
if (outputTarget.generateTypeDeclarations) {
const typesDirectory = getAbsolutePath(config, join(defaultDir, 'types'));
outputs.push({
type: DIST_TYPES,
dir: outputTarget.dir,
typesDir: typesDirectory,
});
}
outputTarget.copy = validateCopy(outputTarget.copy, []);
if (outputTarget.copy.length > 0) {
outputs.push({
type: COPY,
dir: config.rootDir,
copy: [...outputTarget.copy],
});
}
outputs.push(outputTarget);
return outputs;
}, [] as (OutputTargetDistCustomElements | OutputTargetCopy | OutputTargetDistTypes)[]);
};