Skip to content

Commit fe1d2e1

Browse files
KornilLekoArts
andauthored
chore(gatsby): convert babel-loaders to typescript (#36318)
* Convert babel-loaders to ts * change imports and exports * import type Co-authored-by: Lennart <[email protected]>
1 parent f1a45e7 commit fe1d2e1

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

packages/gatsby/src/utils/babel-loader-helpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const getCustomOptions = (stage: Stage): IBabelStage["options"] => {
4444
*/
4545
const configItemsMemoCache = new Map()
4646

47-
interface ICustomOptions extends Record<string, unknown> {
47+
export interface ICustomOptions extends Record<string, unknown> {
4848
stage: Stage
4949
resourceQuery: string
5050
}

packages/gatsby/src/utils/babel-loader.js renamed to packages/gatsby/src/utils/babel-loader.ts

+24-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
const babelLoader = require(`babel-loader`)
2-
1+
import babelLoader from "babel-loader"
2+
import type { Compiler } from "webpack"
3+
import Babel, { ConfigItem } from "@babel/core"
34
import {
45
prepareOptions,
56
getCustomOptions,
67
mergeConfigItemOptions,
78
addRequiredPresetOptions,
9+
ICustomOptions,
810
} from "./babel-loader-helpers"
9-
10-
const { getBrowsersList } = require(`./browserslist`)
11+
import type { Stage } from "../commands/types"
12+
import { getBrowsersList } from "./browserslist"
1113

1214
/**
1315
* Gatsby's custom loader for webpack & babel
@@ -25,22 +27,27 @@ const { getBrowsersList } = require(`./browserslist`)
2527
* You can find documentation for the custom loader here: https://babeljs.io/docs/en/next/babel-core.html#loadpartialconfig
2628
*/
2729

30+
interface IBabelCustomLoader {
31+
custom: ICustomOptions
32+
loader: Record<string, unknown>
33+
}
34+
2835
const customOptionsCache = new Map()
2936
const configCache = new Map()
3037
const babelrcFileToCacheKey = new Map()
3138

32-
module.exports = babelLoader.custom(babel => {
39+
const customBabelLoader = babelLoader.custom(babel => {
3340
return {
3441
// Passed the loader options.
3542
customOptions({
36-
stage = `test`,
43+
stage = `test` as Stage,
3744
reactRuntime = `classic`,
3845
reactImportSource,
3946
isPageTemplate,
4047
resourceQuery,
4148
rootDir = process.cwd(),
4249
...options
43-
}) {
50+
}): IBabelCustomLoader {
4451
const customOptionsCacheKey = `${stage}-${isPageTemplate}-${resourceQuery}`
4552

4653
if (customOptionsCache.has(customOptionsCacheKey)) {
@@ -63,7 +70,7 @@ module.exports = babelLoader.custom(babel => {
6370
env: babel.getEnv(),
6471
}),
6572
sourceType: `unambiguous`,
66-
...getCustomOptions(stage),
73+
...getCustomOptions(stage as Stage),
6774
...options,
6875
},
6976
}
@@ -74,7 +81,7 @@ module.exports = babelLoader.custom(babel => {
7481
},
7582

7683
// Passed Babel's 'PartialConfig' object.
77-
config(partialConfig, { customOptions }) {
84+
config(partialConfig, { customOptions }): Babel.TransformOptions {
7885
const { stage, isPageTemplate, resourceQuery } = customOptions
7986
let configCacheKey = `${stage}-${isPageTemplate}-${resourceQuery}`
8087

@@ -134,7 +141,7 @@ module.exports = babelLoader.custom(babel => {
134141
reduxPresets.forEach(preset => {
135142
options.presets = mergeConfigItemOptions({
136143
items: options.presets,
137-
itemToMerge: preset,
144+
itemToMerge: preset as ConfigItem,
138145
type: `preset`,
139146
babel,
140147
})
@@ -143,7 +150,7 @@ module.exports = babelLoader.custom(babel => {
143150
reduxPlugins.forEach(plugin => {
144151
options.plugins = mergeConfigItemOptions({
145152
items: options.plugins,
146-
itemToMerge: plugin,
153+
itemToMerge: plugin as ConfigItem,
147154
type: `plugin`,
148155
babel,
149156
})
@@ -163,12 +170,16 @@ module.exports = babelLoader.custom(babel => {
163170
}
164171
})
165172

166-
module.exports.BabelConfigItemsCacheInvalidatorPlugin = class BabelConfigItemsCacheInvalidatorPlugin {
173+
export default customBabelLoader
174+
175+
export class BabelConfigItemsCacheInvalidatorPlugin {
176+
name: string
177+
167178
constructor() {
168179
this.name = `BabelConfigItemsCacheInvalidatorPlugin`
169180
}
170181

171-
apply(compiler) {
182+
apply(compiler: Compiler): void {
172183
compiler.hooks.invalid.tap(this.name, function (file) {
173184
const cacheKeysToInvalidate = babelrcFileToCacheKey.get(file)
174185

packages/gatsby/src/utils/webpack.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { builtinModules } from "module"
2525
import { shouldGenerateEngines } from "./engines-helpers"
2626
import { major } from "semver"
2727
import { ROUTES_DIRECTORY } from "../constants"
28-
const { BabelConfigItemsCacheInvalidatorPlugin } = require(`./babel-loader`)
28+
import { BabelConfigItemsCacheInvalidatorPlugin } from "./babel-loader"
2929

3030
const FRAMEWORK_BUNDLES = [`react`, `react-dom`, `scheduler`, `prop-types`]
3131

0 commit comments

Comments
 (0)