From 2123158408abe05b96cfe198e1e25257c61e28de Mon Sep 17 00:00:00 2001 From: ryo Date: Tue, 10 Mar 2020 23:27:31 +0900 Subject: [PATCH] chore(gatsby): Convert utils/path to typescript (#22130) --- .../load-babel-config/gatsby-node.js | 2 +- .../src/utils/__tests__/{path.js => path.ts} | 8 ++++---- packages/gatsby/src/utils/{path.js => path.ts} | 18 ++++++++---------- packages/gatsby/src/utils/webpack.config.js | 2 +- 4 files changed, 14 insertions(+), 16 deletions(-) rename packages/gatsby/src/utils/__tests__/{path.js => path.ts} (94%) rename packages/gatsby/src/utils/{path.js => path.ts} (60%) diff --git a/packages/gatsby/src/internal-plugins/load-babel-config/gatsby-node.js b/packages/gatsby/src/internal-plugins/load-babel-config/gatsby-node.js index 6c95b82e82f12..613d6d345c2aa 100644 --- a/packages/gatsby/src/internal-plugins/load-babel-config/gatsby-node.js +++ b/packages/gatsby/src/internal-plugins/load-babel-config/gatsby-node.js @@ -3,7 +3,7 @@ const fs = require(`fs-extra`) const apiRunnerNode = require(`../../utils/api-runner-node`) -const { withBasePath } = require(`../../utils/path`) +import { withBasePath } from "../../utils/path" exports.onPreBootstrap = async ({ store, parentSpan }) => { const { directory, browserslist } = store.getState().program diff --git a/packages/gatsby/src/utils/__tests__/path.js b/packages/gatsby/src/utils/__tests__/path.ts similarity index 94% rename from packages/gatsby/src/utils/__tests__/path.js rename to packages/gatsby/src/utils/__tests__/path.ts index d751398acbf35..d7cfef4ba7acf 100644 --- a/packages/gatsby/src/utils/__tests__/path.js +++ b/packages/gatsby/src/utils/__tests__/path.ts @@ -1,6 +1,6 @@ -const { joinPath } = require(`gatsby-core-utils`) -const { withBasePath, getCommonDir } = require(`../path`) -const os = require(`os`) +import { joinPath } from "gatsby-core-utils" +import { withBasePath, getCommonDir } from "../path" +import os from "os" describe(`paths`, () => { describe(`joinPath`, () => { @@ -55,7 +55,7 @@ describe(`paths`, () => { }) describe(`getCommonDir`, () => { - it.each([ + it.each<[string, { path1: string; path2: string; expected: string }]>([ [ `posix: path2 is sub-path of path1`, { diff --git a/packages/gatsby/src/utils/path.js b/packages/gatsby/src/utils/path.ts similarity index 60% rename from packages/gatsby/src/utils/path.js rename to packages/gatsby/src/utils/path.ts index 6bb8d0469dfe8..c22986436b45d 100644 --- a/packages/gatsby/src/utils/path.js +++ b/packages/gatsby/src/utils/path.ts @@ -1,22 +1,20 @@ -const path = require(`path`) -const { joinPath } = require(`gatsby-core-utils`) +import path from "path" +import { joinPath } from "gatsby-core-utils" -export function withBasePath(basePath) { - return (...paths) => joinPath(basePath, ...paths) -} +export const withBasePath = (basePath: string) => ( + ...paths: string[] +): string => joinPath(basePath, ...paths) -export function withTrailingSlash(basePath) { - return `${basePath}/` -} +export const withTrailingSlash = (basePath: string): string => `${basePath}/` -const posixJoinWithLeadingSlash = paths => +const posixJoinWithLeadingSlash = (paths: string[]): string => path.posix.join( ...paths.map((segment, index) => segment === `` && index === 0 ? `/` : segment ) ) -export function getCommonDir(path1, path2) { +export const getCommonDir = (path1: string, path2: string): string => { const path1Segments = path1.split(/[/\\]/) const path2Segments = path2.split(/[/\\]/) diff --git a/packages/gatsby/src/utils/webpack.config.js b/packages/gatsby/src/utils/webpack.config.js index 52b35fe207579..feac949c57e2d 100644 --- a/packages/gatsby/src/utils/webpack.config.js +++ b/packages/gatsby/src/utils/webpack.config.js @@ -9,7 +9,7 @@ const { actions } = require(`../redux/actions`) const { getPublicPath } = require(`./get-public-path`) const debug = require(`debug`)(`gatsby:webpack-config`) const report = require(`gatsby-cli/lib/reporter`) -const { withBasePath, withTrailingSlash } = require(`./path`) +import { withBasePath, withTrailingSlash } from "./path" const getGatsbyDependents = require(`./gatsby-dependents`) const apiRunnerNode = require(`./api-runner-node`)