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

Replace the use of 'require' by 'import' to reduce library side and improve performance. (Tree shaking) #1399

Open
3 tasks done
sadortun opened this issue Aug 27, 2021 · 1 comment
Labels
type:feature New feature or improvement of existing feature

Comments

@sadortun
Copy link
Contributor

sadortun commented Aug 27, 2021

New Feature / Enhancement Checklist

Current Limitation

Follow up of the discussion on #203

Currently, the SDK is imported globally, and the compilers cannot optimize the imports and remove unused code.

Firebase have a great explanation
https://firebase.googleblog.com/2021/08/the-new-firebase-js-sdk-now-ga.html?m=1

Feature / Enhancement Description

This would allow to reduce the compiled client side app by eliminating the unused code.

This would also allow to fix #203 by allowing to create instances the Parse library for those who need to interact with multiple Servers.

Example Use Case

Alternatives / Workarounds

3rd Party References

https://www.codingame.com/playgrounds/7463/tree-shaking-in-javascript-with-rollup

@mtrezza mtrezza added type:feature New feature or improvement of existing feature and removed type:improvement labels Dec 6, 2021
@dplewis
Copy link
Member

dplewis commented May 20, 2024

@sadortun @mtrezza I tried to replace require with import but ran into an issue with babel

I used import on EventEmitter.ts

import RNEventEmitter from 'react-native/Libraries/vendor/emitter/EventEmitter';
import { EventEmitter as NodeEventEmitter } from 'events';
let EventEmitter: any;

try {
  if (process.env.PARSE_BUILD === 'react-native') {
    EventEmitter = RNEventEmitter;
    EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  } else {
    EventEmitter = NodeEventEmitter;
  }
} catch (_) {
  // EventEmitter unavailable
}
module.exports = EventEmitter;
export default EventEmitter;

Babel outputs to lib/node.js, as you can see the dead code has been eliminated but the unused import _EventEmitter didn't get removed. Are there any babel experts that would like to have a look?

var _EventEmitter = _interopRequireDefault(require("react-native/Libraries/vendor/emitter/EventEmitter"));
var _events = require("events");
var EventEmitter;
try {
  EventEmitter = _events.EventEmitter;
} catch (_) {
  // EventEmitter unavailable
}
module.exports = EventEmitter;
var _default = EventEmitter;
exports.default = _default;

There are a few workarounds

  • Dynamic Imports with top level await?
  • Override the EventEmitter is already supported
  • Use an isomorphic EventEmitter library see discussion Isomorphic javascript client library. #1258
  • Create our own EventEmitter in memory (isomorphic support)
  • Fix unused imports for Babel

This is also an issue with the CryptoController.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature or improvement of existing feature
Projects
None yet
Development

No branches or pull requests

3 participants