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

Move to ES6, Rollup bundler for ESM/CJS bundles #128

Closed
wants to merge 1 commit into from
Closed

Move to ES6, Rollup bundler for ESM/CJS bundles #128

wants to merge 1 commit into from

Conversation

ShukantPal
Copy link

Changes

  • Move to ES6 classes, let/const
  • ESLint added & auto-corrected over the codebase
  • Move to rollup bundler to generate CommonJS, ESM bundles as well

@ShukantPal
Copy link
Author

#127

@ShukantPal
Copy link
Author

@Pomax I wanted to know if you were okay with some of the changes, specifically ESLint & Rollup?

@Pomax
Copy link
Owner

Pomax commented Aug 6, 2020

There should not be any need for babel, if we're redoing this as proper ES6 code: adding a type: "module" to the package.json should be enough. And if folks need their codebase converted to ES5 during a build step, then their own config should be able to take care of that just fine.

@@ -11,11 +11,11 @@ or read the souce (`./lib` for the library code, start at `index.js`).

## Use

`var Bezier = require('bezier-js')` and off you go.
`const { Bezier } = require('bezier-js')` and off you go.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should definitely be import { Bezier } from "bezier-js"; now =)


## Use in the browser

Load the toplevel `bezier.js` the same way you would any other browser script, using a `<script src="..."></script>` declaration.
Load `dist/bezier.js` the same way you would any other browser script, using a `<script src="..."></script>` declaration.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this should probably no longer be necessary, if we're using proper ES6 (both the browser and node supposed es modules)

"browserify": "^9.0.3",
"chai": "^3.2.0",
"chai-stats": "^0.3.0",
"eslint": "^7.6.0",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use prettier rather than eslint. I'd rather have an opinionated but zero-conf linter than a free form linter that requires its own .rc file to do something sensible.

"webpack": "^1.12.13"
"rollup": "^2.23.0",
"rollup-plugin-sourcemaps": "^0.6.2",
"rollup-plugin-terser": "^6.1.0"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to preemptively minify: if folks need minification, that would be a step their own config takes care of. Preemptive minification can in fact lead to worse results.

"uglify-js": "^2.4.17",
"webpack": "^1.12.13"
"rollup": "^2.23.0",
"rollup-plugin-sourcemaps": "^0.6.2",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly don't see much value in adding this: you don't minify code in your dev environment, so you don't need sourcemaps there, and prod should never load insanely huge debug-only files by default, so while an interesting idea, let's not add source mapping.

@@ -3,33 +3,33 @@
* and full commands, rather than relative coordinates
* and/or shortcut commands.
*/
function normalizePath(d) {
export function normalisePath (d) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a singleton file, exposing a single function: this can be a default export.

Suggested change
export function normalisePath (d) {
export default function normalisePath (d) {

@@ -1,33 +1,33 @@
var normalise = require("./normalise-svg.js");
import { normalisePath as normalise } from './normalise-svg';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the export made a default, this can just "stay" what it was:

Suggested change
import { normalisePath as normalise } from './normalise-svg';
import normalise from './normalise-svg';

Comment on lines +22 to +27
var term;
var matcher = new RegExp('[MLCQZ]', '');
var segment;
var values;
var segments = [];
var ARGS = { C: 6, Q: 4, L: 2, M: 2 };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ES6 should not be using var? ;)

import { Bezier } from './Bezier';

// math-inlining.
const abs = Math.abs;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, ES6 is better than ES5 here.

const { abs, cos, sin, etc, etc } = Math;

var utils = Bezier.getUtils();
var assert = require("chai").use(require("chai-stats")).assert;

const Bezier = require('../lib/bezier');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should definitely also become normal ES6 module code.

@Pomax
Copy link
Owner

Pomax commented Aug 6, 2020

I think we need to restart this but on a different footing. If bezier.js gets uplifted to ES6, then that should be fully committed to: no more require, nothing that generates ES5, and ES5 code patterns that have been replaced with better ones in ES6, replaced with those better ones. Also, for linting, let's use https://prettier.io, without any customization: whatever it considers good code is simply what the code will look like (and that can change from version to version, but that's fine).

Let's restart this but do this in steps:

  1. let's start a new es6 branch, with just the /lib and /test code, and uplift those files first, with an npm script for linting
    1. initial super-minimal rewrites just so that things use import/export without any other code changes
    2. then, switch things over to normal modern class syntax, without any code changes inside functions
    3. then, start rewriting function internals to ES6 syntax and patterns.
  2. switch the testing to jest, because it's still the weird ancient hand-written testing atm.
  3. once that work's done, let's add in rollup so that a browser "bundle" can be created. Note that minifcation is not necessary here: any sane server is going to serve that file with gzip, which is superior to any kind of JS based minification solution out there, and can actually perform worse on pre-minified code.

@ShukantPal
Copy link
Author

I'll redo this then I guess ;)

@ShukantPal ShukantPal closed this Aug 6, 2020
@Pomax
Copy link
Owner

Pomax commented Aug 7, 2020

Just remember to not "do everything": do step 1, file a PR, let's get that merged into https://github.com/Pomax/bezierjs/tree/es6 (I created a new branch for us to work against). Then we can iteratively improve that without ending up with one giant PR that is impossible to easily review =)

"Write small changes, file a PR, land the PR, write more small changes" is infinitely better than "write large changes, discover something fundamental invalidates all the work" ^_^;;

@Pomax Pomax mentioned this pull request Aug 7, 2020
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.

2 participants