Skip to content

Commit

Permalink
Expose Graph information for pluginContext calls (#2565)
Browse files Browse the repository at this point in the history
* Expose graph module information for plugins.

* Refactor plugin module information and add test

* Add test that early access to moduleIds and module information is handled gracefully
  • Loading branch information
samccone authored and lukastaegert committed Dec 16, 2018
1 parent 7924d35 commit 1ef9f6b
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/rollup/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ export interface PluginContext {
getAssetFileName: (assetId: string) => string;
warn(warning: RollupWarning | string, pos?: { line: number; column: number }): void;
error(err: RollupError | string, pos?: { line: number; column: number }): void;
moduleIds: IterableIterator<string>;
getModuleInfo: (
moduleId: string
) => {
id: string;
isExternal: boolean;
importedIds: string[];
};
}

export interface PluginContextMeta {
Expand Down
16 changes: 16 additions & 0 deletions src/utils/pluginDriver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { version as rollupVersion } from 'package.json';
import Graph from '../Graph';
import Module from '../Module';
import {
InputOptions,
Plugin,
Expand Down Expand Up @@ -114,6 +115,21 @@ export function createPluginDriver(
warning.plugin = plugin.name || '(anonymous plugin)';
graph.warn(warning);
},
moduleIds: graph.moduleById.keys(),
getModuleInfo: (moduleId: string) => {
const foundModule = graph.moduleById.get(moduleId);
if (foundModule == null) {
throw new Error(`Unable to find module ${moduleId}`);
}

return {
id: foundModule.id,
isExternal: !!foundModule.isExternal,
importedIds: foundModule.isExternal
? []
: (foundModule as Module).sources.map(id => (foundModule as Module).resolvedIds[id])
};
},
watcher
};
return context;
Expand Down
6 changes: 4 additions & 2 deletions test/form/samples/transform-bundle-plugin-options/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ module.exports = {
plugins: [
{
transformBundle(code, options) {
console.log(Object.keys(options));
assert.strictEqual(Object.keys(options).join(', '), require('../../../misc/optionList').output);
assert.strictEqual(
Object.keys(options).join(', '),
require('../../../misc/optionList').output
);
return options.format;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const assert = require('assert');
const path = require('path');

const ID_MAIN = path.join(__dirname, 'main.js');

module.exports = {
description: 'handles accessing module information via plugins early in a graceful way',
options: {
external: ['path'],
plugins: [
{
buildStart() {
assert.deepEqual(Array.from(this.moduleIds), []);
// should throw "not found" error
this.getModuleInfo(ID_MAIN);
}
}
]
},
error: {
code: 'PLUGIN_ERROR',
hook: 'buildStart',
message: `Unable to find module ${ID_MAIN}`,
plugin: 'Plugin at pos 0'
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 42;
47 changes: 47 additions & 0 deletions test/function/samples/plugin-module-information/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const assert = require('assert');
const path = require('path');

const ID_MAIN = path.join(__dirname, 'main.js');
const ID_FOO = path.join(__dirname, 'foo.js');
const ID_NESTED = path.join(__dirname, 'nested', 'nested.js');
const ID_PATH = 'path';

let rendered = false;

module.exports = {
description: 'provides module information on the plugin context',
options: {
external: ['path'],
plugins: [
{
renderStart() {
rendered = true;
assert.deepEqual(Array.from(this.moduleIds), [ID_MAIN, ID_FOO, ID_NESTED, ID_PATH]);
assert.deepEqual(this.getModuleInfo(ID_MAIN), {
id: ID_MAIN,
importedIds: [ID_FOO, ID_NESTED],
isExternal: false
});
assert.deepEqual(this.getModuleInfo(ID_FOO), {
id: ID_FOO,
importedIds: [ID_PATH],
isExternal: false
});
assert.deepEqual(this.getModuleInfo(ID_NESTED), {
id: ID_NESTED,
importedIds: [ID_FOO],
isExternal: false
});
assert.deepEqual(this.getModuleInfo(ID_PATH), {
id: ID_PATH,
importedIds: [],
isExternal: true
});
}
}
]
},
bundle() {
assert.ok(rendered);
}
};
3 changes: 3 additions & 0 deletions test/function/samples/plugin-module-information/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import path from 'path';

export const foo = path.resolve('foo');
4 changes: 4 additions & 0 deletions test/function/samples/plugin-module-information/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {foo} from './foo.js';
import { nested } from './nested/nested';

export {nested};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { foo } from '../foo.js';

export const nested = 'nested' + foo;

0 comments on commit 1ef9f6b

Please sign in to comment.