From d8b667ae4ca6da13cff737c250305a78dfdc62d8 Mon Sep 17 00:00:00 2001 From: ryo Date: Tue, 31 Mar 2020 00:41:51 +0900 Subject: [PATCH] chore(gatsby): Migrate src/redux/plugin-runner to TypeScript (#22476) * chore(gatsby): Migrate src/redux/plugin-runner to TypeScript * Improve typing and reduce module side effects * Add return type Co-authored-by: Blaine Kasten --- packages/gatsby/src/bootstrap/index.js | 3 +- packages/gatsby/src/redux/plugin-runner.js | 13 ------ packages/gatsby/src/redux/plugin-runner.ts | 51 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 14 deletions(-) delete mode 100644 packages/gatsby/src/redux/plugin-runner.js create mode 100644 packages/gatsby/src/redux/plugin-runner.ts diff --git a/packages/gatsby/src/bootstrap/index.js b/packages/gatsby/src/bootstrap/index.js index 95d075a45936c..7e5fa7e712b01 100644 --- a/packages/gatsby/src/bootstrap/index.js +++ b/packages/gatsby/src/bootstrap/index.js @@ -13,6 +13,7 @@ const telemetry = require(`gatsby-telemetry`) const apiRunnerNode = require(`../utils/api-runner-node`) import { getBrowsersList } from "../utils/browserslist" import { createSchemaCustomization } from "../utils/create-schema-customization" +import { startPluginRunner } from "../redux/plugin-runner" const { store, emitter } = require(`../redux`) const loadPlugins = require(`./load-plugins`) const loadThemes = require(`./load-themes`) @@ -66,7 +67,7 @@ module.exports = async (args: BootstrapArgs) => { // Start plugin runner which listens to the store // and invokes Gatsby API based on actions. - require(`../redux/plugin-runner`) + startPluginRunner() const directory = slash(args.directory) diff --git a/packages/gatsby/src/redux/plugin-runner.js b/packages/gatsby/src/redux/plugin-runner.js deleted file mode 100644 index 5c5313fd5063a..0000000000000 --- a/packages/gatsby/src/redux/plugin-runner.js +++ /dev/null @@ -1,13 +0,0 @@ -// Invoke plugins for certain actions. - -const { emitter } = require(`./index`) -const apiRunnerNode = require(`../utils/api-runner-node`) - -emitter.on(`CREATE_PAGE`, action => { - const page = action.payload - apiRunnerNode( - `onCreatePage`, - { page, traceId: action.traceId, parentSpan: action.parentSpan }, - { pluginSource: action.plugin.name, activity: action.activity } - ) -}) diff --git a/packages/gatsby/src/redux/plugin-runner.ts b/packages/gatsby/src/redux/plugin-runner.ts new file mode 100644 index 0000000000000..a00c762f02cfe --- /dev/null +++ b/packages/gatsby/src/redux/plugin-runner.ts @@ -0,0 +1,51 @@ +import { Span } from "opentracing" +import { emitter } from "./index" +import apiRunnerNode from "../utils/api-runner-node" +import { ActivityTracker } from "../../" + +type Plugin = any // TODO + +// This might make sense to live somewhere else +interface ICreatePageAction { + graphql( + query: string, + variables?: TVariables + ): Promise<{ + errors?: any + data?: TData + }> + traceId: "initial-createPages" + waitForCascadingActions: boolean + parentSpan: Span + activity: ActivityTracker + type: `CREATE_PAGE` + contextModified: boolean + plugin: Plugin + payload: { + internalComponentName: string + path: string + matchPath: string | undefined + component: string + componentChunkName: string + isCreatedByStatefulCreatePages: boolean + context: { + slug: string + id: string + } + updatedAt: number + pluginCreator__NODE: string + pluginCreatorId: string + componentPath: string + } +} + +export const startPluginRunner = (): void => { + emitter.on(`CREATE_PAGE`, (action: ICreatePageAction) => { + const page = action.payload + apiRunnerNode( + `onCreatePage`, + { page, traceId: action.traceId, parentSpan: action.parentSpan }, + { pluginSource: action.plugin.name, activity: action.activity } + ) + }) +}