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

custom_relational_functions example not working #3332

Open
DavidMulder0 opened this issue Dec 4, 2024 · 2 comments
Open

custom_relational_functions example not working #3332

DavidMulder0 opened this issue Dec 4, 2024 · 2 comments

Comments

@DavidMulder0
Copy link

Here is a JSFiddle with MathJS 14 using a direct copy of the custom_relation_functions code:

https://jsfiddle.net/uonxj72q/

The output shows that the custom relation functions do not seem to be used. (E.g. when I put a debugger into them).

Oddly enough this seems to be related somehow to how the browser bundler is working, because the same code is working for me when using Vite to bundle it from node_modules.

Been trying to figure out what's going on for a couple of hours now, and frankly hitting a wall 😟.

@DavidMulder0
Copy link
Author

(Btw, just a workaround in case someone does encounter this, you can directly call the factory metohd with an empty context to overwrite the functions on the instance, but I am not sure what side effects that might have.)

@josdejong
Copy link
Owner

Hm yeah that is confusing, sorry. Let me try to explain what's going on.

The npm library contains a static function create(factories, config), and a collection of factory functions all and individual factories. When you create a mathjs instance like math = create(factories, config), it will contain a function math.create(config) which is bound to the factories of the original instance. So, it is not the same as the static version and can only create a new instance with the same factories and a changed configuration. Probably a better name for this function would be math.cloneInstance(config) or something like that.

Now, the browser bundle is not the same as the npm library: it exports a mathjs instance. So, the not the static function create(factories, config) and factory functions all, but the bound function math.create(config). The browser bundle doesn't have the collection all like the npm import does (if you print all in your jsfiddle, you will see it is undefined).

So in short: it's not possible to do with the browser bundle right now. It would be good to think through if we can address this issue.

I did a quick test: if we change the contents of defaultInstance.js to the following:

import * as math from './index.js'

export default math

And then create the browser bundle, your jsfiddle example works as expected. The browser bundle grows from 183 KB to 202 KB when zipped: it is larger because it now exports all named factories.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants