diff --git a/src/Chunk.ts b/src/Chunk.ts index a9b0f195ea9..bcf96ebb293 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -725,9 +725,7 @@ export default class Chunk { const map = module.getExportNamesByVariable(); for (const exportedVariable of map.keys()) { const isSynthetic = exportedVariable instanceof SyntheticNamedExportVariable; - const importedVariable = isSynthetic - ? (exportedVariable as SyntheticNamedExportVariable).getBaseVariable() - : exportedVariable; + const importedVariable = isSynthetic ? exportedVariable.getBaseVariable() : exportedVariable; if (!(importedVariable instanceof NamespaceVariable && this.outputOptions.preserveModules)) { this.checkCircularDependencyImport(importedVariable, module); const exportingModule = importedVariable.module; diff --git a/src/Graph.ts b/src/Graph.ts index 8feb0765a5c..db02abed7cd 100644 --- a/src/Graph.ts +++ b/src/Graph.ts @@ -91,7 +91,7 @@ export default class Graph { watcher.onCurrentRun('close', handleClose); } this.pluginDriver = new PluginDriver(this, options, options.plugins, this.pluginCache); - this.acornParser = acorn.Parser.extend(...(options.acornInjectPlugins as any)); + this.acornParser = acorn.Parser.extend(...(options.acornInjectPlugins as any[])); this.moduleLoader = new ModuleLoader(this, this.modulesById, this.options, this.pluginDriver); this.fileOperationQueue = new Queue(options.maxParallelFileOps); } diff --git a/src/ModuleLoader.ts b/src/ModuleLoader.ts index 78cd2a443e3..1c6f36db70d 100644 --- a/src/ModuleLoader.ts +++ b/src/ModuleLoader.ts @@ -58,9 +58,9 @@ type NormalizedResolveIdWithoutDefaults = Partial> & id: string; }; -type ResolveStaticDependencyPromise = Promise<[source: string, resolvedId: ResolvedId]>; +type ResolveStaticDependencyPromise = Promise; type ResolveDynamicDependencyPromise = Promise< - [dynamicImport: DynamicImport, resolvedId: ResolvedId | string | null] + readonly [dynamicImport: DynamicImport, resolvedId: ResolvedId | string | null] >; type LoadModulePromise = Promise< [ @@ -548,7 +548,7 @@ export class ModuleLoader { if (resolvedId && typeof resolvedId === 'object') { dynamicImport.id = resolvedId.id; } - return [dynamicImport, resolvedId] as [DynamicImport, ResolvedId | string | null]; + return [dynamicImport, resolvedId] as const; }); } @@ -567,7 +567,7 @@ export class ModuleLoader { module.id, assertions )) - ] as [string, ResolvedId] + ] as const ); } diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index 0ebdc347db9..70805021277 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -1,9 +1,14 @@ export const VERSION: string; -type FalsyValue = false | null | undefined; +// utils +type NullValue = null | undefined | void; type MaybeArray = T | T[]; type MaybePromise = T | Promise; +type PartialNull = { + [P in keyof T]: T[P] | null; +}; + export interface RollupError extends RollupLog { name?: string; stack?: string; @@ -82,10 +87,6 @@ export interface SourceMap { export type SourceMapInput = ExistingRawSourceMap | string | null | { mappings: '' }; -type PartialNull = { - [P in keyof T]: T[P] | null; -}; - interface ModuleOptions { assertions: Record; meta: CustomPluginOptions; @@ -224,7 +225,7 @@ interface PartialResolvedId extends Partial> { id: string; } -export type ResolveIdResult = string | false | null | void | PartialResolvedId; +export type ResolveIdResult = string | NullValue | false | PartialResolvedId; export type ResolveIdHook = ( this: PluginContext, @@ -252,11 +253,11 @@ export type IsExternal = ( isResolved: boolean ) => boolean; -export type IsPureModule = (id: string) => boolean | null | void; +export type IsPureModule = (id: string) => boolean | NullValue; export type HasModuleSideEffects = (id: string, external: boolean) => boolean; -export type LoadResult = SourceDescription | string | null | void; +export type LoadResult = SourceDescription | string | NullValue; export type LoadHook = (this: PluginContext, id: string) => LoadResult; @@ -264,7 +265,7 @@ export interface TransformPluginContext extends PluginContext { getCombinedSourcemap: () => SourceMap; } -export type TransformResult = string | null | void | Partial; +export type TransformResult = string | NullValue | Partial; export type TransformHook = ( this: TransformPluginContext, @@ -280,7 +281,7 @@ export type RenderChunkHook = ( chunk: RenderedChunk, options: NormalizedOutputOptions, meta: { chunks: Record } -) => { code: string; map?: SourceMapInput } | string | null | undefined; +) => { code: string; map?: SourceMapInput } | string | NullValue; export type ResolveDynamicImportHook = ( this: PluginContext, @@ -293,7 +294,7 @@ export type ResolveImportMetaHook = ( this: PluginContext, property: string | null, options: { chunkId: string; format: InternalModuleFormat; moduleId: string } -) => string | null | void; +) => string | NullValue; export type ResolveFileUrlHook = ( this: PluginContext, @@ -305,7 +306,7 @@ export type ResolveFileUrlHook = ( referenceId: string; relativePath: string; } -) => string | null | void; +) => string | NullValue; export type AddonHookFunction = ( this: PluginContext, @@ -351,8 +352,8 @@ export interface FunctionPluginHooks { ) => void; load: LoadHook; moduleParsed: ModuleParsedHook; - options: (this: MinimalPluginContext, options: InputOptions) => InputOptions | null | void; - outputOptions: (this: PluginContext, options: OutputOptions) => OutputOptions | null | void; + options: (this: MinimalPluginContext, options: InputOptions) => InputOptions | NullValue; + outputOptions: (this: PluginContext, options: OutputOptions) => OutputOptions | NullValue; renderChunk: RenderChunkHook; renderDynamicImport: ( this: PluginContext, @@ -362,7 +363,7 @@ export interface FunctionPluginHooks { moduleId: string; targetModuleId: string | null; } - ) => { left: string; right: string } | null | void; + ) => { left: string; right: string } | NullValue; renderError: (this: PluginContext, error?: Error) => void; renderStart: ( this: PluginContext, @@ -481,13 +482,13 @@ interface ManualChunkMeta { getModuleIds: () => IterableIterator; getModuleInfo: GetModuleInfo; } -export type GetManualChunk = (id: string, meta: ManualChunkMeta) => string | null | void; +export type GetManualChunk = (id: string, meta: ManualChunkMeta) => string | NullValue; export type ExternalOption = | (string | RegExp)[] | string | RegExp - | ((source: string, importer: string | undefined, isResolved: boolean) => boolean | null | void); + | ((source: string, importer: string | undefined, isResolved: boolean) => boolean | NullValue); export type PureModulesOption = boolean | string[] | IsPureModule; export type GlobalsOption = { [name: string]: string } | ((name: string) => string); export type InputOption = string | string[] | { [entryAlias: string]: string }; @@ -499,7 +500,7 @@ export type SourcemapPathTransformOption = ( sourcemapPath: string ) => string; -export type InputPluginOption = MaybePromise; +export type InputPluginOption = MaybePromise; export interface InputOptions { acorn?: Record; @@ -517,7 +518,7 @@ export interface InputOptions { maxParallelFileOps?: number; /** @deprecated Use the "maxParallelFileOps" option instead. */ maxParallelFileReads?: number; - moduleContext?: ((id: string) => string | null | void) | { [id: string]: string }; + moduleContext?: ((id: string) => string | NullValue) | { [id: string]: string }; onwarn?: WarningHandlerWithDefault; perf?: boolean; plugins?: InputPluginOption; @@ -623,7 +624,7 @@ export type NormalizedAmdOptions = ( type AddonFunction = (chunk: RenderedChunk) => string | Promise; -type OutputPluginOption = MaybePromise; +type OutputPluginOption = MaybePromise; export interface OutputOptions { amd?: AmdOptions; diff --git a/src/utils/options/mergeOptions.ts b/src/utils/options/mergeOptions.ts index 5071374422d..be429cfb598 100644 --- a/src/utils/options/mergeOptions.ts +++ b/src/utils/options/mergeOptions.ts @@ -5,8 +5,7 @@ import type { OutputOptions, RollupCache, RollupOptions, - WarningHandler, - WarningHandlerWithDefault + WarningHandler } from '../../rollup/types'; import { ensureArray } from '../ensureArray'; import type { CommandConfigObject } from './normalizeInputOptions'; @@ -159,7 +158,7 @@ async function mergeInputOptions( } const getExternal = (config: InputOptions, overrides: CommandConfigObject): ExternalOption => { - const configExternal = config.external as ExternalOption | undefined; + const configExternal = config.external; return typeof configExternal === 'function' ? (source: string, importer: string | undefined, isResolved: boolean) => configExternal(source, importer, isResolved) || overrides.external.includes(source) @@ -167,9 +166,7 @@ const getExternal = (config: InputOptions, overrides: CommandConfigObject): Exte }; const getOnWarn = (config: InputOptions, defaultOnWarnHandler: WarningHandler): WarningHandler => - config.onwarn - ? warning => (config.onwarn as WarningHandlerWithDefault)(warning, defaultOnWarnHandler) - : defaultOnWarnHandler; + config.onwarn ? warning => config.onwarn!(warning, defaultOnWarnHandler) : defaultOnWarnHandler; const getObjectOption = ( config: T, diff --git a/src/utils/options/normalizeInputOptions.ts b/src/utils/options/normalizeInputOptions.ts index d28675db19d..52b941b3812 100644 --- a/src/utils/options/normalizeInputOptions.ts +++ b/src/utils/options/normalizeInputOptions.ts @@ -186,7 +186,7 @@ const getmaxParallelFileOps = ( warn: WarningHandler, strictDeprecations: boolean ): NormalizedInputOptions['maxParallelFileOps'] => { - const maxParallelFileReads = config.maxParallelFileReads as unknown; + const maxParallelFileReads = config.maxParallelFileReads; if (typeof maxParallelFileReads === 'number') { warnDeprecationWithOptions( 'The "maxParallelFileReads" option is deprecated. Use the "maxParallelFileOps" option instead.', @@ -195,7 +195,7 @@ const getmaxParallelFileOps = ( strictDeprecations ); } - const maxParallelFileOps = (config.maxParallelFileOps as unknown) ?? maxParallelFileReads; + const maxParallelFileOps = config.maxParallelFileOps ?? maxParallelFileReads; if (typeof maxParallelFileOps === 'number') { if (maxParallelFileOps <= 0) return Infinity; return maxParallelFileOps; diff --git a/src/utils/options/normalizeOutputOptions.ts b/src/utils/options/normalizeOutputOptions.ts index 3e0797e9908..e03e92eba9b 100644 --- a/src/utils/options/normalizeOutputOptions.ts +++ b/src/utils/options/normalizeOutputOptions.ts @@ -287,7 +287,7 @@ const getAddon = ( if (typeof configAddon === 'function') { return configAddon as NormalizedOutputOptions[T]; } - return () => (configAddon as string) || ''; + return () => configAddon || ''; }; // eslint-disable-next-line unicorn/prevent-abbreviations diff --git a/src/utils/renderChunks.ts b/src/utils/renderChunks.ts index dae3bc5f38b..74bbf60c16e 100644 --- a/src/utils/renderChunks.ts +++ b/src/utils/renderChunks.ts @@ -161,10 +161,7 @@ async function transformChunk( map.sources = map.sources .map(sourcePath => { if (sourcemapPathTransform) { - const newSourcePath = sourcemapPathTransform( - sourcePath, - `${resultingFile}.map` - ) as unknown; + const newSourcePath = sourcemapPathTransform(sourcePath, `${resultingFile}.map`); if (typeof newSourcePath !== 'string') { error(errorFailedValidation(`sourcemapPathTransform function must return a string.`));