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

Using with Rollup #1232

Closed
smddzcy opened this issue Sep 2, 2018 · 11 comments
Closed

Using with Rollup #1232

smddzcy opened this issue Sep 2, 2018 · 11 comments

Comments

@smddzcy
Copy link

smddzcy commented Sep 2, 2018

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?

@josdejong
Copy link
Owner

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.

@smddzcy
Copy link
Author

smddzcy commented Sep 3, 2018

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.

@josdejong
Copy link
Owner

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

@benrodenhaeuser
Copy link

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';

@josdejong
Copy link
Owner

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?

@EliasHasle
Copy link

EliasHasle commented Jul 23, 2019

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.

@josdejong
Copy link
Owner

@EliasHasle thanks for reporting. Can you try

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

explicitly pointing to the ES6 code?

@EliasHasle
Copy link

EliasHasle commented Jul 24, 2019

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 josdejong/typed-function#18 (comment)

@josdejong
Copy link
Owner

josdejong commented Aug 5, 2019

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

@EliasHasle
Copy link

EliasHasle commented Aug 8, 2019

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

@josdejong
Copy link
Owner

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.

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

4 participants