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

ES6 Modules #121

Closed
wants to merge 3 commits into from
Closed

ES6 Modules #121

wants to merge 3 commits into from

Conversation

timrwood
Copy link
Contributor

This is a first pass at converting the source to ES6 modules.

It's still WIP, so tests don't pass yet. I just wanted to see if this was something you were interested in.

One of the benefits of ES6 modules is that you would be able to do something like this.

import { Body, World, Circle } from "p2";

var world = new World({ gravity : [0,-10] });
var body = new Body();
body.addShape(new Circle(10));
world.addBody(body);

You can transpile ES6 modules to ES5 using something like square/es6-module-transpiler.

npm install -g es6-module-transpiler
compile-modules convert -I src -o build/p2.es6.js src/p2.js

An example of how the output might look is here. https://gist.github.com/timrwood/5f96de256ec9768f70b5

Using the bundle format from the es6-module-transpiler also has some significant file savings over browserify.

After some rough tests, it was around half the un-gzipped size of the browserify build and 66% of the gzipped size. I didn't think it was worth comparing exact file sizes until tests are passing and poly-decomp is included.

Sorry for the brain dump, just wanted to get out all the talking points.

TODO:

  • Figure out a solution for importing poly-decomp
  • Figure out a solution for compiling to common-js for unit testing

@englercj
Copy link
Contributor

This is really cool, and since this is a compiled library kind of makes sense as well. What is the support like for runnning ES6 code on node (without transpile)?

@timrwood
Copy link
Contributor Author

During development it's a bit trickier, you either need to recompile to a temp dir every time you want to run tests, or use traceur to patch Node's require method.

For a published module, it's much easier. You just create a commonjs or umd build version and point the package.json#main to build/p2.js instead of src/p2.js. As long as the file is built before publishing to npm, it will behave the same as if it was authored in cjs.

@englercj
Copy link
Contributor

As long as the file is built before publishing to npm

That was the question, if it could be used without transpiling; to which the answer is "no" it sounds like. You have to polyfill/patch/transpile or something, not just use es6 and run.

@timrwood
Copy link
Contributor Author

You are able to use Traceur to require es6 modules without transpiling. See https://github.com/google/traceur-compiler/wiki/Using-Traceur-with-Node.js.

Most people who npm install p2.js would probably not want to be forced to use Traceur just to use p2, so providing an es5 umd build would be useful.

This es5 umd file is already being generated with browserify, so it would just be a matter of switching to the es6-module-transpiler for the build step.

@schteppe
Copy link
Owner

Definitely interesting! Will have a closer look after my vacation :)
Are there any other libraries using this es6 transpile/npm flow?

@timrwood
Copy link
Contributor Author

Ember is the most notable library authoring in es6 and compiling to es5, but it doesn't really target node/npm.

@jamiebuilds
Copy link

Hey @schteppe and @timrwood es6-module-transpiler and Traceur are great, you might also want to check out 6to5. It has a great set of tooling built around it and creates much nicer output. You can run es6 scripts with the 6to5-node cli (npm i -g 6to5), or you can enable it for require() calls via the require('6to5/register') hook. I'm a contributor for 6to5, so let me know if you need any help. You can also get support in our gitter room.

@jamiebuilds
Copy link

Oh we also have a bunch of module formatters for whatever environment you want to work in.

@wellcaffeinated
Copy link
Contributor

Very cool. Is there any difference in performance that you can measure?

Were there any specific difficulties in doing this, or was the process straight forward?

@jamiebuilds
Copy link

For very large code bases you'll likely notice a difference if you don't use some kind of watch task which can build only the changes. It looks like you're already using browserify so you can use the 6to5 transform with watchify and be good.

@timrwood timrwood closed this Aug 29, 2016
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

Successfully merging this pull request may close these issues.

None yet

5 participants