diff --git a/packages/gatsby/src/bootstrap/index.js b/packages/gatsby/src/bootstrap/index.js index a7b066f1a048c..45f17fd6c2dab 100644 --- a/packages/gatsby/src/bootstrap/index.js +++ b/packages/gatsby/src/bootstrap/index.js @@ -32,7 +32,7 @@ process.on(`unhandledRejection`, (reason, p) => { import { createGraphQLRunner } from "./create-graphql-runner" const { extractQueries } = require(`../query/query-watcher`) const requiresWriter = require(`./requires-writer`) -const { writeRedirects } = require(`./redirects-writer`) +import { writeRedirects, startRedirectListener } from "./redirects-writer" // Override console.log to add the source file + line number. // Useful for debugging if you lose a console.log somewhere. @@ -70,6 +70,8 @@ module.exports = async (args: BootstrapArgs) => { // and invokes Gatsby API based on actions. startPluginRunner() + startRedirectListener() + const directory = slash(args.directory) const program = { diff --git a/packages/gatsby/src/bootstrap/redirects-writer.js b/packages/gatsby/src/bootstrap/redirects-writer.ts similarity index 71% rename from packages/gatsby/src/bootstrap/redirects-writer.js rename to packages/gatsby/src/bootstrap/redirects-writer.ts index f6a5611279e0a..6d93efac9e4d1 100644 --- a/packages/gatsby/src/bootstrap/redirects-writer.js +++ b/packages/gatsby/src/bootstrap/redirects-writer.ts @@ -1,15 +1,16 @@ import _ from "lodash" import crypto from "crypto" import fs from "fs-extra" -import { store, emitter } from "../redux/" +import { store, emitter } from "../redux" import { joinPath } from "gatsby-core-utils" -let lastHash = null +let lastHash: string | null = null +let bootstrapFinished = false -const writeRedirects = async () => { +export const writeRedirects = async (): Promise => { bootstrapFinished = true - let { program, redirects } = store.getState() + const { program, redirects } = store.getState() // Filter for redirects that are meant for the browser. const browserRedirects = redirects.filter(r => r.redirectInBrowser) @@ -31,9 +32,6 @@ const writeRedirects = async () => { ) } -exports.writeRedirects = writeRedirects - -let bootstrapFinished = false const debouncedWriteRedirects = _.debounce(() => { // Don't write redirects again until bootstrap has finished. if (bootstrapFinished) { @@ -41,6 +39,8 @@ const debouncedWriteRedirects = _.debounce(() => { } }, 250) -emitter.on(`CREATE_REDIRECT`, () => { - debouncedWriteRedirects() -}) +export const startRedirectListener = (): void => { + emitter.on(`CREATE_REDIRECT`, () => { + debouncedWriteRedirects() + }) +}