From f3269e98af72039f1c37bf117664e657eeeea670 Mon Sep 17 00:00:00 2001 From: Francesco Agnoletto Date: Mon, 18 May 2020 20:44:50 +0200 Subject: [PATCH] chore(gatsby): convert babelrc to typescript (#24115) * 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 * Fix typo Co-authored-by: Ward Peeters * chore(release): Publish - gatsby-admin@0.1.32 - gatsby-theme-blog-core@1.5.7 - gatsby-theme-blog@1.6.7 - gatsby-theme-notes@1.3.33 - gatsby-theme-ui-preset@0.0.22 - gatsby@2.21.32 * perf(gatsby): enable fast filters for `in` comparator (#24095) * chore(release): Publish - gatsby-admin@0.1.33 - gatsby-theme-blog-core@1.5.8 - gatsby-theme-blog@1.6.8 - gatsby-theme-notes@1.3.34 - gatsby-theme-ui-preset@0.0.23 - gatsby@2.21.33 Co-authored-by: Mikhail Novikov Co-authored-by: Ward Peeters Co-authored-by: Peter van der Zee <209817+pvdz@users.noreply.github.com> Co-authored-by: Peter van der Zee --- .../{babelrc.js.snap => babelrc.ts.snap} | 0 .../__tests__/{babelrc.js => babelrc.ts} | 12 ++-- .../redux/reducers/{babelrc.js => babelrc.ts} | 16 +++--- packages/gatsby/src/redux/reducers/index.js | 3 +- packages/gatsby/src/redux/types.ts | 56 +++++++++++++++++-- 5 files changed, 69 insertions(+), 18 deletions(-) rename packages/gatsby/src/redux/__tests__/__snapshots__/{babelrc.js.snap => babelrc.ts.snap} (100%) rename packages/gatsby/src/redux/__tests__/{babelrc.js => babelrc.ts} (93%) rename packages/gatsby/src/redux/reducers/{babelrc.js => babelrc.ts} (88%) diff --git a/packages/gatsby/src/redux/__tests__/__snapshots__/babelrc.js.snap b/packages/gatsby/src/redux/__tests__/__snapshots__/babelrc.ts.snap similarity index 100% rename from packages/gatsby/src/redux/__tests__/__snapshots__/babelrc.js.snap rename to packages/gatsby/src/redux/__tests__/__snapshots__/babelrc.ts.snap diff --git a/packages/gatsby/src/redux/__tests__/babelrc.js b/packages/gatsby/src/redux/__tests__/babelrc.ts similarity index 93% rename from packages/gatsby/src/redux/__tests__/babelrc.js rename to packages/gatsby/src/redux/__tests__/babelrc.ts index da19f8ba6f5cd..69413460f7fa2 100644 --- a/packages/gatsby/src/redux/__tests__/babelrc.js +++ b/packages/gatsby/src/redux/__tests__/babelrc.ts @@ -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`, () => { @@ -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) @@ -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) }) diff --git a/packages/gatsby/src/redux/reducers/babelrc.js b/packages/gatsby/src/redux/reducers/babelrc.ts similarity index 88% rename from packages/gatsby/src/redux/reducers/babelrc.js rename to packages/gatsby/src/redux/reducers/babelrc.ts index 2f83cf1714976..f6dfb704549b0 100644 --- a/packages/gatsby/src/redux/reducers/babelrc.js +++ b/packages/gatsby/src/redux/reducers/babelrc.ts @@ -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: [], @@ -37,8 +39,8 @@ module.exports = ( }, }, }, - action -) => { + action: ActionsUnion +): IGatsbyState["babelrc"] => { switch (action.type) { case `SET_BABEL_PLUGIN`: { Object.keys(state.stages).forEach(stage => { @@ -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. @@ -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. diff --git a/packages/gatsby/src/redux/reducers/index.js b/packages/gatsby/src/redux/reducers/index.js index 3492ba3050be6..afaa19a6bb1ea 100644 --- a/packages/gatsby/src/redux/reducers/index.js +++ b/packages/gatsby/src/redux/reducers/index.js @@ -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 @@ -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, diff --git a/packages/gatsby/src/redux/types.ts b/packages/gatsby/src/redux/types.ts index eb197377e390b..895184856689f 100644 --- a/packages/gatsby/src/redux/types.ts +++ b/packages/gatsby/src/redux/types.ts @@ -106,6 +106,27 @@ export interface IGatsbyCompleteJobV2 { inputPaths: InternalJobInterface["inputPaths"] } +export interface IPlugin { + name: string + options: Record +} + +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 @@ -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: { @@ -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`