diff --git a/src/Chunk.ts b/src/Chunk.ts index 3eed0478a7d..738e6ebf45c 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -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); @@ -202,8 +202,6 @@ export default class Chunk { getAliasName(moduleForNaming.id) ) ); - } else { - this.variableName = '__chunk_' + ++graph.curChunkIndex; } } diff --git a/src/Graph.ts b/src/Graph.ts index ecb9141e0d7..871994d972c 100644 --- a/src/Graph.ts +++ b/src/Graph.ts @@ -66,7 +66,6 @@ export default class Graph { acornParser: typeof acorn.Parser; cachedModules: Map; contextParse: (code: string, acornOptions?: acorn.Options) => ESTree.Program; - curChunkIndex = 0; deoptimizationTracker: EntityPathTracker; getModuleContext: (id: string) => string; moduleById = new Map(); @@ -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) { diff --git a/src/utils/error.ts b/src/utils/error.ts index ca4d67afcd5..7010eb883c2 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -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' diff --git a/src/utils/pluginDriver.ts b/src/utils/pluginDriver.ts index 7f7de0302b8..cf6411c53cc 100644 --- a/src/utils/pluginDriver.ts +++ b/src/utils/pluginDriver.ts @@ -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 extends (...args: infer K) => any ? K : never; @@ -76,7 +77,7 @@ export interface PluginDriver { } export type Reduce = (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 '; @@ -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, @@ -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, @@ -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( @@ -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 = { diff --git a/src/utils/transform.ts b/src/utils/transform.ts index ac04c23c08e..2e6d6f382bb 100644 --- a/src/utils/transform.ts +++ b/src/utils/transform.ts @@ -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, @@ -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 { @@ -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 { @@ -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; diff --git a/test/function/samples/deprecations/transform-dependencies/_config.js b/test/function/samples/deprecations/transform-dependencies/_config.js index 84f836df2fc..00ca6697bac 100644 --- a/test/function/samples/deprecations/transform-dependencies/_config.js +++ b/test/function/samples/deprecations/transform-dependencies/_config.js @@ -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' } };