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

Support browserify #29

Closed
cburgmer opened this issue Apr 19, 2014 · 3 comments
Closed

Support browserify #29

cburgmer opened this issue Apr 19, 2014 · 3 comments

Comments

@cburgmer
Copy link
Contributor

This issue has already been mentioned in #22, however there still seems to be work to do.

The following three things should be tackled for the original packaging to work together with browserify:

The latter can be solved by the following solutions:

  1. Tell browserify to exclude the canvas dependency. The following is a hacky solution that works for me:

    $ npm install browserify
    $ touch canvasShim.js
    $ ./node_modules/.bin/browserify -e js/imagediff.js -s imagediff -r ./canvasShim.js:canvas --exclude ./canvasShim.js -o imagediff.browserified.js
    

    Or in human language: Package imagediff (-e) as a standalone package (-s) by replacing the canvas dependency with our mock (-r) that you shall not include (--exclude).

  2. Use canvas-browserify to always provide a Node.js package, but one that actually works in the browser.

    1. Either, completely remove the homegrown getCanvas() method, thus delegating this to canvas-browserify. This will however sacrifice the ability of the produced artifact to be usable in the browser out-of-the-box.
    2. Or, keep the getCanvas() implementation, but in the case of the browserify bundle use the implementation provided by canvas-browserify. We will produce code duplication, but then all cases should be covered.
  3. Ask around for other solutions.

I would currently favour 2.ii.

@ghost
Copy link

ghost commented Apr 19, 2014

You can add a browser field to package.json that will override local require calls with custom versions. For example

{
  "browser": {
    "canvas": "./canvas.js"
  }
}

and then in canvas.js:

module.exports = function (w, h) {
  var elem = document.createElement('canvas');
  elem.width = w;
  elem.height = h;
  return elem;
}

@cburgmer
Copy link
Contributor Author

Thanks. I didn't know about the per-file override.

That could refine options 2.i. and 2.ii.

@cesutherland
Copy link
Member

👍

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