Skip to content

Commit

Permalink
chore(gatsby): Convert redirects-writer to typescript (#22530)
Browse files Browse the repository at this point in the history
* chore(gatsby): Convert redirects-writer to typescript

* refactor: Wrap and export emitter
  • Loading branch information
mottox2 authored May 5, 2020
1 parent 7b16404 commit c1bbbd1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
4 changes: 3 additions & 1 deletion packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<void> => {
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)
Expand All @@ -31,16 +32,15 @@ const writeRedirects = async () => {
)
}

exports.writeRedirects = writeRedirects

let bootstrapFinished = false
const debouncedWriteRedirects = _.debounce(() => {
// Don't write redirects again until bootstrap has finished.
if (bootstrapFinished) {
writeRedirects()
}
}, 250)

emitter.on(`CREATE_REDIRECT`, () => {
debouncedWriteRedirects()
})
export const startRedirectListener = (): void => {
emitter.on(`CREATE_REDIRECT`, () => {
debouncedWriteRedirects()
})
}

0 comments on commit c1bbbd1

Please sign in to comment.