Skip to content

Commit

Permalink
chore(snc): resolve top level dev server type errors (#4383)
Browse files Browse the repository at this point in the history
this commit fixes 'top level' type errors related to a stencil
configuration's `devServer` config. it adds `devServer` to the
`ValidatedConfig` type, to verify that the field exists on any instances
of `ValidatedConfig`.

this commit does not attempt to set reasonable defaults for subfields of
the `DevServerConfig` type in order to limit the scope/size of the
commit
  • Loading branch information
rwaskiewicz authored May 16, 2023
1 parent 5751672 commit ae23e92
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/cli/task-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { CoreCompiler } from './load-compiler';
import { startupCompilerLog } from './logs';

export const taskDocs = async (coreCompiler: CoreCompiler, config: ValidatedConfig) => {
config.devServer = null;
config.devServer = {};
config.outputTargets = config.outputTargets.filter(isOutputTargetDocs);
config.devMode = true;

Expand Down
5 changes: 3 additions & 2 deletions src/cli/task-watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { CoreCompiler } from './load-compiler';
import { startupCompilerLog } from './logs';

export const taskWatch = async (coreCompiler: CoreCompiler, config: ValidatedConfig) => {
let devServer: DevServer = null;
let devServer: DevServer | null = null;
let exitCode = 0;

try {
Expand Down Expand Up @@ -36,7 +36,8 @@ export const taskWatch = async (coreCompiler: CoreCompiler, config: ValidatedCon
const rmDevServerLog = watcher.on('buildFinish', () => {
// log the dev server url one time
rmDevServerLog();
config.logger.info(`${config.logger.cyan(devServer.browserUrl)}\n`);
const url = devServer?.browserUrl ?? 'UNKNOWN URL';
config.logger.info(`${config.logger.cyan(url)}\n`);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/build/build-hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type * as d from '../../declarations';
import { getScopeId } from '../style/scope-css';

export const generateHmr = (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) => {
if (config.devServer == null || config.devServer.reloadStrategy == null) {
if (config.devServer?.reloadStrategy == null) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/bundle/bundle-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const getRollupOptions = (
}
return resolved;
});
if (config.devServer && config.devServer.experimentalDevModules) {
if (config.devServer?.experimentalDevModules) {
nodeResolvePlugin.resolveId = async function (importee: string, importer: string) {
const resolvedId = await orgNodeResolveId2.call(nodeResolvePlugin, importee, importer);
return devNodeModuleResolveId(config, compilerCtx.fs, resolvedId, importee);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/config/test/validate-dev-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe('validateDevServer', () => {

it.each([1, []])('should default historyApiFallback when an invalid value (%s) is provided', (badValue) => {
// this test explicitly checks for a bad value in the stencil.config file, hence the type assertion
(inputConfig.devServer.historyApiFallback as any) = badValue;
inputConfig.devServer = { ...inputDevServerConfig, historyApiFallback: badValue as any };
const { config } = validateConfig(inputConfig, mockLoadConfigInit());
expect(config.devServer.historyApiFallback).toBeDefined();
expect(config.devServer.historyApiFallback.index).toBe('index.html');
Expand Down
1 change: 1 addition & 0 deletions src/compiler/config/validate-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export const validateConfig = (
const logger = bootstrapConfig.logger || config.logger || createLogger();

const validatedConfig: ValidatedConfig = {
devServer: {}, // assign `devServer` before spreading `config`, in the event 'devServer' is not a key on `config`
...config,
// flags _should_ be JSON safe
flags: JSON.parse(JSON.stringify(config.flags || {})),
Expand Down
7 changes: 2 additions & 5 deletions src/compiler/config/validate-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ import { isAbsolute, join } from 'path';

import type * as d from '../../declarations';

export const validateDevServer = (
config: d.ValidatedConfig,
diagnostics: d.Diagnostic[]
): d.DevServerConfig | undefined => {
export const validateDevServer = (config: d.ValidatedConfig, diagnostics: d.Diagnostic[]): d.DevServerConfig => {
if ((config.devServer === null || (config.devServer as any)) === false) {
return undefined;
return {};
}

const { flags } = config;
Expand Down
1 change: 1 addition & 0 deletions src/declarations/stencil-public-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ type RequireFields<T, K extends keyof T> = T & { [P in K]-?: T[P] };
*/
type StrictConfigFields =
| 'cacheDir'
| 'devServer'
| 'flags'
| 'hydratedFlag'
| 'logger'
Expand Down
2 changes: 1 addition & 1 deletion src/dev-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function startServer(

let isActivelyBuilding = false;
let lastBuildResults: CompilerBuildResults = null;
let devServer: DevServer = null;
let devServer: DevServer | null = null;
let removeWatcher: BuildOnEventRemove = null;
let closeResolve: () => void = null;
let hasStarted = false;
Expand Down
1 change: 1 addition & 0 deletions src/testing/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function mockValidatedConfig(overrides: Partial<ValidatedConfig> = {}): V

return {
...baseConfig,
devServer: {},
flags: createConfigFlags(),
hydratedFlag: null,
logger: mockLogger(),
Expand Down

0 comments on commit ae23e92

Please sign in to comment.