Skip to content

Commit

Permalink
update snowpack user types (#1577)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott authored Nov 12, 2020
1 parent ba5ed56 commit 1f50061
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
19 changes: 12 additions & 7 deletions snowpack/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
ProxyOptions,
SnowpackConfig,
SnowpackPlugin,
SnowpackUserConfig,
} from './types/snowpack';
import {
addLeadingSlash,
Expand All @@ -38,7 +39,7 @@ const CONFIG_NAME = 'snowpack';
const ALWAYS_EXCLUDE = ['**/node_modules/**/*', '**/web_modules/**/*', '**/.types/**/*'];

// default settings
const DEFAULT_CONFIG: Partial<SnowpackConfig> = {
const DEFAULT_CONFIG: SnowpackUserConfig = {
plugins: [],
alias: {},
scripts: {},
Expand Down Expand Up @@ -596,8 +597,12 @@ function normalizeAlias(config: SnowpackConfig, cwd: string, createMountAlias: b
}

/** resolve --dest relative to cwd, etc. */
function normalizeConfig(config: SnowpackConfig): SnowpackConfig {
function normalizeConfig(_config: SnowpackUserConfig): SnowpackConfig {
const cwd = process.cwd();
// TODO: This function is really fighting with TypeScript. Now that we have an accurate
// SnowpackUserConfig type, we can have this function construct a fresh config object
// from scratch instead of trying to modify the user's config object in-place.
let config: SnowpackConfig = _config as any as SnowpackConfig;
config.knownEntrypoints = (config as any).install || [];
// @ts-ignore
if (config.devOptions.out) {
Expand Down Expand Up @@ -842,7 +847,7 @@ export function validatePluginLoadResult(
}

export function createConfiguration(
config: Partial<SnowpackConfig>,
config: SnowpackUserConfig,
): [ValidatorResult['errors'], undefined] | [null, SnowpackConfig] {
const {errors: validationErrors} = validate(config, configSchema, {
propertyName: CONFIG_NAME,
Expand All @@ -851,7 +856,7 @@ export function createConfiguration(
if (validationErrors.length > 0) {
return [validationErrors, undefined];
}
const mergedConfig = merge<SnowpackConfig>([DEFAULT_CONFIG, config], {
const mergedConfig = merge<SnowpackUserConfig>([DEFAULT_CONFIG, config], {
isMergeableObject: isPlainObject,
});
return [null, normalizeConfig(mergedConfig)];
Expand Down Expand Up @@ -912,11 +917,11 @@ export function loadAndValidateConfig(flags: CLIFlags, pkgManifest: any): Snowpa
}

// validate against schema; throw helpful user if invalid
const config: SnowpackConfig = result.config;
const config: SnowpackUserConfig = result.config;
validateConfigAgainstV1(config, flags);
const cliConfig = expandCliFlags(flags);

let extendConfig: SnowpackConfig = {} as SnowpackConfig;
let extendConfig: SnowpackUserConfig = {} as SnowpackUserConfig;
if (config.extends) {
const extendConfigLoc = config.extends.startsWith('.')
? path.resolve(path.dirname(result.filepath), config.extends)
Expand Down Expand Up @@ -951,7 +956,7 @@ export function loadAndValidateConfig(flags: CLIFlags, pkgManifest: any): Snowpa
}
}
// if valid, apply config over defaults
const mergedConfig = merge<SnowpackConfig>(
const mergedConfig = merge<SnowpackUserConfig>(
[
pkgManifest.homepage ? {buildOptions: {baseUrl: pkgManifest.homepage}} : {},
extendConfig,
Expand Down
17 changes: 16 additions & 1 deletion snowpack/src/types/snowpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,22 @@ export interface SnowpackConfig {
_extensionMap: Record<string, string>;
}

export type SnowpackUserConfig = DeepPartial<SnowpackConfig>;
export type SnowpackUserConfig = {
install?: string[];
extends?: string;
exclude?: string[];
proxy?: Record<string, string | ProxyOptions>;
mount?: Record<string, string | Partial<MountEntry>>;
alias?: Record<string, string>;
/** @deprecated */
scripts?: Record<string, string>;
plugins?: (string | [string, any])[];
devOptions?: Partial<SnowpackConfig['devOptions']>;
installOptions?: Partial<SnowpackConfig['installOptions']>;
buildOptions?: Partial<SnowpackConfig['buildOptions']>;
testOptions?: Partial<SnowpackConfig['testOptions']>;
experiments?: Partial<SnowpackConfig['experiments']>;
}

export interface CLIFlags extends Omit<InstallOptions, 'env'> {
help?: boolean; // display help text
Expand Down

1 comment on commit 1f50061

@vercel
Copy link

@vercel vercel bot commented on 1f50061 Nov 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.