Skip to content
This repository has been archived by the owner on Aug 5, 2020. It is now read-only.

Commit

Permalink
add support for runtime plugin options (#1192)
Browse files Browse the repository at this point in the history
* add support for runtime plugin options

* unify plugin types

* update tests

* update tests with new unified types

* fix lint issues
  • Loading branch information
toddw committed May 10, 2018
1 parent e42ba60 commit b9a8977
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 65 deletions.
15 changes: 9 additions & 6 deletions packages/gluestick/src/generator/predefined/clientEntryInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ if (typeof window === "object") {
${args =>
args.plugins
.filter(plugin => plugin.meta.wrapper)
.reduce((prev, curr, index) => {
return prev.concat(
`${index > 0 ? ' ' : ''}${convertToCamelCase(
curr.name,
)}.plugin,\n`,
);
.reduce((prev, curr) => {
return prev.concat(`
{
name: "${curr.meta ? curr.meta.name : curr.name || 'unknown'}",
meta: ${JSON.stringify(curr.meta)},
body: ${convertToCamelCase(curr.name)}.plugin,
options: ${JSON.stringify(curr.options)}
},\n
`);
}, '')}
];
Expand Down
20 changes: 13 additions & 7 deletions packages/gluestick/src/generator/predefined/serverEntries.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,21 @@ ${args =>
export const plugins = [
${args =>
args.plugins.reduce((prev, curr, index, arr) => {
args.plugins.reduce((prev, curr, index) => {
return prev.concat(
`${index > 0 ? ' ' : ''}` +
`{ ref: ${getPluginName(curr.name, curr.meta.type)}, type: "${curr
.meta.type}"` +
`${curr.options
? `, options: ${JSON.stringify(curr.options)} },`
: ' },'}` +
`${arr.length - 1 !== index ? '\n' : ''}`,
`{
body: typeof ${getPluginName(
curr.name,
curr.meta.type,
)} === "function" ? ${getPluginName(
curr.name,
curr.meta.type,
)} : ${getPluginName(curr.name, curr.meta.type)}.plugin,
meta: ${JSON.stringify(curr.meta)},
options: ${JSON.stringify(curr.options)}
},
`,
);
}, '')}
];
Expand Down
4 changes: 3 additions & 1 deletion packages/gluestick/src/generator/templates/EntryWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ export default class EntryWrapper extends Component {
} = this.props;
return rootWrappers.reduce((prev, curr) => {
return curr(prev, rootWrappersOptions);
const plugin = curr.body;
const options = curr.options || {};
return plugin(prev, {...rootWrappersOptions, ...options});
}, (
<Root
routerContext={routerContext}
Expand Down
30 changes: 11 additions & 19 deletions packages/gluestick/src/plugins/__tests__/serverPlugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@ const plugin2Ref = () => {};
plugin2Ref.meta = { type: 'runtime' };
const validPlugins = [
{
ref: plugin0Ref,
type: 'server',
body: plugin0Ref,
},
{
ref: plugin1Ref,
body: plugin1Ref,
options: { prop: true },
type: 'server',
},
{
ref: plugin2Ref,
type: 'runtime',
body: plugin2Ref,
},
];

Expand All @@ -43,19 +40,14 @@ describe('plugins/serverPlugins', () => {
it('should return plugins array', () => {
jest.doMock('project-entries', () => ({
plugins: [
{ body: plugin0Ref, meta: plugin0Ref.meta, name: plugin0Ref.meta.name },
{
ref: plugin0Ref,
type: 'server',
},
{
ref: plugin1Ref,
body: plugin1Ref,
meta: plugin1Ref.meta,
name: plugin1Ref.meta.name,
options: { prop: true },
type: 'server',
},
{
ref: plugin2Ref,
type: 'runtime',
},
{ body: plugin2Ref, meta: plugin2Ref.meta },
],
}));
const plugins = require('../serverPlugins');
Expand All @@ -74,7 +66,7 @@ describe('plugins/serverPlugins', () => {
jest.doMock('project-entries', () => ({
plugins: [
{
ref: 'abc',
body: 'abc',
},
],
}));
Expand All @@ -96,8 +88,8 @@ describe('plugins/serverPlugins', () => {
jest.doMock('project-entries', () => ({
plugins: [
{
ref: invalidPlugin,
type: 'server',
body: invalidPlugin,
meta: invalidPlugin.meta,
},
],
}));
Expand Down
25 changes: 7 additions & 18 deletions packages/gluestick/src/plugins/serverPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ type CompilationResults = {
error?: Error,
};

type PluginRef = {
ref: Function,
type: string,
options?: Object,
};

/**
* Compile plugin.
*/
Expand Down Expand Up @@ -57,14 +51,11 @@ const prepareServerPlugins = (): ServerPlugin[] => {
try {
// Get server plugins only and perform necessry checks.
const filteredPlugins = plugins.filter(
(plugin: PluginRef, index: number): boolean => {
if (
typeof plugin.ref !== 'function' &&
typeof plugin.ref.plugin !== 'function'
) {
(plugin: Plugin, index: number): boolean => {
if (typeof plugin !== 'function' && typeof plugin.body !== 'function') {
throw new Error(`Plugin at position ${index} must export a function`);
}
return plugin.type === 'server';
return plugin.meta.type === 'server';
},
);
if (!filteredPlugins.length) {
Expand All @@ -74,13 +65,11 @@ const prepareServerPlugins = (): ServerPlugin[] => {
let logMessage: string = 'Compiling server plugins:\n';
// Compile plugin, if compilation fails, further compilation is prevented.
const compiledPlugins: ServerPlugin[] = filteredPlugins.map(
(value: PluginRef): ServerPlugin => {
(value: Plugin): ServerPlugin => {
const normalizedPlugin: Plugin = {
name: value.ref.meta
? value.ref.meta.name
: value.ref.name || 'unknown',
meta: value.ref.meta || {},
body: value.ref,
name: value.meta ? value.meta.name : value.name || 'unknown',
meta: value.meta || {},
body: value.body,
options: value.options || {},
};
// Second `compilePlugin` argument is an object with gluestick utilities that
Expand Down
8 changes: 3 additions & 5 deletions packages/gluestick/src/renderer/__tests__/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ describe('renderer/render', () => {

it('should prepare plugins and pass it to EntryWrapper', () => {
const entriesRuntimePlugins = [
{ plugin: v => v, meta: { wrapper: true } },
{ plugin: () => {}, meta: {} },
{ name: 'plugin1', body: v => v, meta: { wrapper: true } },
{ name: 'plugin2', body: () => {}, meta: {} },
];
// eslint-disable-next-line react/no-multi-comp
class MockEntryWrapper extends React.Component {
Expand Down Expand Up @@ -220,9 +220,7 @@ describe('renderer/render', () => {
{ assets, loadjsConfig, cacheManager },
{},
);
expect(MockEntryWrapper.plugins).toEqual([
entriesRuntimePlugins[0].plugin,
]);
expect(MockEntryWrapper.plugins).toEqual([entriesRuntimePlugins[0]]);
});

describe('when the route is an email route', () => {
Expand Down
7 changes: 4 additions & 3 deletions packages/gluestick/src/renderer/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
RenderOutput,
CacheManager,
RenderMethod,
Plugin,
} from '../types';

import hooks from './helpers/hooks';
Expand Down Expand Up @@ -162,9 +163,9 @@ const middleware: Middleware = async (req, res, { assets }) => {
const statusCode: number = getStatusCode(store, currentRoute);

// Get runtime plugins that will be passed to EntryWrapper.
const runtimePlugins: Object[] = entriesPlugins
.filter((plugin: Object) => plugin.type === 'runtime')
.map((plugin: Object) => plugin.ref);
const runtimePlugins: Plugin[] = entriesPlugins.filter(
(plugin: Object) => plugin.meta.type === 'runtime',
);

const outputBeforeHooks: RenderOutput = await render(
{ config, logger },
Expand Down
14 changes: 9 additions & 5 deletions packages/gluestick/src/renderer/render.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/* @flow */
import type { Context, Request, RenderOutput, RenderMethod } from '../types';
import type {
Context,
Request,
RenderOutput,
RenderMethod,
Plugin,
} from '../types';

const React = require('react');
const { RouterContext } = require('react-router');
Expand Down Expand Up @@ -29,7 +35,7 @@ type WrappersRequirements = {
BodyWrapper: Object,
entryWrapperConfig: Object,
envVariables: any[],
entriesPlugins: { plugin: Function, meta: Object }[],
entriesPlugins: Plugin[],
};
type AssetsCacheOpts = {
assets: Object,
Expand Down Expand Up @@ -60,9 +66,7 @@ module.exports = async function render(
);
const isEmail = !!currentRoute.email;
const routerContext = <RouterContext {...renderProps} />;
const rootWrappers = entriesPlugins
.filter(plugin => plugin.meta.wrapper)
.map(({ plugin }) => plugin);
const rootWrappers = entriesPlugins.filter(plugin => plugin.meta.wrapper);
const entryWrapper = (
<EntryWrapper
store={store}
Expand Down
2 changes: 1 addition & 1 deletion packages/gluestick/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export type Plugin = {
[key: string]: any,
},
body: Function | null,
options: Object,
options?: Object,
};

export type ConfigPlugin = {
Expand Down

0 comments on commit b9a8977

Please sign in to comment.