Skip to content

Commit 9133386

Browse files
committed
Improve serializability check & error message
1 parent bf87ee1 commit 9133386

2 files changed

Lines changed: 31 additions & 25 deletions

File tree

packages/babel/src/index.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,26 @@ const returnObject = () => {
102102
return {};
103103
};
104104

105-
function isSerializable(value) {
106-
if (value === null) {
107-
return true;
108-
} else if (Array.isArray(value)) {
109-
return value.every(isSerializable);
110-
}
111-
switch (typeof value) {
112-
case 'string':
113-
case 'number':
114-
case 'boolean':
115-
return true;
116-
case 'object':
117-
return Object.keys(value).every((key) => isSerializable(value[key]));
118-
default:
119-
return false;
105+
function findNonSerializableOption(obj) {
106+
const isSerializable = (value) => {
107+
if (value === null) return true;
108+
if (Array.isArray(value)) return value.every(isSerializable);
109+
switch (typeof value) {
110+
case 'string':
111+
case 'number':
112+
case 'boolean':
113+
return true;
114+
case 'object':
115+
return Object.values(value).every(isSerializable);
116+
default:
117+
return false;
118+
}
119+
};
120+
121+
for (const key of Object.keys(obj)) {
122+
if (!isSerializable(obj[key])) return key;
120123
}
124+
return null;
121125
}
122126

123127
function createBabelInputPluginFactory(customCallback = returnObject) {
@@ -159,12 +163,16 @@ function createBabelInputPluginFactory(customCallback = returnObject) {
159163
}
160164

161165
if (parallel) {
162-
const parallelAllowed =
163-
isSerializable(babelOptions) && !overrides?.config && !overrides?.result;
164-
165-
if (!parallelAllowed) {
166+
if (overrides?.config) {
167+
throw new Error('Cannot use "parallel" mode with a custom "config" override.');
168+
}
169+
if (overrides?.result) {
170+
throw new Error('Cannot use "parallel" mode with a custom "result" override.');
171+
}
172+
const nonSerializableKey = findNonSerializableOption(babelOptions);
173+
if (nonSerializableKey) {
166174
throw new Error(
167-
'Cannot use "parallel" mode alongside custom overrides or non-serializable Babel options.'
175+
`Cannot use "parallel" mode because the "${nonSerializableKey}" option is not serializable.`
168176
);
169177
}
170178

packages/babel/test/as-input-plugin.mjs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -655,9 +655,7 @@ test('throws when using parallel with non-serializable babel options', async ()
655655
}
656656
]
657657
})
658-
).rejects.toThrow(
659-
/Cannot use "parallel" mode alongside custom overrides or non-serializable Babel options/
660-
);
658+
).rejects.toThrow(/Cannot use "parallel" mode because the "plugins" option is not serializable/);
661659
});
662660

663661
test('throws when using parallel with config override', () => {
@@ -670,7 +668,7 @@ test('throws when using parallel with config override', () => {
670668
});
671669

672670
expect(() => customBabelPlugin({ babelHelpers: 'bundled', parallel: true })).toThrow(
673-
/Cannot use "parallel" mode alongside custom overrides or non-serializable Babel options/
671+
/Cannot use "parallel" mode with a custom "config" override/
674672
);
675673
});
676674

@@ -684,6 +682,6 @@ test('throws when using parallel with result override', () => {
684682
});
685683

686684
expect(() => customBabelPlugin({ babelHelpers: 'bundled', parallel: true })).toThrow(
687-
/Cannot use "parallel" mode alongside custom overrides or non-serializable Babel options/
685+
/Cannot use "parallel" mode with a custom "result" override/
688686
);
689687
});

0 commit comments

Comments
 (0)