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

Fix: Workaround for transpilation failure with create-react-app #9093

Merged
merged 2 commits into from Jan 20, 2023

Conversation

SuperSodaSea
Copy link
Member

@SuperSodaSea SuperSodaSea commented Jan 20, 2023

Description of change

Users using create-react-app + PixiJS are continuously facing problem with transpilation (#8733, #8824 and #9064). Let me explain the details.

In #8698 I added code below in ImageBitmapResource.constructor:

if (typeof source === 'string')
{
super(ImageBitmapResource.EMPTY);
this.url = source;
}
else
{
super(source);
this.url = null;
}

This looks good, and when releasing it will be transpiled by esbuild:

var __super = (...args) => {
  super(...args);
};
if (typeof source === "string") {
  __super(ImageBitmapResource.EMPTY);
  this.url = source;
} else {
  __super(source);
  this.url = null;
}

(Source code of related logic in esbuild here.)

Until then, everything is fine, but users usually need to do their own transpilation, and here is where it can cause problems. Noticing that __super = (...args) => { super(...args); } is an arrow function with railing comma in parameters, and have a super call in its body. Unfortunately, due to limitation of transpilation, it is impossible to transpile it without transpiling the whole class (using polyfills like _createClass in Babel instead of native ES6 classes).

This situation can happen when using the default setting of create-react-app, or @babel/preset-env with target since 2017. Previously Babel wrongly transpiles this into:

var _supercall = function () {
  return _supercall(...arguments);
};
var __super = function () {
  _supercall(...arguments);
};
if (typeof source === "string") {
  __super(ImageBitmapResource.EMPTY);
  this.url = source;
} else {
  __super(source);
  this.url = null;
}

This will lead to infinite recursion. So I opened issue babel/babel#15148 and PR babel/babel#15163 to fix this bug in Babel. Now it will report error when transpiling:

SyntaxError: .../ImageBitmapResource.mjs: When using '@babel/plugin-transform-parameters', it's not possible to compile `super()` in an arrow function with default or rest parameters without compiling classes.
Please add '@babel/plugin-transform-classes' to your Babel configuration.

Users will get an explicit error instead of a confusing infinite recursion, but it is still quite annoying. So in this PR I just move super() out of if(), solving this issue.

Pre-Merge Checklist

  • Lint process passed (npm run lint)
  • Tests passed (npm run test)

@SuperSodaSea SuperSodaSea added this to the v7.1.1 milestone Jan 20, 2023
@SuperSodaSea SuperSodaSea marked this pull request as ready for review January 20, 2023 17:36
@SuperSodaSea SuperSodaSea changed the title Fix: Workaround for transpilation failure Fix: Workaround for transpilation failure with create-react-app Jan 20, 2023
Copy link
Member

@bigtimebuddy bigtimebuddy left a comment

Choose a reason for hiding this comment

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

Good fix!

@bigtimebuddy bigtimebuddy added the ✅ Ready To Merge Helpful when issues are in the queue waiting to get merged. This means the PR is completed and has t label Jan 20, 2023
@bigtimebuddy bigtimebuddy merged commit cf5c00c into dev Jan 20, 2023
@bigtimebuddy bigtimebuddy deleted the fix/super-transpilation branch January 20, 2023 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✅ Ready To Merge Helpful when issues are in the queue waiting to get merged. This means the PR is completed and has t
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants