diff --git a/packages/gatsby/src/bootstrap/__mocks__/resolve-module-exports.js b/packages/gatsby/src/bootstrap/__mocks__/resolve-module-exports.ts similarity index 78% rename from packages/gatsby/src/bootstrap/__mocks__/resolve-module-exports.js rename to packages/gatsby/src/bootstrap/__mocks__/resolve-module-exports.ts index 5c2454d82f97a..2ec1d9decca18 100644 --- a/packages/gatsby/src/bootstrap/__mocks__/resolve-module-exports.js +++ b/packages/gatsby/src/bootstrap/__mocks__/resolve-module-exports.ts @@ -2,7 +2,7 @@ let mockResults = {} -module.exports = input => { +export const resolveModuleExports = (input: unknown): string[] | undefined => { // return a mocked result if (typeof input === `string`) { return mockResults[input] diff --git a/packages/gatsby/src/bootstrap/__tests__/resolve-module-exports.js b/packages/gatsby/src/bootstrap/__tests__/resolve-module-exports.js index aac8a6f128860..7c6ca196a1386 100644 --- a/packages/gatsby/src/bootstrap/__tests__/resolve-module-exports.js +++ b/packages/gatsby/src/bootstrap/__tests__/resolve-module-exports.js @@ -13,7 +13,7 @@ jest.mock(`gatsby-cli/lib/reporter`, () => { const fs = require(`fs`) const reporter = require(`gatsby-cli/lib/reporter`) -const resolveModuleExports = require(`../resolve-module-exports`) +const { resolveModuleExports } = require(`../resolve-module-exports`) let resolver describe(`Resolve module exports`, () => { diff --git a/packages/gatsby/src/bootstrap/load-plugins/__tests__/validate.js b/packages/gatsby/src/bootstrap/load-plugins/__tests__/validate.js index 1d08d7fed15cd..c118e78575c49 100644 --- a/packages/gatsby/src/bootstrap/load-plugins/__tests__/validate.js +++ b/packages/gatsby/src/bootstrap/load-plugins/__tests__/validate.js @@ -41,7 +41,7 @@ describe(`collatePluginAPIs`, () => { } beforeEach(() => { - const resolveModuleExports = require(`../../resolve-module-exports`) + const { resolveModuleExports } = require(`../../resolve-module-exports`) resolveModuleExports(MOCK_RESULTS) }) diff --git a/packages/gatsby/src/bootstrap/load-plugins/validate.js b/packages/gatsby/src/bootstrap/load-plugins/validate.js index 4fdaffba5392c..fd9c8bc433e99 100644 --- a/packages/gatsby/src/bootstrap/load-plugins/validate.js +++ b/packages/gatsby/src/bootstrap/load-plugins/validate.js @@ -3,7 +3,7 @@ const semver = require(`semver`) const stringSimilarity = require(`string-similarity`) const { version: gatsbyVersion } = require(`gatsby/package.json`) const reporter = require(`gatsby-cli/lib/reporter`) -const resolveModuleExports = require(`../resolve-module-exports`) +const { resolveModuleExports } = require(`../resolve-module-exports`) const { getLatestAPIs } = require(`../../utils/get-latest-apis`) const getGatsbyUpgradeVersion = entries => diff --git a/packages/gatsby/src/bootstrap/resolve-module-exports.js b/packages/gatsby/src/bootstrap/resolve-module-exports.ts similarity index 84% rename from packages/gatsby/src/bootstrap/resolve-module-exports.js rename to packages/gatsby/src/bootstrap/resolve-module-exports.ts index 8d141291c5007..3e5611f02bda9 100644 --- a/packages/gatsby/src/bootstrap/resolve-module-exports.js +++ b/packages/gatsby/src/bootstrap/resolve-module-exports.ts @@ -1,16 +1,18 @@ -// @flow import fs from "fs" import traverse from "@babel/traverse" import get from "lodash/get" -import { codeFrameColumns } from "@babel/code-frame" +import { codeFrameColumns, SourceLocation } from "@babel/code-frame" import { babelParseToAst } from "../utils/babel-parse-to-ast" import report from "gatsby-cli/lib/reporter" import { testRequireError } from "../utils/test-require-error" -const staticallyAnalyzeExports = (modulePath, resolver = require.resolve) => { - let absPath - const exportNames = [] +const staticallyAnalyzeExports = ( + modulePath: string, + resolver = require.resolve +): string[] => { + let absPath: string | undefined + const exportNames: string[] = [] try { absPath = resolver(modulePath) @@ -28,7 +30,7 @@ const staticallyAnalyzeExports = (modulePath, resolver = require.resolve) => { const codeFrame = codeFrameColumns( code, { - start: err.loc, + start: ((err as unknown) as { loc: SourceLocation["start"] }).loc, }, { highlightCode: true, @@ -50,7 +52,7 @@ const staticallyAnalyzeExports = (modulePath, resolver = require.resolve) => { // extract names of exports from file traverse(ast, { // Check if the file is using ES6 imports - ImportDeclaration: function ImportDeclaration(astPath) { + ImportDeclaration: function ImportDeclaration() { isES6 = true }, @@ -59,8 +61,8 @@ const staticallyAnalyzeExports = (modulePath, resolver = require.resolve) => { // get foo from `export const foo = bar` if ( - get(astPath, `node.declaration.type`) === `VariableDeclaration` && - get(astPath, `node.declaration.declarations[0].id.name`) + declaration?.type === `VariableDeclaration` && + declaration.declarations[0]?.id.type === `Identifier` ) { isES6 = true exportNames.push(declaration.declarations[0].id.name) @@ -68,8 +70,8 @@ const staticallyAnalyzeExports = (modulePath, resolver = require.resolve) => { // get foo from `export function foo()` if ( - get(astPath, `node.declaration.type`) === `FunctionDeclaration` && - get(astPath, `node.declaration.id.name`) + declaration?.type === `FunctionDeclaration` && + declaration.id?.type === `Identifier` ) { isES6 = true exportNames.push(declaration.id.name) @@ -139,16 +141,16 @@ https://gatsby.dev/no-mixed-modules * * Returns [] for invalid paths and modules without exports. * - * @param {string} modulePath - * @param {string} mode - * @param {function} resolver + * @param modulePath + * @param mode + * @param resolver */ -module.exports = ( - modulePath, +export const resolveModuleExports = ( + modulePath: string, { mode = `analysis`, resolver = require.resolve } = {} -) => { +): string[] => { if (mode === `require`) { - let absPath + let absPath: string | undefined try { absPath = resolver(modulePath) return Object.keys(require(modulePath)).filter(