Using with Rollup
We're currently having problems using this library with Rollup. Here's the error:

And my Rollup config:
export default {
external: ['react', 'react-dom'],
input: 'src/index.ts',
output: [
{
file: pkg.main,
name: 'index',
format: 'umd',
sourcemap: false
},
{
file: pkg.module,
format: 'es',
sourcemap: false
}
],
watch: {
include: 'src/**'
},
plugins: [
// Node built-in fns support
builtins(),
postcss({
extensions: ['.css'],
plugins: [url({ url: 'inline' })],
inject: false
}),
// Allow json resolution
json(),
// Compile TypeScript files
typescript({
useTsconfigDeclarationDir: true,
abortOnError: false,
check: false
}),
// Allow node_modules resolution, so you can use 'external' to control
// which external modules to include in the bundle
// https://github.com/rollup/rollup-plugin-node-resolve#usage
resolve({
jsnext: true,
main: true
}),
// Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
commonjs({
namedExports: {
'../adaptableblotter/node_modules/react/index.js': [
'Children',
'Component',
'PropTypes',
'createElement',
'cloneElement'
],
'../adaptableblotter/node_modules/react-dom/index.js': ['render']
},
sourceMap: false
}),
// Resolve source maps to the original source
// sourceMaps(),
]
}
Am I doing something wrong or is it related to your exports?
Thanks for reporting.
I don't have much experience with rollup. I just spend 20 minutes trying to get a simple rollup config working but I can't even get a function imported and bundled from lodash (messing around with rollup-plugin-node-resolve). I'm sorry.
I would like to try in a simple stand alone setup without TypeScript to be sure whether the issue originates in TypeScript or rollup.
It's probably because of your custom lazy loading / importing logic. Rollup is not very good at handling things automagically. Thanks for taking the time to look into this, I'll try fixing it in my spare time as well.
Thanks. Maybe importing a bundle from dist works, so everything is included.
For me, the following works:
- install Math.js via npm
- use the
commonjsandnode-resolverollup plugins
Then you can do:
import * as math from 'mathjs';
Thanks for your input. It can very well be that this issue is fixed in v6, which moved from commonjs to ES6 modules. @smddzcy can you give v6 a try?
I fail to import mathjs with Rollup too. (I am posting my problem here, as the name of the issue matches, and the issue is open and otherwise not alive.)
Reported problems: For some scripts (typed-function.js among them) this is undefined, and (probably as a consequence) typed-function does not export. My Rollup config applies rollup-plugin-node-resolve and exports in UMD format.
I have tried multiple ways of importing, including:
import {create, FibonacciHeapDependencies} from "mathjs";
const {FibonacciHeap} = create({FibonacciHeapDependencies});
import {create, all} from "mathjs";
const math = create(all);
import * as math from "mathjs";
const FibonacciHeap = math.type.FibonacciHeap;
All of them fail.
Update: Looks like the error is using import on a module (typed-function) that does not export. Apparently, one is supposed to require instead. Or modify the module so that it exports.
@EliasHasle thanks for reporting. Can you try
import {create, all} from "mathjs/main/es6/";
const math = create(all);
explicitly pointing to the ES6 code?
I now tried:
import {create, FibonacciHeapDependencies} from "mathjs/main/ES6";
const {FibonacciHeap} = create({FibonacciHeapDependencies});
with rollup-plugin-commonjs disabled again, and got the samme errors as before. Enabling the commonjs plugin solves the problem, as before. As far as I know, the purpose of the commonjs plugin is to enable imports from commonjs modules, and in practice I think it transpiles the imports to requires. It will make a very bloated bundle.
Since for now I only need the FibonacciHeap, I will use https://github.com/gwtw/ts-fibonacci-heap instead (with similar/identical API). But I am interested in benefiting from mathjs later, preferably without having to bundle the whole library. :-)
Update: I think I misunderstood the behavior of the commonjs plugin. I solved building only typed-function in https://github.com/josdejong/typed-function/issues/18#issuecomment-515750292
This issue is most likely fixed in v6.0.4 (see #1554), can you give this a try @EliasHasle? There should be no need to point to mathjs/main/es6 (which internally is changed to mathjs/main/esm now).
EDIT: only relevant for Elias
I can confirm that my issue is resolved with 6.0.4. Thanks! 😄
That's great to hear, thanks for the feedback.
Since multiple people report they can successfully use mathjs in their rollup projects I'll close this issue now.