UNPKG

2.69 kBJavaScriptView Raw
1import { normalizePath } from './utils';
2import { reactProxyOutput } from './output-react';
3import path from 'path';
4/**
5 * Creates an output target for binding Stencil components to be used in a React context
6 * @param outputTarget the user-defined output target defined in a Stencil configuration file
7 * @returns an output target that can be used by the Stencil compiler
8 */
9export const reactOutputTarget = (outputTarget) => ({
10 type: 'custom',
11 name: 'react-library',
12 validate(config) {
13 return normalizeOutputTarget(config, outputTarget);
14 },
15 async generator(config, compilerCtx, buildCtx) {
16 const timespan = buildCtx.createTimeSpan(`generate react started`, true);
17 await reactProxyOutput(config, compilerCtx, outputTarget, buildCtx.components);
18 timespan.finish(`generate react finished`);
19 },
20});
21/**
22 * Normalizes the structure of a provided output target and verifies a Stencil configuration
23 * associated with the wrapper is valid
24 * @param config the configuration to validate
25 * @param outputTarget the output target to normalize
26 * @returns an output target that's been normalized
27 */
28export function normalizeOutputTarget(config, outputTarget) {
29 var _a, _b;
30 const results = Object.assign(Object.assign({}, outputTarget), { excludeComponents: outputTarget.excludeComponents || [], includePolyfills: (_a = outputTarget.includePolyfills) !== null && _a !== void 0 ? _a : true, includeDefineCustomElements: (_b = outputTarget.includeDefineCustomElements) !== null && _b !== void 0 ? _b : true });
31 if (config.rootDir == null) {
32 throw new Error('rootDir is not set and it should be set by stencil itself');
33 }
34 if (outputTarget.proxiesFile == null) {
35 throw new Error('proxiesFile is required');
36 }
37 if (outputTarget.includeDefineCustomElements && outputTarget.includeImportCustomElements) {
38 throw new Error('includeDefineCustomElements cannot be used at the same time as includeImportCustomElements since includeDefineCustomElements is used for lazy loading components. Set `includeDefineCustomElements: false` in your React output target config to resolve this.');
39 }
40 if (outputTarget.includeImportCustomElements && outputTarget.includePolyfills) {
41 throw new Error('includePolyfills cannot be used at the same time as includeImportCustomElements. Set `includePolyfills: false` in your React output target config to resolve this.');
42 }
43 if (outputTarget.directivesProxyFile && !path.isAbsolute(outputTarget.directivesProxyFile)) {
44 results.proxiesFile = normalizePath(path.join(config.rootDir, outputTarget.proxiesFile));
45 }
46 return results;
47}