Skip to content

Commit

Permalink
refactor: improve types (#4667)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Oct 12, 2022
1 parent 3cb7f13 commit fec5132
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 59 deletions.
43 changes: 13 additions & 30 deletions src/rollup/rollup.ts
Expand Up @@ -15,11 +15,11 @@ import { promises as fs } from '../utils/fs';
import { catchUnfinishedHookActions } from '../utils/hookActions';
import { normalizeInputOptions } from '../utils/options/normalizeInputOptions';
import { normalizeOutputOptions } from '../utils/options/normalizeOutputOptions';
import type { GenericConfigObject } from '../utils/options/options';
import { dirname, resolve } from '../utils/path';
import { ANONYMOUS_OUTPUT_PLUGIN_PREFIX, ANONYMOUS_PLUGIN_PREFIX } from '../utils/pluginUtils';
import { getTimings, initialiseTimers, timeEnd, timeStart } from '../utils/timers';
import type {
InputOptions,
NormalizedInputOptions,
NormalizedOutputOptions,
OutputAsset,
Expand All @@ -33,12 +33,12 @@ import type {
RollupWatcher
} from './types';

export default function rollup(rawInputOptions: GenericConfigObject): Promise<RollupBuild> {
export default function rollup(rawInputOptions: RollupOptions): Promise<RollupBuild> {
return rollupInternal(rawInputOptions, null);
}

export async function rollupInternal(
rawInputOptions: GenericConfigObject,
rawInputOptions: RollupOptions,
watcher: RollupWatcher | null
): Promise<RollupBuild> {
const { options: inputOptions, unsetOptions: unsetInputOptions } = await getInputOptions(
Expand Down Expand Up @@ -89,33 +89,21 @@ export async function rollupInternal(
async generate(rawOutputOptions: OutputOptions) {
if (result.closed) return error(errorAlreadyClosed());

return handleGenerateWrite(
false,
inputOptions,
unsetInputOptions,
rawOutputOptions as GenericConfigObject,
graph
);
return handleGenerateWrite(false, inputOptions, unsetInputOptions, rawOutputOptions, graph);
},
watchFiles: Object.keys(graph.watchFiles),
async write(rawOutputOptions: OutputOptions) {
if (result.closed) return error(errorAlreadyClosed());

return handleGenerateWrite(
true,
inputOptions,
unsetInputOptions,
rawOutputOptions as GenericConfigObject,
graph
);
return handleGenerateWrite(true, inputOptions, unsetInputOptions, rawOutputOptions, graph);
}
};
if (inputOptions.perf) result.getTimings = getTimings;
return result;
}

async function getInputOptions(
rawInputOptions: GenericConfigObject,
rawInputOptions: InputOptions,
watchMode: boolean
): Promise<{ options: NormalizedInputOptions; unsetOptions: Set<string> }> {
if (!rawInputOptions) {
Expand All @@ -133,16 +121,11 @@ async function getInputOptions(
}

function applyOptionHook(watchMode: boolean) {
return async (
inputOptions: Promise<GenericConfigObject>,
plugin: Plugin
): Promise<GenericConfigObject> => {
return async (inputOptions: Promise<RollupOptions>, plugin: Plugin): Promise<InputOptions> => {
const handler = 'handler' in plugin.options! ? plugin.options.handler : plugin.options!;
return (
((await handler.call(
{ meta: { rollupVersion, watchMode } },
await inputOptions
)) as GenericConfigObject) || inputOptions
(await handler.call({ meta: { rollupVersion, watchMode } }, await inputOptions)) ||
inputOptions
);
};
}
Expand All @@ -159,7 +142,7 @@ function handleGenerateWrite(
isWrite: boolean,
inputOptions: NormalizedInputOptions,
unsetInputOptions: ReadonlySet<string>,
rawOutputOptions: GenericConfigObject,
rawOutputOptions: OutputOptions,
graph: Graph
): Promise<RollupOutput> {
const {
Expand Down Expand Up @@ -193,7 +176,7 @@ function handleGenerateWrite(
}

function getOutputOptionsAndPluginDriver(
rawOutputOptions: GenericConfigObject,
rawOutputOptions: OutputOptions,
inputPluginDriver: PluginDriver,
inputOptions: NormalizedInputOptions,
unsetInputOptions: ReadonlySet<string>
Expand All @@ -218,13 +201,13 @@ function getOutputOptionsAndPluginDriver(
function getOutputOptions(
inputOptions: NormalizedInputOptions,
unsetInputOptions: ReadonlySet<string>,
rawOutputOptions: GenericConfigObject,
rawOutputOptions: OutputOptions,
outputPluginDriver: PluginDriver
): { options: NormalizedOutputOptions; unsetOptions: Set<string> } {
return normalizeOutputOptions(
outputPluginDriver.hookReduceArg0Sync(
'outputOptions',
[rawOutputOptions.output || rawOutputOptions] as [OutputOptions],
[rawOutputOptions],
(outputOptions, result) => result || outputOptions,
pluginContext => {
const emitError = () => pluginContext.error(errorCannotEmitFromOptionsHook());
Expand Down
31 changes: 13 additions & 18 deletions src/utils/options/mergeOptions.ts
Expand Up @@ -6,6 +6,7 @@ import type {
OutputOptions,
OutputPluginOption,
RollupCache,
RollupOptions,
WarningHandler,
WarningHandlerWithDefault
} from '../../rollup/types';
Expand Down Expand Up @@ -41,7 +42,7 @@ export const commandAliases: { [key: string]: string } = {
const EMPTY_COMMAND_OPTIONS = { external: [], globals: undefined };

export function mergeOptions(
config: GenericConfigObject,
config: RollupOptions,
rawCommandOptions: GenericConfigObject = EMPTY_COMMAND_OPTIONS,
defaultOnWarnHandler: WarningHandler = defaultOnWarn
): MergedRollupOptions {
Expand All @@ -51,7 +52,7 @@ export function mergeOptions(
if (command.output) {
Object.assign(command, command.output);
}
const outputOptionsArray = ensureArray(config.output) as GenericConfigObject[];
const outputOptionsArray = ensureArray(config.output);
if (outputOptionsArray.length === 0) outputOptionsArray.push({});
const outputOptions = outputOptionsArray.map(singleOutputOptions =>
mergeOutputOptions(singleOutputOptions, command, warn)
Expand Down Expand Up @@ -108,7 +109,7 @@ type CompleteInputOptions<U extends keyof InputOptions> = {
};

function mergeInputOptions(
config: GenericConfigObject,
config: InputOptions,
overrides: CommandConfigObject,
defaultOnWarnHandler: WarningHandler
): InputOptions {
Expand Down Expand Up @@ -157,29 +158,23 @@ function mergeInputOptions(
return inputOptions;
}

const getExternal = (
config: GenericConfigObject,
overrides: CommandConfigObject
): ExternalOption => {
const getExternal = (config: InputOptions, overrides: CommandConfigObject): ExternalOption => {
const configExternal = config.external as ExternalOption | undefined;
return typeof configExternal === 'function'
? (source: string, importer: string | undefined, isResolved: boolean) =>
configExternal(source, importer, isResolved) || overrides.external.includes(source)
: [...ensureArray(configExternal), ...overrides.external];
};

const getOnWarn = (
config: GenericConfigObject,
defaultOnWarnHandler: WarningHandler
): WarningHandler =>
const getOnWarn = (config: InputOptions, defaultOnWarnHandler: WarningHandler): WarningHandler =>
config.onwarn
? warning => (config.onwarn as WarningHandlerWithDefault)(warning, defaultOnWarnHandler)
: defaultOnWarnHandler;

const getObjectOption = (
config: GenericConfigObject,
overrides: GenericConfigObject,
name: string,
const getObjectOption = <T extends object>(
config: T,
overrides: T,
name: keyof T,
objectifyValue = objectifyOption
) => {
const commandOption = normalizeObjectOptionValue(overrides[name], objectifyValue);
Expand All @@ -190,7 +185,7 @@ const getObjectOption = (
return configOption;
};

export const getWatch = (config: GenericConfigObject, overrides: GenericConfigObject) =>
export const getWatch = (config: InputOptions, overrides: InputOptions) =>
config.watch !== false && getObjectOption(config, overrides, 'watch');

export const isWatchEnabled = (optionValue: unknown): boolean => {
Expand Down Expand Up @@ -224,8 +219,8 @@ type CompleteOutputOptions<U extends keyof OutputOptions> = {
};

function mergeOutputOptions(
config: GenericConfigObject,
overrides: GenericConfigObject,
config: OutputOptions,
overrides: OutputOptions,
warn: WarningHandler
): OutputOptions {
const getOption = (name: keyof OutputOptions): any => overrides[name] ?? config[name];
Expand Down
3 changes: 1 addition & 2 deletions src/utils/options/normalizeInputOptions.ts
Expand Up @@ -14,7 +14,6 @@ import { resolve } from '../path';
import relativeId from '../relativeId';
import {
defaultOnWarn,
type GenericConfigObject,
getOptionWithPreset,
normalizePluginOption,
treeshakePresets,
Expand Down Expand Up @@ -65,7 +64,7 @@ export function normalizeInputOptions(config: InputOptions): {
};

warnUnknownOptions(
config as GenericConfigObject,
config,
[...Object.keys(options), 'watch'],
'input options',
options.onwarn,
Expand Down
10 changes: 2 additions & 8 deletions src/utils/options/normalizeOutputOptions.ts
Expand Up @@ -17,7 +17,6 @@ import { sanitizeFileName as defaultSanitizeFileName } from '../sanitizeFileName
import { isValidUrl } from '../url';
import {
generatedCodePresets,
type GenericConfigObject,
getOptionWithPreset,
normalizePluginOption,
warnUnknownOptions
Expand Down Expand Up @@ -95,12 +94,7 @@ export function normalizeOutputOptions(
validate: config.validate || false
};

warnUnknownOptions(
config as GenericConfigObject,
Object.keys(outputOptions),
'output options',
inputOptions.onwarn
);
warnUnknownOptions(config, Object.keys(outputOptions), 'output options', inputOptions.onwarn);
return { options: outputOptions, unsetOptions };
}

Expand Down Expand Up @@ -289,7 +283,7 @@ const getAddon = <T extends 'banner' | 'footer' | 'intro' | 'outro'>(
config: OutputOptions,
name: T
): NormalizedOutputOptions[T] => {
const configAddon = (config as GenericConfigObject)[name];
const configAddon = config[name];
if (typeof configAddon === 'function') {
return configAddon as NormalizedOutputOptions[T];
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/options/options.ts
Expand Up @@ -20,7 +20,7 @@ export interface GenericConfigObject {
export const defaultOnWarn: WarningHandler = warning => console.warn(warning.message || warning);

export function warnUnknownOptions(
passedOptions: GenericConfigObject,
passedOptions: object,
validOptions: readonly string[],
optionType: string,
warn: WarningHandler,
Expand Down

0 comments on commit fec5132

Please sign in to comment.