Skip to content

Commit

Permalink
chore(gatsby): migrate webpack-error-utils to TypeScript (#22278)
Browse files Browse the repository at this point in the history
* chore(gatsby): migrate webpack-error-utils to TypeScript

* fix pointed out
  • Loading branch information
sasurau4 committed Mar 19, 2020
1 parent 3380e89 commit e4cd2f1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby/src/commands/build.js
Expand Up @@ -16,7 +16,7 @@ const { store, readState } = require(`../redux`)
const queryUtil = require(`../query`)
import * as appDataUtil from "../utils/app-data"
import * as WorkerPool from "../utils/worker/pool"
const { structureWebpackErrors } = require(`../utils/webpack-error-utils`)
import { structureWebpackErrors } from "../utils/webpack-error-utils"
import {
userPassesFeedbackRequestHeuristic,
showFeedbackRequest,
Expand Down
@@ -1,12 +1,31 @@
const reporter = require(`gatsby-cli/lib/reporter`)
import { Stats } from "webpack"

const stageCodeToReadableLabel = {
"build-javascript": `Generating JavaScript bundles`,
"build-html": `Generating SSR bundle`,
develop: `Generating development JavaScript bundle`,
}
} as const

type Stage = keyof typeof stageCodeToReadableLabel
type StageLabel = typeof stageCodeToReadableLabel[Stage]

const transformWebpackError = (stage, webpackError) => {
interface ITransformedWebpackError {
id: "98123"
filePath?: string
location?: {
start: string
}
context: {
stage: Stage
stageLabel: StageLabel
message?: string
}
}
const transformWebpackError = (
stage: keyof typeof stageCodeToReadableLabel,
webpackError: any
): ITransformedWebpackError => {
return {
id: `98123`,
filePath: webpackError?.module?.resource,
Expand All @@ -30,15 +49,18 @@ const transformWebpackError = (stage, webpackError) => {
}
}

exports.structureWebpackErrors = (stage, webpackError) => {
export const structureWebpackErrors = (
stage: Stage,
webpackError: any
): ITransformedWebpackError[] | ITransformedWebpackError => {
if (Array.isArray(webpackError)) {
return webpackError.map(e => transformWebpackError(stage, e))
}

return transformWebpackError(stage, webpackError)
}

exports.reportWebpackWarnings = stats => {
export const reportWebpackWarnings = (stats: Stats): void => {
stats.compilation.warnings.forEach(webpackWarning => {
if (webpackWarning.warning) {
// grab inner Exception if it exists
Expand Down

0 comments on commit e4cd2f1

Please sign in to comment.