Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gatsby): Migrate utils/get-public-path.js to ts #22093

Merged
merged 2 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const getPublicPath = require(`../get-public-path`)
import { getPublicPath } from "../get-public-path"

const assetPrefix = `https://cdn.example.com`
const pathPrefix = `/blog`
Expand Down
12 changes: 6 additions & 6 deletions packages/gatsby/src/utils/api-runner-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const {
buildScalarType,
} = require(`../schema/types/type-builders`)
const { emitter, store } = require(`../redux`)
const getPublicPath = require(`./get-public-path`)
const { getPublicPath } = require(`./get-public-path`)
const { getNonGatsbyCodeFrameFormatted } = require(`./stack-trace-utils`)
const { trackBuildError, decorateEvent } = require(`gatsby-telemetry`)
const { default: errorParser } = require(`./api-runner-error-parser`)
Expand Down Expand Up @@ -170,7 +170,7 @@ const runAPI = (plugin, api, args, activity) => {
},
}
}
let localReporter = getLocalReporter(activity, reporter)
const localReporter = getLocalReporter(activity, reporter)

const apiCallArgs = [
{
Expand Down Expand Up @@ -238,8 +238,8 @@ const runAPI = (plugin, api, args, activity) => {
return null
}

let apisRunningById = new Map()
let apisRunningByTraceId = new Map()
const apisRunningById = new Map()
const apisRunningByTraceId = new Map()
let waitingForCasacadeToFinish = []

module.exports = async (api, args = {}, { pluginSource, activity } = {}) =>
Expand Down Expand Up @@ -332,7 +332,7 @@ module.exports = async (api, args = {}, { pluginSource, activity } = {}) =>
return null
}

let pluginName =
const pluginName =
plugin.name === `default-site-plugin` ? `gatsby-node.js` : plugin.name

return new Promise(resolve => {
Expand All @@ -342,7 +342,7 @@ module.exports = async (api, args = {}, { pluginSource, activity } = {}) =>
pluginName: `${plugin.name}@${plugin.version}`,
})

let localReporter = getLocalReporter(activity, reporter)
const localReporter = getLocalReporter(activity, reporter)

const file = stackTrace
.parse(err)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
const trimSlashes = part => part.replace(/(^\/)|(\/$)/g, ``)
const trimSlashes = (part: string): string => part.replace(/(^\/)|(\/$)/g, ``)

const isURL = possibleUrl =>
const isURL = (possibleUrl: string): boolean =>
[`http://`, `https://`, `//`].some(expr => possibleUrl.startsWith(expr))

module.exports = function getPublicPath({
export const getPublicPath = ({
assetPrefix,
pathPrefix,
prefixPaths,
}) {
}: {
assetPrefix?: string
pathPrefix?: string
prefixPaths: boolean
}): string => {
if (prefixPaths && (assetPrefix || pathPrefix)) {
const normalized = [assetPrefix, pathPrefix]
.filter(part => part && part.length > 0)
.filter((part): part is string => (part ? part.length > 0 : false))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

part is string is this valid syntax? Why not just part: string I think this line is wrong. I think you want (part: string): boolean => ..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

part is string is valid syntax called type predicate officially. As far as I know, TS cannot infer types exactly inside Array.prototype.reduce() so if it is (part: string): boolean => .. the compiler infers part in the next map as string | undefined
example

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL! V cool, thanks!

.map(part => trimSlashes(part))
.join(`/`)

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const dotenv = require(`dotenv`)
const PnpWebpackPlugin = require(`pnp-webpack-plugin`)
const { store } = require(`../redux`)
const { actions } = require(`../redux/actions`)
const getPublicPath = require(`./get-public-path`)
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`)
Expand Down