Skip to content

Commit bdd3ff5

Browse files
committed
Add onTransformStart method
1 parent 12b2d48 commit bdd3ff5

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/sourcebit.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,21 @@ class Sourcebit {
183183
objects: []
184184
};
185185
const contextSnapshot = cloneDeep(this.context);
186+
187+
this.pluginBlocks.forEach((pluginBlock, index) => {
188+
const plugin = this.pluginModules[index];
189+
const pluginName = this.getNameOfPluginAtIndex(index);
190+
191+
if (typeof plugin.onTransformStart === 'function') {
192+
plugin.onTransformStart({
193+
debug: this.getDebugMethodForPlugin(pluginName),
194+
getPluginContext: () => contextSnapshot[pluginName] || {},
195+
log: this.logFromPlugin.bind(this),
196+
options: this.parsePluginOptions(plugin, pluginBlock.options)
197+
});
198+
}
199+
});
200+
186201
const onTransformEndCallbacks = [];
187202
const queue = this.pluginBlocks.reduce((queue, pluginBlock, index) => {
188203
// If the plugin hasn't been bootstrapped, we don't want to run its

lib/sourcebit.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,12 +896,14 @@ describe('`transform()`', () => {
896896
expect(callbackData).not.toBeDefined();
897897
});
898898

899-
test('calls the `onTransformEndFn` callback of each plugin, if defined, when `transform` has finished running', async () => {
899+
test('calls the `onTransformStart` and `onTransformEnd` callbacks of each plugin, if defined, when `transform` has finished running', async () => {
900+
const onTransformStartFn = jest.fn();
900901
const onTransformEndFn = jest.fn();
901902
const plugins = [
902903
{
903904
module: {
904905
name: 'sourcebit-test1',
906+
onTransformStart: onTransformStartFn,
905907
onTransformEnd: onTransformEndFn,
906908
transform: async ({ data }) => {
907909
return {
@@ -942,6 +944,7 @@ describe('`transform()`', () => {
942944
{
943945
module: {
944946
name: 'sourcebit-test4',
947+
onTransformStart: onTransformStartFn,
945948
onTransformEnd: onTransformEndFn
946949
}
947950
}
@@ -956,8 +959,16 @@ describe('`transform()`', () => {
956959

957960
const data = await sourcebit.transform();
958961

962+
expect(onTransformStartFn).toHaveBeenCalledTimes(2);
959963
expect(onTransformEndFn).toHaveBeenCalledTimes(4);
960964

965+
onTransformStartFn.mock.calls.forEach(call => {
966+
expect(call[0].debug).toBeInstanceOf(Function);
967+
expect(call[0].getPluginContext).toBeInstanceOf(Function);
968+
expect(call[0].log).toBeInstanceOf(Function);
969+
expect(call[0].options).toEqual({});
970+
});
971+
961972
onTransformEndFn.mock.calls.forEach(call => {
962973
expect(call[0].debug).toBeInstanceOf(Function);
963974
expect(call[0].getPluginContext).toBeInstanceOf(Function);

0 commit comments

Comments
 (0)