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"
3
4
import {
4
5
prepareOptions ,
5
6
getCustomOptions ,
6
7
mergeConfigItemOptions ,
7
8
addRequiredPresetOptions ,
9
+ ICustomOptions ,
8
10
} from "./babel-loader-helpers"
9
-
10
- const { getBrowsersList } = require ( ` ./browserslist` )
11
+ import type { Stage } from "../commands/types"
12
+ import { getBrowsersList } from " ./browserslist"
11
13
12
14
/**
13
15
* Gatsby's custom loader for webpack & babel
@@ -25,22 +27,27 @@ const { getBrowsersList } = require(`./browserslist`)
25
27
* You can find documentation for the custom loader here: https://babeljs.io/docs/en/next/babel-core.html#loadpartialconfig
26
28
*/
27
29
30
+ interface IBabelCustomLoader {
31
+ custom : ICustomOptions
32
+ loader : Record < string , unknown >
33
+ }
34
+
28
35
const customOptionsCache = new Map ( )
29
36
const configCache = new Map ( )
30
37
const babelrcFileToCacheKey = new Map ( )
31
38
32
- module . exports = babelLoader . custom ( babel => {
39
+ const customBabelLoader = babelLoader . custom ( babel => {
33
40
return {
34
41
// Passed the loader options.
35
42
customOptions ( {
36
- stage = `test` ,
43
+ stage = `test` as Stage ,
37
44
reactRuntime = `classic` ,
38
45
reactImportSource,
39
46
isPageTemplate,
40
47
resourceQuery,
41
48
rootDir = process . cwd ( ) ,
42
49
...options
43
- } ) {
50
+ } ) : IBabelCustomLoader {
44
51
const customOptionsCacheKey = `${ stage } -${ isPageTemplate } -${ resourceQuery } `
45
52
46
53
if ( customOptionsCache . has ( customOptionsCacheKey ) ) {
@@ -63,7 +70,7 @@ module.exports = babelLoader.custom(babel => {
63
70
env : babel . getEnv ( ) ,
64
71
} ) ,
65
72
sourceType : `unambiguous` ,
66
- ...getCustomOptions ( stage ) ,
73
+ ...getCustomOptions ( stage as Stage ) ,
67
74
...options ,
68
75
} ,
69
76
}
@@ -74,7 +81,7 @@ module.exports = babelLoader.custom(babel => {
74
81
} ,
75
82
76
83
// Passed Babel's 'PartialConfig' object.
77
- config ( partialConfig , { customOptions } ) {
84
+ config ( partialConfig , { customOptions } ) : Babel . TransformOptions {
78
85
const { stage, isPageTemplate, resourceQuery } = customOptions
79
86
let configCacheKey = `${ stage } -${ isPageTemplate } -${ resourceQuery } `
80
87
@@ -134,7 +141,7 @@ module.exports = babelLoader.custom(babel => {
134
141
reduxPresets . forEach ( preset => {
135
142
options . presets = mergeConfigItemOptions ( {
136
143
items : options . presets ,
137
- itemToMerge : preset ,
144
+ itemToMerge : preset as ConfigItem ,
138
145
type : `preset` ,
139
146
babel,
140
147
} )
@@ -143,7 +150,7 @@ module.exports = babelLoader.custom(babel => {
143
150
reduxPlugins . forEach ( plugin => {
144
151
options . plugins = mergeConfigItemOptions ( {
145
152
items : options . plugins ,
146
- itemToMerge : plugin ,
153
+ itemToMerge : plugin as ConfigItem ,
147
154
type : `plugin` ,
148
155
babel,
149
156
} )
@@ -163,12 +170,16 @@ module.exports = babelLoader.custom(babel => {
163
170
}
164
171
} )
165
172
166
- module . exports . BabelConfigItemsCacheInvalidatorPlugin = class BabelConfigItemsCacheInvalidatorPlugin {
173
+ export default customBabelLoader
174
+
175
+ export class BabelConfigItemsCacheInvalidatorPlugin {
176
+ name : string
177
+
167
178
constructor ( ) {
168
179
this . name = `BabelConfigItemsCacheInvalidatorPlugin`
169
180
}
170
181
171
- apply ( compiler ) {
182
+ apply ( compiler : Compiler ) : void {
172
183
compiler . hooks . invalid . tap ( this . name , function ( file ) {
173
184
const cacheKeysToInvalidate = babelrcFileToCacheKey . get ( file )
174
185
0 commit comments