Skip to content

Commit

Permalink
Use strict types with PluginDriver (#3426)
Browse files Browse the repository at this point in the history
* Use strict types with PluginDriver

* Fix type errors

Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
  • Loading branch information
NotWoods and lukastaegert committed Mar 10, 2020
1 parent ddc267e commit 77dbe1f
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 80 deletions.
4 changes: 2 additions & 2 deletions src/ast/nodes/MetaProperty.ts
Expand Up @@ -106,7 +106,7 @@ export default class MetaProperty extends NodeBase {
]);
}
if (!replacement) {
replacement = outputPluginDriver.hookFirstSync<'resolveFileUrl', string>('resolveFileUrl', [
replacement = outputPluginDriver.hookFirstSync<'resolveFileUrl'>('resolveFileUrl', [
{
assetReferenceId,
chunkId,
Expand All @@ -117,7 +117,7 @@ export default class MetaProperty extends NodeBase {
referenceId: referenceId || assetReferenceId || chunkReferenceId!,
relativePath
}
]);
]) as string;
}

code.overwrite(
Expand Down
4 changes: 2 additions & 2 deletions src/rollup/rollup.ts
Expand Up @@ -362,8 +362,8 @@ function normalizeOutputOptions(
const outputOptions = parseOutputOptions(
outputPluginDriver.hookReduceArg0Sync(
'outputOptions',
[rawOutputOptions.output || inputOptions.output || rawOutputOptions],
(outputOptions: OutputOptions, result: OutputOptions) => result || outputOptions,
[rawOutputOptions.output || inputOptions.output || rawOutputOptions] as [OutputOptions],
(outputOptions, result) => result || outputOptions,
pluginContext => {
const emitError = () => pluginContext.error(errCannotEmitFromOptionsHook());
return {
Expand Down
48 changes: 47 additions & 1 deletion src/rollup/types.d.ts
Expand Up @@ -288,7 +288,8 @@ export type ResolveFileUrlHook = (
}
) => string | null | undefined;

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

/**
* use this type for plugin annotation
Expand Down Expand Up @@ -352,6 +353,51 @@ export interface PluginHooks extends OutputPluginHooks {
watchChange: (id: string) => void;
}

export type AsyncPluginHooks =
| 'buildEnd'
| 'buildStart'
| 'generateBundle'
| 'load'
| 'renderChunk'
| 'renderError'
| 'renderStart'
| 'resolveDynamicImport'
| 'resolveId'
| 'transform'
| 'writeBundle';

export type PluginValueHooks = 'banner' | 'footer' | 'intro' | 'outro';

export type SyncPluginHooks = Exclude<keyof PluginHooks, AsyncPluginHooks>;

export type FirstPluginHooks =
| 'load'
| 'resolveAssetUrl'
| 'resolveDynamicImport'
| 'resolveFileUrl'
| 'resolveId'
| 'resolveImportMeta';

export type SequentialPluginHooks =
| 'augmentChunkHash'
| 'generateBundle'
| 'options'
| 'outputOptions'
| 'renderChunk'
| 'transform'
| 'watchChange';

export type ParallelPluginHooks =
| 'banner'
| 'buildEnd'
| 'buildStart'
| 'footer'
| 'intro'
| 'outro'
| 'renderError'
| 'renderStart'
| 'writeBundle';

interface OutputPluginValueHooks {
banner: AddonHook;
cacheKey: string;
Expand Down

0 comments on commit 77dbe1f

Please sign in to comment.