Skip to content

Commit

Permalink
chore(gatsby): convert babelrc to typescript (#24115)
Browse files Browse the repository at this point in the history
* Initial conversion

* Add ISetBabelPluginAction

* Fix naming

* Add missing actions

* Add missing plugin type

* Convert babelrc test file to TS

* COnvert snap file as well

* fix(gatsby): Protect about possibly missing context in graphql (#24108)

* Guard against possibly missing context in resolver

* Warn about bad resolver invocations

* Add warning

* Update packages/gatsby/src/schema/resolvers.ts

Co-authored-by: Ward Peeters <[email protected]>

* Fix typo

Co-authored-by: Ward Peeters <[email protected]>

* chore(release): Publish

 - [email protected]
 - [email protected]
 - [email protected]
 - [email protected]
 - [email protected]
 - [email protected]

* perf(gatsby): enable fast filters for `in` comparator (#24095)

* chore(release): Publish

 - [email protected]
 - [email protected]
 - [email protected]
 - [email protected]
 - [email protected]
 - [email protected]

Co-authored-by: Mikhail Novikov <[email protected]>
Co-authored-by: Ward Peeters <[email protected]>
Co-authored-by: Peter van der Zee <[email protected]>
Co-authored-by: Peter van der Zee <[email protected]>
  • Loading branch information
5 people committed May 18, 2020
1 parent 1d18c52 commit f3269e9
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { actions } = require(`../actions`)
const babelrcReducer = require(`../reducers/babelrc`)
const {
import { actions } from "../actions"
import { babelrcReducer } from "../reducers/babelrc"
import {
prepareOptions,
mergeConfigItemOptions,
} = require(`../../utils/babel-loader-helpers`)
} from "../../utils/babel-loader-helpers"

describe(`Babelrc actions/reducer`, () => {
it(`allows adding a new plugin`, () => {
Expand Down Expand Up @@ -75,7 +75,7 @@ describe(`Babelrc actions/reducer`, () => {
})

it(`sets default presets/plugins if there's no userland babelrc`, () => {
const fakeResolver = moduleName => `/path/to/module/${moduleName}`
const fakeResolver = (moduleName): string => `/path/to/module/${moduleName}`
const babel = { createConfigItem: jest.fn() }

prepareOptions(babel, { stage: `test` }, fakeResolver)
Expand Down Expand Up @@ -105,7 +105,7 @@ describe(`Babelrc actions/reducer`, () => {
{ options: { sourceMaps: `inline` }, stage: `develop` },
{ name: `test` }
)
let state = babelrcReducer(undefined, action)
const state = babelrcReducer(undefined, action)
expect(state.stages.develop.options.sourceMaps).toBe(`inline`)
expect(state.stages[`develop-html`].options.sourceMaps).toBe(undefined)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const _ = require(`lodash`)
import _ from "lodash"

module.exports = (
state = {
import { IGatsbyState, ActionsUnion, IPlugin } from "../types"

export const babelrcReducer = (
state: IGatsbyState["babelrc"] = {
stages: {
develop: {
plugins: [],
Expand Down Expand Up @@ -37,8 +39,8 @@ module.exports = (
},
},
},
action
) => {
action: ActionsUnion
): IGatsbyState["babelrc"] => {
switch (action.type) {
case `SET_BABEL_PLUGIN`: {
Object.keys(state.stages).forEach(stage => {
Expand All @@ -48,7 +50,7 @@ module.exports = (

const index = _.findIndex(
state.stages[stage].plugins,
p => p.name === action.payload.name
(plugin: IPlugin) => plugin.name === action.payload.name
)
// If the plugin already exists, merge the options.
// Otherwise, add it to the end.
Expand Down Expand Up @@ -76,7 +78,7 @@ module.exports = (

const index = _.findIndex(
state.stages[stage].presets,
p => p.name === action.payload.name
(plugin: IPlugin) => plugin.name === action.payload.name
)
// If the plugin already exists, merge the options.
// Otherwise, add it to the end.
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby/src/redux/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { lastAction } from "./last-action"
import { jobsV2Reducer } from "./jobsv2"
import { componentsReducer } from "./components"
import { componentDataDependenciesReducer } from "./component-data-dependencies"
import { babelrcReducer } from "./babelrc"

/**
* @property exports.nodesTouched Set<string>
Expand All @@ -38,7 +39,7 @@ module.exports = {
webpack: webpackReducer,
webpackCompilationHash: webpackCompilationHashReducer,
redirects: redirectsReducer,
babelrc: require(`./babelrc`),
babelrc: babelrcReducer,
schemaCustomization: require(`./schema-customization`),
themes: themesReducer,
logs: logReducer,
Expand Down
56 changes: 52 additions & 4 deletions packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,27 @@ export interface IGatsbyCompleteJobV2 {
inputPaths: InternalJobInterface["inputPaths"]
}

export interface IPlugin {
name: string
options: Record<string, any>
}

interface IBabelStage {
plugins: IPlugin[]
presets: IPlugin[]
options: {
cacheDirectory: boolean
sourceType: string
sourceMaps?: string
}
}

type BabelStageKeys =
| "develop"
| "develop-html"
| "build-html"
| "build-javascript"

export interface IGatsbyState {
program: IProgram
nodes: GatsbyNodes
Expand Down Expand Up @@ -179,10 +200,7 @@ export interface IGatsbyState {
redirects: IRedirect[]
babelrc: {
stages: {
develop: any // TODO
"develop-html": any // TODO
"build-html": any // TODO
"build-javascript": any // TODO
[key in BabelStageKeys]: IBabelStage
}
}
schemaCustomization: {
Expand Down Expand Up @@ -258,6 +276,36 @@ export type ActionsUnion =
| IEndJobV2Action
| IRemoveStaleJobV2Action
| IRemoveTemplateComponentAction
| ISetBabelPluginAction
| ISetBabelPresetAction
| ISetBabelOptionsAction

interface ISetBabelPluginAction {
type: `SET_BABEL_PLUGIN`
payload: {
stage: BabelStageKeys
name: IPlugin["name"]
options: IPlugin["options"]
}
}

interface ISetBabelPresetAction {
type: `SET_BABEL_PRESET`
payload: {
stage: BabelStageKeys
name: IPlugin["name"]
options: IPlugin["options"]
}
}

interface ISetBabelOptionsAction {
type: `SET_BABEL_OPTIONS`
payload: {
stage: BabelStageKeys
name: IPlugin["name"]
options: IPlugin["options"]
}
}

export interface ICreateJobV2Action {
type: `CREATE_JOB_V2`
Expand Down

0 comments on commit f3269e9

Please sign in to comment.