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

Question about a generated closures. #219

Closed
Mugen87 opened this issue Sep 24, 2019 · 9 comments
Closed

Question about a generated closures. #219

Mugen87 opened this issue Sep 24, 2019 · 9 comments
Labels

Comments

@Mugen87
Copy link

Mugen87 commented Sep 24, 2019

We at the three.js team try move our code base to ES6 classes and want to use Bublé to generate ES2015 code. When inspecting the output, we noticed that Bublé produces a closure around class definitions iff the class is inherited from another one. Consider this (stripped-down) code:

class BoxGeometry extends Geometry {

	constructor( width, height, depth, widthSegments, heightSegments, depthSegments ) {

		super();

		this.type = 'BoxGeometry';

	}

}

Bublé produces:

var BoxGeometry = /*@__PURE__*/(function (Geometry) {
	function BoxGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) {

		Geometry.call(this);

		this.type = 'BoxGeometry';

	}

	if ( Geometry ) BoxGeometry.__proto__ = Geometry;
	BoxGeometry.prototype = Object.create( Geometry && Geometry.prototype );
	BoxGeometry.prototype.constructor = BoxGeometry;

	return BoxGeometry;
}(Geometry));

Is there a way to avoid the closure around BoxGeometry (see mrdoob/three.js#17276 (comment))?

@mrdoob
Copy link

mrdoob commented Sep 24, 2019

Ideally we would like it to produce this:

function BoxGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) {

	Geometry.call( this );

	this.type = 'BoxGeometry';

}

BoxGeometry.prototype = Object.create( Geometry.prototype );
BoxGeometry.prototype.constructor = BoxGeometry;

@mourner
Copy link
Collaborator

mourner commented Sep 24, 2019

@mrdoob @Mugen87 as far as I know, the closure is necessary in case of subclassing because superclass reference can otherwise change, leading to broken super calls. Consider this:

class Foo {}
class Bar extends Foo {}
Foo = null;
new Bar();

This code will throw if transpiled without a closure. TypeScript behaves the same way (adding a closure), and Babel probably is too.

if ( Geometry ) BoxGeometry.__proto__ = Geometry;

This line is necessary to inherit static methods.

Checks for whether superclass is falsy are necessary for compatibility, because ES allows code like class Foo extends null {}.

@mrdoob
Copy link

mrdoob commented Sep 24, 2019

@mourner The ideal output I shared is the current pattern the whole library uses. We were hoping to adopt ES6 classes, and be able to produce the same ES5 build (or as close as possible).

@mourner
Copy link
Collaborator

mourner commented Sep 24, 2019

@mrdoob yeah, I see, although it's unlikely to be achievable with Buble without some custom transforms — we can't sacrifice compatibility for a few more bytes that are likely mitigated by gzip.

BTW I'm wondering what you think about dropping IE11 support — any rough timelines? That would produce a lean build and make transpilation unnecessary.

@mrdoob
Copy link

mrdoob commented Sep 24, 2019

we can't sacrifice compatibility for a few more bytes that are likely mitigated by gzip.

Yeah I understand. FWIW, is not about bytes, but about avoiding side effects. If the ES5 code remains the same I can go to sleep wihtout worrying that in a random config somewhere the extra closure is not changing something or affecting performance somehow.

BTW I'm wondering what you think about dropping IE11 support — any rough timelines? That would produce a lean build and make transpilation unnecessary.

The three.module.js build already dropped IE11 support (unless the user transpiles it that is).

@mrdoob
Copy link

mrdoob commented Sep 24, 2019

@mourner now that you're here... are you planning on adding a "module" export to Earcut's package.json? 😇

@mourner
Copy link
Collaborator

mourner commented Sep 24, 2019

@mrdoob oh yes :) Let's discuss there: mapbox/earcut#126

@mrdoob
Copy link

mrdoob commented Jul 27, 2020

Ended up doing a rollup transform to cleans this up: mrdoob/three.js#19934

@mrdoob
Copy link

mrdoob commented Jul 27, 2020

This can be closed.

@mourner mourner closed this as completed Jul 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants