Transform method for plugins has correct 'this' context

See link for more info: https://github.com/rollup/rollup/wiki/Plugins#warnings-and-errors
This commit is contained in:
Christian Svensson
2017-11-16 02:46:01 +01:00
parent 139e3769dc
commit 00fa22a35e
2 changed files with 22 additions and 2 deletions
+9 -1
View File
@@ -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<Bundle>
+13 -1
View File
@@ -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({