Skip to content

Commit c4d894f

Browse files
chore(compiler): make outputTargets required on the ValidatedConfig (#3477)
This makes the `outputTargets` field required on the `ValidatedConfig` type, allowing us to freely access `.outputTargets` on a validated configuration object in the compiler code without having to use the `!` operator, check `=== undefined`, etc. This includes changes to the places where a `ValidatedConfig` object is created to default `.outputTargets` to `[]` if no suitable value is present.
1 parent 0b9d4b0 commit c4d894f

File tree

12 files changed

+60
-28
lines changed

12 files changed

+60
-28
lines changed

src/cli/run.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,14 @@ export const run = async (init: d.CliInitOptions) => {
7575
loadedCompilerLog(sys, logger, flags, coreCompiler);
7676

7777
if (task === 'info') {
78-
await telemetryAction(sys, { flags: createConfigFlags({ task: 'info' }), logger }, coreCompiler, async () => {
79-
await taskInfo(coreCompiler, sys, logger);
80-
});
78+
await telemetryAction(
79+
sys,
80+
{ flags: createConfigFlags({ task: 'info' }), logger, outputTargets: [] },
81+
coreCompiler,
82+
async () => {
83+
await taskInfo(coreCompiler, sys, logger);
84+
}
85+
);
8186
return;
8287
}
8388

@@ -122,9 +127,12 @@ export const runTask = async (
122127
sys?: d.CompilerSystem
123128
) => {
124129
const logger = config.logger ?? createLogger();
125-
const strictConfig: ValidatedConfig = { ...config, flags: createConfigFlags(config.flags ?? { task }), logger };
126-
127-
strictConfig.outputTargets = strictConfig.outputTargets || [];
130+
const strictConfig: ValidatedConfig = {
131+
...config,
132+
flags: createConfigFlags(config.flags ?? { task }),
133+
logger,
134+
outputTargets: config.outputTargets ?? [],
135+
};
128136

129137
switch (task) {
130138
case 'build':

src/compiler/config/test/validate-service-worker.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('validateServiceWorker', () => {
1616
devMode: false,
1717
flags: createConfigFlags(),
1818
logger: mockLogger(),
19+
outputTargets: [],
1920
};
2021
});
2122

src/compiler/config/validate-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const validateConfig = (
5151
// flags _should_ be JSON safe
5252
flags: JSON.parse(JSON.stringify(config.flags || {})),
5353
logger,
54+
outputTargets: config.outputTargets ?? [],
5455
};
5556

5657
// default devMode false

src/compiler/output-targets/dist-custom-elements/custom-elements-types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import { normalizePath, dashToPascalCase } from '@utils';
1212
* @param typesDir the path to the directory where type declarations are saved
1313
*/
1414
export const generateCustomElementsTypes = async (
15-
config: d.Config,
15+
config: d.ValidatedConfig,
1616
compilerCtx: d.CompilerCtx,
1717
buildCtx: d.BuildCtx,
1818
typesDir: string
1919
): Promise<void> => {
20-
const outputTargets = (config.outputTargets ?? []).filter(isOutputTargetDistCustomElements);
20+
const outputTargets = config.outputTargets.filter(isOutputTargetDistCustomElements);
2121

2222
await Promise.all(
2323
outputTargets.map((outputTarget) =>
@@ -36,7 +36,7 @@ export const generateCustomElementsTypes = async (
3636
* @param outputTarget the output target for which types are being currently generated
3737
*/
3838
const generateCustomElementsTypesOutput = async (
39-
config: d.Config,
39+
config: d.ValidatedConfig,
4040
compilerCtx: d.CompilerCtx,
4141
buildCtx: d.BuildCtx,
4242
typesDir: string,

src/compiler/output-targets/dist-custom-elements/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ import ts from 'typescript';
2828
* which do actual work of generating the rollup configuration, creating an
2929
* entry chunk, running, the build, etc.
3030
*
31-
* @param config the user-supplied compiler configuration we're using
31+
* @param config the validated compiler configuration we're using
3232
* @param compilerCtx the current compiler context
3333
* @param buildCtx the current build context
3434
* @returns an empty Promise which won't resolve until the work is done!
3535
*/
3636
export const outputCustomElements = async (
37-
config: d.Config,
37+
config: d.ValidatedConfig,
3838
compilerCtx: d.CompilerCtx,
3939
buildCtx: d.BuildCtx
4040
): Promise<void> => {
4141
if (!config.buildDist) {
4242
return;
4343
}
4444

45-
const outputTargets = (config.outputTargets ?? []).filter(isOutputTargetDistCustomElements);
45+
const outputTargets = config.outputTargets.filter(isOutputTargetDistCustomElements);
4646
if (outputTargets.length === 0) {
4747
return;
4848
}
@@ -59,14 +59,14 @@ export const outputCustomElements = async (
5959
* Get bundle options for our current build and compiler context which we'll use
6060
* to generate a Rollup build and so on.
6161
*
62-
* @param config user-supplied Stencil configuration
62+
* @param config a validated Stencil configuration object
6363
* @param buildCtx the current build context
6464
* @param compilerCtx the current compiler context
6565
* @param outputTarget the outputTarget we're currently dealing with
6666
* @returns bundle options suitable for generating a rollup configuration
6767
*/
6868
export const getBundleOptions = (
69-
config: d.Config,
69+
config: d.ValidatedConfig,
7070
buildCtx: d.BuildCtx,
7171
compilerCtx: d.CompilerCtx,
7272
outputTarget: d.OutputTargetDistCustomElements
@@ -96,15 +96,15 @@ export const getBundleOptions = (
9696
/**
9797
* Get bundle options for rollup, run the rollup build, optionally minify the
9898
* output, and write files to disk.
99-
* @param config user-supplied Stencil configuration
100-
* @param buildCtx the current build context
99+
*
100+
* @param config the validated Stencil configuration we're using
101101
* @param compilerCtx the current compiler context
102+
* @param buildCtx the current build context
102103
* @param outputTarget the outputTarget we're currently dealing with
103104
* @returns an empty promise
104105
*/
105-
106106
export const bundleCustomElements = async (
107-
config: d.Config,
107+
config: d.ValidatedConfig,
108108
compilerCtx: d.CompilerCtx,
109109
buildCtx: d.BuildCtx,
110110
outputTarget: d.OutputTargetDistCustomElements

src/compiler/output-targets/output-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { isOutputTargetDistTypes } from './output-utils';
99
* @param buildCtx the context associated with the current build
1010
*/
1111
export const outputTypes = async (
12-
config: d.Config,
12+
config: d.ValidatedConfig,
1313
compilerCtx: d.CompilerCtx,
1414
buildCtx: d.BuildCtx
1515
): Promise<void> => {

src/compiler/output-targets/test/custom-elements-types.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { path } from '@stencil/core/compiler';
2-
import { mockConfig, mockCompilerSystem, mockBuildCtx, mockCompilerCtx, mockModule } from '@stencil/core/testing';
2+
import {
3+
mockCompilerSystem,
4+
mockBuildCtx,
5+
mockCompilerCtx,
6+
mockModule,
7+
mockValidatedConfig,
8+
} from '@stencil/core/testing';
39
import type * as d from '../../../declarations';
410
import * as outputCustomElementsMod from '../dist-custom-elements';
511
import { stubComponentCompilerMeta } from '../../types/tests/ComponentCompilerMeta.stub';
@@ -9,7 +15,7 @@ import { join, relative } from 'path';
915

1016
const setup = () => {
1117
const sys = mockCompilerSystem();
12-
const config: d.Config = mockConfig(sys);
18+
const config: d.ValidatedConfig = mockValidatedConfig(sys);
1319
const compilerCtx = mockCompilerCtx(config);
1420
const buildCtx = mockBuildCtx(config, compilerCtx);
1521
const root = config.rootDir;

src/compiler/output-targets/test/output-targets-dist-custom-elements.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { path } from '@stencil/core/compiler';
2-
import { mockConfig, mockCompilerSystem, mockBuildCtx, mockCompilerCtx, mockModule } from '@stencil/core/testing';
2+
import {
3+
mockCompilerSystem,
4+
mockBuildCtx,
5+
mockCompilerCtx,
6+
mockModule,
7+
mockValidatedConfig,
8+
} from '@stencil/core/testing';
39
import type * as d from '../../../declarations';
410
import {
511
addCustomElementInputs,
@@ -16,7 +22,7 @@ import { DIST_CUSTOM_ELEMENTS, DIST_CUSTOM_ELEMENTS_BUNDLE } from '../output-uti
1622

1723
const setup = () => {
1824
const sys = mockCompilerSystem();
19-
const config: d.Config = mockConfig(sys);
25+
const config: d.ValidatedConfig = mockValidatedConfig(sys);
2026
const compilerCtx = mockCompilerCtx(config);
2127
const buildCtx = mockBuildCtx(config, compilerCtx);
2228
const root = config.rootDir;

src/compiler/sys/config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { createConfigFlags } from '../../cli/config-flags';
66

77
export const getConfig = (userConfig: d.Config): d.ValidatedConfig => {
88
const logger = userConfig.logger ?? createLogger();
9-
const config: d.ValidatedConfig = { ...userConfig, flags: createConfigFlags(userConfig.flags ?? {}), logger };
9+
const config: d.ValidatedConfig = {
10+
...userConfig,
11+
flags: createConfigFlags(userConfig.flags ?? {}),
12+
logger,
13+
outputTargets: userConfig.outputTargets ?? [],
14+
};
1015

1116
if (!config.sys) {
1217
config.sys = createSystem({ logger: config.logger });

src/compiler/types/generate-types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { isDtsFile } from '@utils';
1414
* @param outputTarget the output target to generate types for
1515
*/
1616
export const generateTypes = async (
17-
config: d.Config,
17+
config: d.ValidatedConfig,
1818
compilerCtx: d.CompilerCtx,
1919
buildCtx: d.BuildCtx,
2020
outputTarget: d.OutputTargetDistTypes
@@ -33,7 +33,7 @@ export const generateTypes = async (
3333
* @param outputTarget the output target to generate types for
3434
*/
3535
const generateTypesOutput = async (
36-
config: d.Config,
36+
config: d.ValidatedConfig,
3737
compilerCtx: d.CompilerCtx,
3838
buildCtx: d.BuildCtx,
3939
outputTarget: d.OutputTargetDistTypes

0 commit comments

Comments
 (0)