Skip to content

Commit

Permalink
Improve plugin error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Aug 2, 2019
1 parent 35ecf88 commit 6d4199e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 45 deletions.
4 changes: 1 addition & 3 deletions src/Chunk.ts
Expand Up @@ -150,8 +150,8 @@ export default class Chunk {
[moduleId: string]: RenderedModule;
};
usedModules: Module[] = undefined as any;
variableName = 'chunk';

variableName: string;
private dependencies: (ExternalModule | Chunk)[] = undefined as any;
private dynamicDependencies: (ExternalModule | Chunk)[] = undefined as any;
private exportNames: { [name: string]: Variable } = Object.create(null);
Expand Down Expand Up @@ -202,8 +202,6 @@ export default class Chunk {
getAliasName(moduleForNaming.id)
)
);
} else {
this.variableName = '__chunk_' + ++graph.curChunkIndex;
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/Graph.ts
Expand Up @@ -66,7 +66,6 @@ export default class Graph {
acornParser: typeof acorn.Parser;
cachedModules: Map<string, ModuleJSON>;
contextParse: (code: string, acornOptions?: acorn.Options) => ESTree.Program;
curChunkIndex = 0;
deoptimizationTracker: EntityPathTracker;
getModuleContext: (id: string) => string;
moduleById = new Map<string, Module | ExternalModule>();
Expand All @@ -90,7 +89,6 @@ export default class Graph {

constructor(options: InputOptions, watcher?: RollupWatcher) {
this.onwarn = (options.onwarn as WarningHandler) || makeOnwarn();
this.curChunkIndex = 0;
this.deoptimizationTracker = new EntityPathTracker();
this.cachedModules = new Map();
if (options.cache) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/error.ts
Expand Up @@ -48,6 +48,7 @@ export enum Errors {
INVALID_PLUGIN_HOOK = 'INVALID_PLUGIN_HOOK',
INVALID_ROLLUP_PHASE = 'INVALID_ROLLUP_PHASE',
NAMESPACE_CONFLICT = 'NAMESPACE_CONFLICT',
PLUGIN_ERROR = 'PLUGIN_ERROR',
UNRESOLVED_ENTRY = 'UNRESOLVED_ENTRY',
UNRESOLVED_IMPORT = 'UNRESOLVED_IMPORT',
VALIDATION_ERROR = 'VALIDATION_ERROR'
Expand Down
52 changes: 26 additions & 26 deletions src/utils/pluginDriver.ts
Expand Up @@ -11,13 +11,14 @@ import {
PluginCache,
PluginContext,
PluginHooks,
RollupError,
RollupWarning,
RollupWatcher,
SerializablePluginCache
} from '../rollup/types';
import { BuildPhase } from './buildPhase';
import { getRollupDefaultPlugin } from './defaultPlugin';
import { errInvalidRollupPhaseForAddWatchFile, error } from './error';
import { errInvalidRollupPhaseForAddWatchFile, error, Errors } from './error';
import { FileEmitter } from './FileEmitter';

type Args<T> = T extends (...args: infer K) => any ? K : never;
Expand Down Expand Up @@ -76,7 +77,7 @@ export interface PluginDriver {
}

export type Reduce<R = any, T = any> = (reduction: T, result: R, plugin: Plugin) => T;
export type HookContext = (context: PluginContext, plugin?: Plugin) => PluginContext;
export type HookContext = (context: PluginContext, plugin: Plugin) => PluginContext;

export const ANONYMOUS_PLUGIN_PREFIX = 'at position ';

Expand Down Expand Up @@ -104,6 +105,26 @@ function warnDeprecatedHooks(plugins: Plugin[], graph: Graph) {
}
}

export function throwPluginError(
err: string | RollupError,
plugin: string,
{ hook, id }: { hook?: string; id?: string } = {}
): never {
if (typeof err === 'string') err = { message: err };
if (err.code && err.code !== Errors.PLUGIN_ERROR) {
err.pluginCode = err.code;
}
err.code = Errors.PLUGIN_ERROR;
err.plugin = plugin;
if (hook) {
err.hook = hook;
}
if (id) {
err.id = id;
}
return error(err);
}

export function createPluginDriver(
graph: Graph,
options: InputOptions,
Expand Down Expand Up @@ -188,11 +209,7 @@ export function createPluginDriver(
),
emitFile: fileEmitter.emitFile,
error(err): never {
if (typeof err === 'string') err = { message: err };
if (err.code) err.pluginCode = err.code;
err.code = 'PLUGIN_ERROR';
err.plugin = plugin.name;
return error(err);
return throwPluginError(err, plugin.name);
},
getAssetFileName: getDeprecatedHookHandler(
fileEmitter.getFileName,
Expand Down Expand Up @@ -320,16 +337,8 @@ export function createPluginDriver(
}
return hook.apply(context, args);
} catch (err) {
if (typeof err === 'string') err = { message: err };
if (err.code !== 'PLUGIN_ERROR') {
if (err.code) err.pluginCode = err.code;
err.code = 'PLUGIN_ERROR';
}
err.plugin = plugin.name;
err.hook = hookName;
error(err);
return throwPluginError(err, plugin.name, { hook: hookName });
}
return undefined as any;
}

function runHook<T>(
Expand Down Expand Up @@ -361,16 +370,7 @@ export function createPluginDriver(
}
return hook.apply(context, args);
})
.catch(err => {
if (typeof err === 'string') err = { message: err };
if (err.code !== 'PLUGIN_ERROR') {
if (err.code) err.pluginCode = err.code;
err.code = 'PLUGIN_ERROR';
}
err.plugin = plugin.name;
err.hook = hookName;
error(err);
});
.catch(err => throwPluginError(err, plugin.name, { hook: hookName }));
}

const pluginDriver: PluginDriver = {
Expand Down
19 changes: 5 additions & 14 deletions src/utils/transform.ts
Expand Up @@ -15,9 +15,9 @@ import {
} from '../rollup/types';
import { collapseSourcemap } from './collapseSourcemaps';
import { decodedSourcemap } from './decodedSourcemap';
import { augmentCodeLocation, error } from './error';
import { augmentCodeLocation } from './error';
import { dirname, resolve } from './path';
import { trackPluginCache } from './pluginDriver';
import { throwPluginError, trackPluginCache } from './pluginDriver';

export default function transform(
graph: Graph,
Expand Down Expand Up @@ -49,8 +49,7 @@ export default function transform(
if (customTransformCache) {
if (result && typeof result === 'object' && Array.isArray(result.dependencies)) {
for (const dep of result.dependencies) {
const depId = resolve(dirname(id), dep);
if (!graph.watchFiles[depId]) graph.watchFiles[depId] = true;
graph.watchFiles[resolve(dirname(id), dep)] = true;
}
}
} else {
Expand Down Expand Up @@ -105,7 +104,7 @@ export default function transform(
[curSource, id],
transformReducer,
(pluginContext, plugin) => {
curPlugin = plugin as Plugin;
curPlugin = plugin;
if (curPlugin.cacheKey) customTransformCache = true;
else trackedPluginCache = trackPluginCache(pluginContext.cache);
return {
Expand Down Expand Up @@ -181,15 +180,7 @@ export default function transform(
};
}
)
.catch(err => {
if (typeof err === 'string') err = { message: err };
if (err.code !== 'PLUGIN_ERROR') {
if (err.code) err.pluginCode = err.code;
err.code = 'PLUGIN_ERROR';
}
err.id = id;
error(err);
})
.catch(err => throwPluginError(err, curPlugin.name, { hook: 'transform', id }))
.then(code => {
if (!customTransformCache && setAssetSourceErr) throw setAssetSourceErr;

Expand Down
Expand Up @@ -11,9 +11,11 @@ module.exports = {
},
error: {
code: 'PLUGIN_ERROR',
hook: 'transform',
id: path.resolve(__dirname, 'main.js'),
message:
'Returning "dependencies" from the "transform" hook as done by plugin at position 1 is deprecated. The "this.addWatchFile" plugin context function should be used instead.',
plugin: 'at position 1',
pluginCode: 'DEPRECATED_FEATURE'
}
};

0 comments on commit 6d4199e

Please sign in to comment.