-
Notifications
You must be signed in to change notification settings - Fork 142
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
Non-serializable objects in macro config should be a build error #1082
Comments
Ah, so the bug here is actually that this should have errored and told you that you can only put JSON-serializable things in the macro config. It's probably not really giving you a RegExp in the browser anyway -- it's going to come out the other side as a string. There's no general purpose way to take an object inside Node and inline it into the browser source code so that it will rehydrate exactly the same. You can still have regular expressions in your config, but they need to be strings, and then your runtime code would do That said, I don't think this specific example really needs macros at all, because it's probably not trying to alter the way your code builds. The big reason to use macros is when your addon needs to include different code based on the config. Here you're probably just going to use this list at runtime, with the same implementation regardless of the values. So a fully runtime API would be easier for both you and your users. For example, Alternatively, |
Ahhh, thank you, I guess that makes sense. Actually, @sentry/ember already allows to configure this also via the On the other hand, it would def. make sense to throw an error then when trying to embed a regex or so as a macro, as it basically will silently not work as expected (and is not so clear that it is not allowed when you do not understand how exactly that works behind the scenes). Thank you for the explanation! |
We could have an option here - https://github.com/yahoo/serialize-javascript |
Either something like this, or else I can make a PR adding a check like this to the export default class MacrosConfig {
private internalSetConfig(fromPath: string, packageName: string | undefined, config: object) {
if (!isSerializable(config)) {
throw new Error(
`[Embroider:MacrosConfig] the given config from '${fromPath}' for packageName '${packageName}' is not JSON serializable.`
);
}
// continue...
}
function isSerializable(obj: object): boolean {
if (isScalar(obj)) {
return true;
}
if (Array.isArray(obj)) {
return !obj.some((arrayItem: any) => !isSerializable(arrayItem));
}
if (isPlainObject(obj)) {
for (let property in obj) {
let value = obj[property] as any;
if (!isSerializable(value)) {
return false;
}
}
return true;
}
return false;
}
function isScalar(val: any) {
return (
typeof val === 'undefined' ||
typeof val === 'string' ||
typeof val === 'boolean' ||
typeof val === 'number' ||
val === null
);
}
function isPlainObject(obj: any): obj is Record<string, any> {
return typeof obj === 'object' && obj.constructor === Object && obj.toString() === '[object Object]';
} |
I get this warning message:
When I use a regex as macro config. So basically this:
Will trigger the warning. When I replace the array with an array of strings, it works. I can also reproduce it by just using a regex anywhere in the config, like:
Or when I pass a built regex (e.g.
myfield: new RegExp("regex here")
).The text was updated successfully, but these errors were encountered: