mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

Using with Rollup

Open smddzcy opened this issue 8 years ago • 11 comments

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

screen shot 2018-09-02 at 23 10 22

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?

smddzcy avatar Sep 02 '18 22:09 smddzcy

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.

josdejong avatar Sep 03 '18 18:09 josdejong

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.

smddzcy avatar Sep 03 '18 20:09 smddzcy

Thanks. Maybe importing a bundle from dist works, so everything is included.

josdejong avatar Sep 03 '18 20:09 josdejong

For me, the following works:

  • install Math.js via npm
  • use the commonjs and node-resolve rollup plugins

Then you can do:

import * as math from 'mathjs';

benrodenhaeuser avatar Jun 28 '19 12:06 benrodenhaeuser

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?

josdejong avatar Jun 28 '19 13:06 josdejong

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 avatar Jul 23 '19 08:07 EliasHasle

@EliasHasle thanks for reporting. Can you try

import {create, all} from "mathjs/main/es6/";
const math = create(all);

explicitly pointing to the ES6 code?

josdejong avatar Jul 23 '19 19:07 josdejong

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

EliasHasle avatar Jul 24 '19 12:07 EliasHasle

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

josdejong avatar Aug 05 '19 20:08 josdejong

I can confirm that my issue is resolved with 6.0.4. Thanks! 😄

EliasHasle avatar Aug 08 '19 07:08 EliasHasle

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.

josdejong avatar Aug 08 '19 11:08 josdejong