diff --git a/packages/gatsby/src/query/query-compiler.js b/packages/gatsby/src/query/query-compiler.js index 455b43142add1..2b5c067861f40 100644 --- a/packages/gatsby/src/query/query-compiler.js +++ b/packages/gatsby/src/query/query-compiler.js @@ -29,7 +29,7 @@ const { VariablesInAllowedPositionRule, } = require(`graphql`) -const getGatsbyDependents = require(`../utils/gatsby-dependents`) +import { getGatsbyDependents } from "../utils/gatsby-dependents" const { store } = require(`../redux`) import * as actions from "../redux/actions/internal" const { default: FileParser } = require(`./file-parser`) diff --git a/packages/gatsby/src/query/query-watcher.js b/packages/gatsby/src/query/query-watcher.js index e002e83bf759c..3506375919275 100644 --- a/packages/gatsby/src/query/query-watcher.js +++ b/packages/gatsby/src/query/query-watcher.js @@ -20,7 +20,7 @@ const queryCompiler = require(`./query-compiler`).default const report = require(`gatsby-cli/lib/reporter`) const queryUtil = require(`./index`) const debug = require(`debug`)(`gatsby:query-watcher`) -const getGatsbyDependents = require(`../utils/gatsby-dependents`) +import { getGatsbyDependents } from "../utils/gatsby-dependents" const getQueriesSnapshot = () => { const state = store.getState() diff --git a/packages/gatsby/src/utils/gatsby-dependents.js b/packages/gatsby/src/utils/gatsby-dependents.ts similarity index 50% rename from packages/gatsby/src/utils/gatsby-dependents.js rename to packages/gatsby/src/utils/gatsby-dependents.ts index 69cdcd053a89a..effcf12e11998 100644 --- a/packages/gatsby/src/utils/gatsby-dependents.js +++ b/packages/gatsby/src/utils/gatsby-dependents.ts @@ -1,32 +1,35 @@ import { store } from "../redux" import { memoize } from "lodash" -const { createRequireFromPath } = require(`gatsby-core-utils`) -const { join, dirname } = require(`path`) +import { createRequireFromPath } from "gatsby-core-utils" +import { join, dirname } from "path" +import { PackageJson } from "../.." +import { readFile } from "fs-extra" -const fs = require(`fs`) -const { promisify } = require(`util`) -const readFile = promisify(fs.readFile) +interface IDependency { + name: string + version: string + path: string +} -const getAllDependencies = (pkg, { noDev } = {}) => +const getAllDependencies = (pkg: PackageJson): Set<[string, string]> => new Set([ ...Object.entries(pkg.dependencies || {}), - ...Object.entries((!noDev && pkg.devDependencies) || {}), + ...Object.entries(pkg.devDependencies || {}), ...Object.entries(pkg.optionalDependencies || {}), ]) -const readJSON = async file => { +const readJSON = async (file: string): Promise => { const buffer = await readFile(file) return JSON.parse(buffer.toString()) } const getTreeFromNodeModules = async ( - dir, - filterFn = () => true, - results = new Map() -) => { + dir: string, + results: Map = new Map() +): Promise => { const requireFromHere = createRequireFromPath(`${dir}/:internal:`) - let packageJSON + let packageJSON: PackageJson try { packageJSON = await readJSON(require.resolve(join(dir, `package.json`))) } catch (error) { @@ -36,17 +39,18 @@ const getTreeFromNodeModules = async ( await Promise.all( Array.from(getAllDependencies(packageJSON)).map(async ([name, version]) => { try { - const currentDependency = { + const currentDependency: IDependency = { name, version, path: dirname(requireFromHere.resolve(`${name}/package.json`)), } - if (filterFn(currentDependency)) { - await getTreeFromNodeModules( - currentDependency.path, - filterFn, - results - ) + + // Include anything that has `gatsby` in its name but not `gatsby` itself + if ( + /gatsby/.test(currentDependency.name) && + currentDependency.name !== `gatsby` + ) { + await getTreeFromNodeModules(currentDependency.path, results) if (!results.has(currentDependency.name)) results.set(currentDependency.name, currentDependency) } @@ -56,16 +60,13 @@ const getTreeFromNodeModules = async ( } }) ) + return Array.from(results.values()) } -// Returns [Object] with name and path -module.exports = memoize(async () => { - const { program } = store.getState() - const nodeModules = await getTreeFromNodeModules( - program.directory, - // Include anything that has `gatsby` in its name but not `gatsby` itself - dependency => /gatsby/.test(dependency.name) && dependency.name !== `gatsby` - ) - return nodeModules -}) +export const getGatsbyDependents = memoize( + async (): Promise => { + const { program } = store.getState() + return getTreeFromNodeModules(program.directory) + } +) diff --git a/packages/gatsby/src/utils/webpack.config.js b/packages/gatsby/src/utils/webpack.config.js index 97f0da1154d16..27d67ae9a5332 100644 --- a/packages/gatsby/src/utils/webpack.config.js +++ b/packages/gatsby/src/utils/webpack.config.js @@ -10,7 +10,7 @@ const { getPublicPath } = require(`./get-public-path`) const debug = require(`debug`)(`gatsby:webpack-config`) const report = require(`gatsby-cli/lib/reporter`) import { withBasePath, withTrailingSlash } from "./path" -const getGatsbyDependents = require(`./gatsby-dependents`) +import { getGatsbyDependents } from "./gatsby-dependents" const apiRunnerNode = require(`./api-runner-node`) const createUtils = require(`./webpack-utils`)