diff --git a/types/rollup/index.d.ts b/types/rollup/index.d.ts index f4afd2c804..6e980fde6e 100644 --- a/types/rollup/index.d.ts +++ b/types/rollup/index.d.ts @@ -133,7 +133,7 @@ export interface Plugin { */ resolveId?(importee: string, importer: string): string | null | undefined | false | 0 | '' /** A module transformer function */ - transform?(source: string, id: string): string | { code: string, map: SourceMap } + transform?(this: TransformContext, source: string, id: string): string | { code: string, map: SourceMap } /** A bundle transformer function */ transformBundle?(source: string, options: { format: Format }): string | { code: string, map: SourceMap } /** Function hook called when bundle.generate() is being executed. */ @@ -150,5 +150,13 @@ export interface Plugin { footer?: string | (() => string) } +// See https://github.com/rollup/rollup/wiki/Plugins#warnings-and-errors +export interface TransformContext { + /** Emit warnings to Rollup which will be logged during bundling */ + warn(message: string | { message: string }, pos?: number | { line: number, column: number }): void; + /** Emit an error, which will abort the bundling process */ + error(message: string | { message: string }, pos?: number | { line: number, column: number }): void; +} + /** Returns a Promise that resolves with a bundle */ export function rollup(options: Options): Promise diff --git a/types/rollup/rollup-tests.ts b/types/rollup/rollup-tests.ts index b3306ef7f3..90c9569d11 100644 --- a/types/rollup/rollup-tests.ts +++ b/types/rollup/rollup-tests.ts @@ -3,7 +3,19 @@ import { rollup, Bundle, Plugin } from 'rollup' let console: any let cache: Bundle | undefined -let plugin: Plugin +let plugin: Plugin = { + name: 'test-plugin', + transform(source, id) { + if (id === 'rxjs') { + this.error(new Error(`Don't import this directly`)) + } + let indexOfQuote = source.indexOf('"') + if (indexOfQuote >= 0) { + this.warn(`Prefer ' over " for strings`, indexOfQuote) + } + return source + } +} async function main() { const bundle = await rollup({