Skip to content

Commit d85f420

Browse files
authored
refactor: use webpack builtin schema util (#1030)
1 parent 2b83a9c commit d85f420

File tree

5 files changed

+174
-88
lines changed

5 files changed

+174
-88
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
"node": "^18.20.0 || ^20.10.0 || >=22.0.0"
1111
},
1212
"dependencies": {
13-
"find-cache-dir": "^4.0.0",
14-
"schema-utils": "^4.0.0"
13+
"find-cache-dir": "^4.0.0"
1514
},
1615
"peerDependencies": {
1716
"@babel/core": "^7.12.0",

src/index.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const injectCaller = require("./injectCaller");
2626
const schema = require("./schema");
2727

2828
const { isAbsolute } = require("path");
29-
const validateOptions = require("schema-utils").validate;
3029

3130
function subscribe(subscriber, metadata, context) {
3231
if (context[subscriber]) {
@@ -54,17 +53,9 @@ function makeLoader(callback) {
5453
async function loader(source, inputSourceMap, overrides) {
5554
const filename = this.resourcePath;
5655

57-
let loaderOptions = this.getOptions();
58-
validateOptions(schema, loaderOptions, {
59-
name: "Babel loader",
60-
});
56+
let loaderOptions = this.getOptions(schema);
6157

6258
if (loaderOptions.customize != null) {
63-
if (typeof loaderOptions.customize !== "string") {
64-
throw new Error(
65-
"Customized loaders must be implemented as standalone modules.",
66-
);
67-
}
6859
if (!isAbsolute(loaderOptions.customize)) {
6960
throw new Error(
7061
"Customized loaders must be passed as absolute paths, since " +
@@ -99,17 +90,21 @@ async function loader(source, inputSourceMap, overrides) {
9990

10091
// Deprecation handling
10192
if ("forceEnv" in loaderOptions) {
102-
console.warn(
103-
"The option `forceEnv` has been removed in favor of `envName` in Babel 7.",
93+
this.emitWarning(
94+
new Error(
95+
"The option `forceEnv` has been removed in favor of `envName` in Babel 7.",
96+
),
10497
);
10598
}
10699
if (typeof loaderOptions.babelrc === "string") {
107-
console.warn(
108-
"The option `babelrc` should not be set to a string anymore in the babel-loader config. " +
109-
"Please update your configuration and set `babelrc` to true or false.\n" +
110-
"If you want to specify a specific babel config file to inherit config from " +
111-
"please use the `extends` option.\nFor more information about this options see " +
112-
"https://babeljs.io/docs/core-packages/#options",
100+
this.emitWarning(
101+
new Error(
102+
"The option `babelrc` should not be set to a string anymore in the babel-loader config. " +
103+
"Please update your configuration and set `babelrc` to true or false.\n" +
104+
"If you want to specify a specific babel config file to inherit config from " +
105+
"please use the `extends` option.\nFor more information about this options see " +
106+
"https://babeljs.io/docs/#options",
107+
),
113108
);
114109
}
115110

src/schema.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
2+
"title": "Babel Loader options",
23
"type": "object",
34
"properties": {
45
"cacheDirectory": {
5-
"oneOf": [
6+
"anyOf": [
67
{
78
"type": "boolean"
89
},
@@ -20,8 +21,18 @@
2021
"default": true
2122
},
2223
"customize": {
23-
"type": "string",
24+
"anyOf": [
25+
{
26+
"type": "null"
27+
},
28+
{
29+
"type": "string"
30+
}
31+
],
2432
"default": null
33+
},
34+
"metadataSubscribers": {
35+
"type": "array"
2536
}
2637
},
2738
"additionalProperties": true

test/options.test.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,148 @@ test("should interpret options given to the loader", async () => {
6363
const files = fs.readdirSync(outputDir);
6464
assert.ok(files.length > 0);
6565
});
66+
67+
test("should throw when options.metadataSubscribers is not an array", async () => {
68+
const config = Object.assign({}, globalConfig, {
69+
output: {
70+
path: context.directory,
71+
},
72+
module: {
73+
rules: [
74+
{
75+
test: /\.jsx?/,
76+
loader: babelLoader,
77+
exclude: /node_modules/,
78+
options: {
79+
metadataSubscribers: function subscriber() {},
80+
},
81+
},
82+
],
83+
},
84+
});
85+
const stats = await webpackAsync(config);
86+
const { errors } = stats.compilation;
87+
assert.deepEqual(errors.length, 1);
88+
const errorMessage = errors[0].message;
89+
assert.match(
90+
errorMessage,
91+
/ValidationError: Invalid options object\. Babel Loader has been initialized using an options object that does not match the API schema\./,
92+
);
93+
assert.match(errorMessage, /options\.metadataSubscribers should be an array/);
94+
});
95+
96+
test("should throw when options.customize is not a string", async () => {
97+
const config = Object.assign({}, globalConfig, {
98+
output: {
99+
path: context.directory,
100+
},
101+
module: {
102+
rules: [
103+
{
104+
test: /\.jsx?/,
105+
loader: babelLoader,
106+
exclude: /node_modules/,
107+
options: {
108+
customize: true,
109+
},
110+
},
111+
],
112+
},
113+
});
114+
const stats = await webpackAsync(config);
115+
const { errors } = stats.compilation;
116+
assert.deepEqual(errors.length, 1);
117+
const errorMessage = errors[0].message;
118+
assert.match(
119+
errorMessage,
120+
/ValidationError: Invalid options object\. Babel Loader has been initialized using an options object that does not match the API schema\./,
121+
);
122+
assert.match(
123+
errorMessage,
124+
/options\.customize should be one of these:\s null | string/,
125+
);
126+
});
127+
128+
test("should throw when options.customize is not an absolute path", async () => {
129+
const config = Object.assign({}, globalConfig, {
130+
output: {
131+
path: context.directory,
132+
},
133+
module: {
134+
rules: [
135+
{
136+
test: /\.jsx?/,
137+
loader: babelLoader,
138+
exclude: /node_modules/,
139+
options: {
140+
customize: "./node_modules/babel-loader-customized",
141+
},
142+
},
143+
],
144+
},
145+
});
146+
const stats = await webpackAsync(config);
147+
const { errors } = stats.compilation;
148+
assert.deepEqual(errors.length, 1);
149+
const errorMessage = errors[0].message;
150+
assert.match(
151+
errorMessage,
152+
/Error: Customized loaders must be passed as absolute paths, since babel-loader has no way to know what they would be relative to\./,
153+
);
154+
});
155+
156+
test("should warn when options.babelrc is a string", async () => {
157+
const config = Object.assign({}, globalConfig, {
158+
output: {
159+
path: context.directory,
160+
},
161+
module: {
162+
rules: [
163+
{
164+
test: /\.jsx?/,
165+
loader: babelLoader,
166+
exclude: /node_modules/,
167+
options: {
168+
babelrc: "./fixtures/babelrc",
169+
},
170+
},
171+
],
172+
},
173+
});
174+
const stats = await webpackAsync(config);
175+
const { warnings } = stats.compilation;
176+
assert.deepEqual(warnings.length, 1);
177+
const warningMessage = warnings[0].message;
178+
assert.match(
179+
warningMessage,
180+
/The option `babelrc` should not be set to a string anymore in the babel-loader config\./,
181+
);
182+
});
183+
184+
test("should warn when options.forceEnv is set", async () => {
185+
const config = Object.assign({}, globalConfig, {
186+
output: {
187+
path: context.directory,
188+
},
189+
module: {
190+
rules: [
191+
{
192+
test: /\.jsx?/,
193+
loader: babelLoader,
194+
exclude: /node_modules/,
195+
options: {
196+
forceEnv: "production",
197+
},
198+
},
199+
],
200+
},
201+
});
202+
const stats = await webpackAsync(config);
203+
const { warnings } = stats.compilation;
204+
assert.deepEqual(warnings.length, 1);
205+
const warningMessage = warnings[0].message;
206+
assert.match(
207+
warningMessage,
208+
/The option `forceEnv` has been removed in favor of `envName` in Babel 7\./,
209+
);
210+
});

yarn.lock

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,7 +1647,7 @@ __metadata:
16471647
languageName: node
16481648
linkType: hard
16491649

1650-
"@types/json-schema@npm:*, @types/json-schema@npm:^7.0.8, @types/json-schema@npm:^7.0.9":
1650+
"@types/json-schema@npm:*, @types/json-schema@npm:^7.0.8":
16511651
version: 7.0.15
16521652
resolution: "@types/json-schema@npm:7.0.15"
16531653
checksum: 97ed0cb44d4070aecea772b7b2e2ed971e10c81ec87dd4ecc160322ffa55ff330dace1793489540e3e318d90942064bb697cc0f8989391797792d919737b3b98
@@ -1881,20 +1881,6 @@ __metadata:
18811881
languageName: node
18821882
linkType: hard
18831883

1884-
"ajv-formats@npm:^2.1.1":
1885-
version: 2.1.1
1886-
resolution: "ajv-formats@npm:2.1.1"
1887-
dependencies:
1888-
ajv: ^8.0.0
1889-
peerDependencies:
1890-
ajv: ^8.0.0
1891-
peerDependenciesMeta:
1892-
ajv:
1893-
optional: true
1894-
checksum: 4a287d937f1ebaad4683249a4c40c0fa3beed30d9ddc0adba04859026a622da0d317851316ea64b3680dc60f5c3c708105ddd5d5db8fe595d9d0207fd19f90b7
1895-
languageName: node
1896-
linkType: hard
1897-
18981884
"ajv-keywords@npm:^3.5.2":
18991885
version: 3.5.2
19001886
resolution: "ajv-keywords@npm:3.5.2"
@@ -1904,17 +1890,6 @@ __metadata:
19041890
languageName: node
19051891
linkType: hard
19061892

1907-
"ajv-keywords@npm:^5.1.0":
1908-
version: 5.1.0
1909-
resolution: "ajv-keywords@npm:5.1.0"
1910-
dependencies:
1911-
fast-deep-equal: ^3.1.3
1912-
peerDependencies:
1913-
ajv: ^8.8.2
1914-
checksum: c35193940b853119242c6757787f09ecf89a2c19bcd36d03ed1a615e710d19d450cb448bfda407b939aba54b002368c8bff30529cc50a0536a8e10bcce300421
1915-
languageName: node
1916-
linkType: hard
1917-
19181893
"ajv@npm:^6.12.4, ajv@npm:^6.12.5":
19191894
version: 6.12.6
19201895
resolution: "ajv@npm:6.12.6"
@@ -1927,18 +1902,6 @@ __metadata:
19271902
languageName: node
19281903
linkType: hard
19291904

1930-
"ajv@npm:^8.0.0, ajv@npm:^8.9.0":
1931-
version: 8.16.0
1932-
resolution: "ajv@npm:8.16.0"
1933-
dependencies:
1934-
fast-deep-equal: ^3.1.3
1935-
json-schema-traverse: ^1.0.0
1936-
require-from-string: ^2.0.2
1937-
uri-js: ^4.4.1
1938-
checksum: bdf3d4c9f1d11e220850051ef4cd89346e951cfb933d6d41be36d45053c1092af1523ee6c62525cce567355caf0a4f4c19a08a93851649c1fa32b4a39b7c4858
1939-
languageName: node
1940-
linkType: hard
1941-
19421905
"ansi-escapes@npm:^5.0.0":
19431906
version: 5.0.0
19441907
resolution: "ansi-escapes@npm:5.0.0"
@@ -2021,7 +1984,6 @@ __metadata:
20211984
husky: ^8.0.3
20221985
lint-staged: ^13.2.3
20231986
prettier: ^3.0.0
2024-
schema-utils: ^4.0.0
20251987
webpack: ^5.93.0
20261988
peerDependencies:
20271989
"@babel/core": ^7.12.0
@@ -3337,13 +3299,6 @@ __metadata:
33373299
languageName: node
33383300
linkType: hard
33393301

3340-
"json-schema-traverse@npm:^1.0.0":
3341-
version: 1.0.0
3342-
resolution: "json-schema-traverse@npm:1.0.0"
3343-
checksum: 02f2f466cdb0362558b2f1fd5e15cce82ef55d60cd7f8fa828cf35ba74330f8d767fcae5c5c2adb7851fa811766c694b9405810879bc4e1ddd78a7c0e03658ad
3344-
languageName: node
3345-
linkType: hard
3346-
33473302
"json-stable-stringify-without-jsonify@npm:^1.0.1":
33483303
version: 1.0.1
33493304
resolution: "json-stable-stringify-without-jsonify@npm:1.0.1"
@@ -4087,13 +4042,6 @@ __metadata:
40874042
languageName: node
40884043
linkType: hard
40894044

4090-
"require-from-string@npm:^2.0.2":
4091-
version: 2.0.2
4092-
resolution: "require-from-string@npm:2.0.2"
4093-
checksum: a03ef6895445f33a4015300c426699bc66b2b044ba7b670aa238610381b56d3f07c686251740d575e22f4c87531ba662d06937508f0f3c0f1ddc04db3130560b
4094-
languageName: node
4095-
linkType: hard
4096-
40974045
"resolve-from@npm:^4.0.0":
40984046
version: 4.0.0
40994047
resolution: "resolve-from@npm:4.0.0"
@@ -4203,18 +4151,6 @@ __metadata:
42034151
languageName: node
42044152
linkType: hard
42054153

4206-
"schema-utils@npm:^4.0.0":
4207-
version: 4.2.0
4208-
resolution: "schema-utils@npm:4.2.0"
4209-
dependencies:
4210-
"@types/json-schema": ^7.0.9
4211-
ajv: ^8.9.0
4212-
ajv-formats: ^2.1.1
4213-
ajv-keywords: ^5.1.0
4214-
checksum: 26a0463d47683258106e6652e9aeb0823bf0b85843039e068b57da1892f7ae6b6b1094d48e9ed5ba5cbe9f7166469d880858b9d91abe8bd249421eb813850cde
4215-
languageName: node
4216-
linkType: hard
4217-
42184154
"semver@npm:^5.6.0":
42194155
version: 5.7.2
42204156
resolution: "semver@npm:5.7.2"
@@ -4648,7 +4584,7 @@ __metadata:
46484584
languageName: node
46494585
linkType: hard
46504586

4651-
"uri-js@npm:^4.2.2, uri-js@npm:^4.4.1":
4587+
"uri-js@npm:^4.2.2":
46524588
version: 4.4.1
46534589
resolution: "uri-js@npm:4.4.1"
46544590
dependencies:

0 commit comments

Comments
 (0)