Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Explicitly allow null as return value for various hooks (#2941)
  • Loading branch information
lukastaegert committed Jun 14, 2019
1 parent 74a3be1 commit dd00ff5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Module.ts
Expand Up @@ -199,7 +199,7 @@ export default class Module {
manualChunkAlias: string = null as any;
moduleSideEffects: boolean;
originalCode!: string;
originalSourcemap!: RawSourceMap | void;
originalSourcemap!: RawSourceMap | null;
reexports: { [name: string]: ReexportDescription } = Object.create(null);
resolvedIds!: ResolvedIdMap;
scope!: ModuleScope;
Expand Down
14 changes: 7 additions & 7 deletions src/ModuleLoader.ts
Expand Up @@ -49,18 +49,19 @@ function normalizeRelativeExternalId(importer: string, source: string) {
}

function getIdMatcher<T extends Array<any>>(
option: boolean | string[] | ((id: string, ...args: T) => boolean | void)
option: boolean | string[] | ((id: string, ...args: T) => boolean | null | undefined)
): (id: string, ...args: T) => boolean {
if (option === true) {
return () => true;
} else if (typeof option === 'function') {
}
if (typeof option === 'function') {
return (id, ...args) => (!id.startsWith('\0') && option(id, ...args)) || false;
} else if (option) {
}
if (option) {
const ids = new Set(Array.isArray(option) ? option : option ? [option] : []);
return (id => ids.has(id)) as (id: string, ...args: T) => boolean;
} else {
return () => false;
}
return () => false;
}

function getHasModuleSideEffects(
Expand Down Expand Up @@ -127,8 +128,7 @@ export class ModuleLoader {
pureExternalModules,
graph
);
this.getManualChunk =
typeof getManualChunk === 'function' ? getManualChunk : () => (null as unknown) as void;
this.getManualChunk = typeof getManualChunk === 'function' ? getManualChunk : () => null;
}

addEntryModuleAndGetReferenceId(unresolvedEntryModule: UnresolvedModuleWithAlias): string {
Expand Down
32 changes: 18 additions & 14 deletions src/rollup/types.d.ts
Expand Up @@ -79,7 +79,7 @@ export interface TransformModuleJSON {
customTransformCache: boolean;
moduleSideEffects: boolean | null;
originalCode: string;
originalSourcemap: RawSourceMap | void;
originalSourcemap: RawSourceMap | null;
resolvedIds?: ResolvedIdMap;
sourcemapChain: (RawSourceMap | { missing: true; plugin: string })[];
transformDependencies: string[] | null;
Expand Down Expand Up @@ -171,17 +171,21 @@ interface PartialResolvedId {
moduleSideEffects?: boolean | null;
}

export type ResolveIdResult = string | false | void | PartialResolvedId;
export type ResolveIdResult = string | false | null | undefined | PartialResolvedId;

export type ResolveIdHook = (
this: PluginContext,
source: string,
importer: string | undefined
) => Promise<ResolveIdResult> | ResolveIdResult;

export type IsExternal = (source: string, importer: string, isResolved: boolean) => boolean | void;
export type IsExternal = (
source: string,
importer: string,
isResolved: boolean
) => boolean | null | undefined;

export type IsPureModule = (id: string) => boolean | void;
export type IsPureModule = (id: string) => boolean | null | undefined;

export type HasModuleSideEffects = (id: string, external: boolean) => boolean;

Expand All @@ -190,7 +194,7 @@ export type LoadHook = (
id: string
) => Promise<SourceDescription | string | null> | SourceDescription | string | null;

export type TransformResult = string | void | TransformSourceDescription;
export type TransformResult = string | null | undefined | TransformSourceDescription;

export type TransformHook = (
this: PluginContext,
Expand All @@ -203,10 +207,10 @@ export type TransformChunkHook = (
code: string,
options: OutputOptions
) =>
| Promise<{ code: string; map: RawSourceMap } | void>
| Promise<{ code: string; map: RawSourceMap } | null | undefined>
| { code: string; map: RawSourceMap }
| void
| null;
| null
| undefined;

export type RenderChunkHook = (
this: PluginContext,
Expand All @@ -229,7 +233,7 @@ export type ResolveImportMetaHook = (
this: PluginContext,
prop: string | null,
options: { chunkId: string; format: string; moduleId: string }
) => string | void;
) => string | null | undefined;

export type ResolveAssetUrlHook = (
this: PluginContext,
Expand All @@ -240,7 +244,7 @@ export type ResolveAssetUrlHook = (
moduleId: string;
relativeAssetPath: string;
}
) => string | void;
) => string | null | undefined;

export type ResolveFileUrlHook = (
this: PluginContext,
Expand All @@ -253,7 +257,7 @@ export type ResolveFileUrlHook = (
moduleId: string;
relativePath: string;
}
) => string | void;
) => string | null | undefined;

export type AddonHook = string | ((this: PluginContext) => string | Promise<string>);

Expand Down Expand Up @@ -303,8 +307,8 @@ export interface PluginHooks {
options: OnWriteOptions,
chunk: OutputChunk
) => void | Promise<void>;
options: (this: MinimalPluginContext, options: InputOptions) => InputOptions | void | null;
outputOptions: (this: PluginContext, options: OutputOptions) => OutputOptions | void | null;
options: (this: MinimalPluginContext, options: InputOptions) => InputOptions | null | undefined;
outputOptions: (this: PluginContext, options: OutputOptions) => OutputOptions | null | undefined;
renderChunk: RenderChunkHook;
renderError: (this: PluginContext, err?: Error) => Promise<void> | void;
renderStart: (this: PluginContext) => Promise<void> | void;
Expand Down Expand Up @@ -341,7 +345,7 @@ export interface TreeshakingOptions {
tryCatchDeoptimization?: boolean;
}

export type GetManualChunk = (id: string) => string | void;
export type GetManualChunk = (id: string) => string | null | undefined;

export type ExternalOption = string[] | IsExternal;
export type PureModulesOption = boolean | string[] | IsPureModule;
Expand Down

0 comments on commit dd00ff5

Please sign in to comment.