rough-stuff / rough

Create graphics with a hand-drawn, sketchy, appearance
http://roughjs.com
MIT License
19.83k stars 613 forks source link

When using Jest and import a non-bundled roughjs modules, Jest throws an error #136

Closed GasimGasimzada closed 4 years ago

GasimGasimzada commented 4 years ago

Screen Shot 2020-02-05 at 12 25 19 PM

My test file only has one line of code:

import { randomSeed } from "roughjs/bin/math";

I am not certain on what the issue is but it seems like it is related to how these packages are bundled.

Also, this error occurs when import anything from the bin directory:

import rough from 'roughjs/bin/rough';

For importing the main rough object, using bundled import fixes the issue:

import rough from 'roughjs';

However, the line above does not work when using webpack (e.g within Create React App).

pshihn commented 4 years ago

Everything in the bin folder is in ES6 module format I'm assuming your react/jest bundling expects it to be in some other format?

GasimGasimzada commented 4 years ago

Yeah, maybe Jest doesn't use Babel (babel-node or something similar) to run the app. I will investigate to see if it is possible to do anything about it.

GasimGasimzada commented 4 years ago

@pshihn one question regarding this. Is it possible to get import rough from 'roughjs'; this one working in a normal webpack + babel based application? Currently, importing like that gives the following error:

TypeError: roughjsWEBPACK_IMPORTED_MODULE13default.a.canvas is not a function. (In 'roughjsWEBPACK_IMPORTED_MODULE13default.a.canvas(_this2.canvas)', 'roughjsWEBPACK_IMPORTED_MODULE13default.a.canvas' is undefined)

pshihn commented 4 years ago

Seems like webpack issues.

FWIW, roughjs does ship with a commonjs bundled version. Not sure how you;d include it : roughjs/bundled/rough.cjs.js

GasimGasimzada commented 4 years ago

It is solved. It can be closed now. Thank you for the help!