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): convert babel-loaders to typescript #36318

Merged
merged 4 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Convert babel-loaders to ts
  • Loading branch information
Kornil committed Aug 4, 2022
commit 34a0bbeb8e84b4802ae96fe6c620e3219d30d379
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/babel-loader-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const getCustomOptions = (stage: Stage): IBabelStage["options"] => {
*/
const configItemsMemoCache = new Map()

interface ICustomOptions extends Record<string, unknown> {
export interface ICustomOptions extends Record<string, unknown> {
stage: Stage
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const babelLoader = require(`babel-loader`)

import { Compiler } from "webpack"
import Babel, { ConfigItem } from "@babel/core"

import {
prepareOptions,
getCustomOptions,
mergeConfigItemOptions,
addRequiredPresetOptions,
ICustomOptions,
} from "./babel-loader-helpers"
import { Stage } from "../commands/types"

const { getBrowsersList } = require(`./browserslist`)

Expand All @@ -25,6 +30,11 @@ const { getBrowsersList } = require(`./browserslist`)
* You can find documentation for the custom loader here: https://babeljs.io/docs/en/next/babel-core.html#loadpartialconfig
*/

interface IBabelCustomLoader {
custom: ICustomOptions
loader: Record<string, unknown>
}

const customOptionsCache = new Map()
const configCache = new Map()
const babelrcFileToCacheKey = new Map()
Expand All @@ -33,14 +43,14 @@ module.exports = babelLoader.custom(babel => {
return {
// Passed the loader options.
customOptions({
stage = `test`,
stage = `test` as Stage,
reactRuntime = `classic`,
reactImportSource,
isPageTemplate,
resourceQuery,
rootDir = process.cwd(),
...options
}) {
}): IBabelCustomLoader {
const customOptionsCacheKey = `${stage}-${isPageTemplate}-${resourceQuery}`

if (customOptionsCache.has(customOptionsCacheKey)) {
Expand All @@ -63,7 +73,7 @@ module.exports = babelLoader.custom(babel => {
env: babel.getEnv(),
}),
sourceType: `unambiguous`,
...getCustomOptions(stage),
...getCustomOptions(stage as Stage),
...options,
},
}
Expand All @@ -74,7 +84,7 @@ module.exports = babelLoader.custom(babel => {
},

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

Expand Down Expand Up @@ -134,7 +144,7 @@ module.exports = babelLoader.custom(babel => {
reduxPresets.forEach(preset => {
options.presets = mergeConfigItemOptions({
items: options.presets,
itemToMerge: preset,
itemToMerge: preset as ConfigItem,
type: `preset`,
babel,
})
Expand All @@ -143,7 +153,7 @@ module.exports = babelLoader.custom(babel => {
reduxPlugins.forEach(plugin => {
options.plugins = mergeConfigItemOptions({
items: options.plugins,
itemToMerge: plugin,
itemToMerge: plugin as ConfigItem,
type: `plugin`,
babel,
})
Expand All @@ -163,12 +173,14 @@ module.exports = babelLoader.custom(babel => {
}
})

module.exports.BabelConfigItemsCacheInvalidatorPlugin = class BabelConfigItemsCacheInvalidatorPlugin {
export class BabelConfigItemsCacheInvalidatorPlugin {
name: string

constructor() {
this.name = `BabelConfigItemsCacheInvalidatorPlugin`
}

apply(compiler) {
apply(compiler: Compiler): void {
compiler.hooks.invalid.tap(this.name, function (file) {
const cacheKeysToInvalidate = babelrcFileToCacheKey.get(file)

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 @@ -25,7 +25,7 @@ import { builtinModules } from "module"
import { shouldGenerateEngines } from "./engines-helpers"
import { major } from "semver"
import { ROUTES_DIRECTORY } from "../constants"
const { BabelConfigItemsCacheInvalidatorPlugin } = require(`./babel-loader`)
import { BabelConfigItemsCacheInvalidatorPlugin } from "./babel-loader"

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

Expand Down