react-native-community / discussions-and-proposals

Discussions and proposals related to the main React Native project
https://reactnative.dev
1.66k stars 125 forks source link

Support "buildless" vanilla js / import maps #639

Open Risto-Stevcev opened 1 year ago

Risto-Stevcev commented 1 year ago

Introduction

Support import maps with react native (see browser support).

Details

With the latest improvements in Node.js and ES6 modules, there 's a new way of developing javascript projects that can avoid a lot of the complexity that often comes with building and setting up a modern javascript project.

The idea is to use import maps with Node.js for your application, so that if you only need vanilla js, you can avoid build steps entirely. The import maps are usually mapped either to node_modules as a relative path, or to a url from unpkg/jspm. Note that only npm modules that support ESM work, but there are a lot that do and it will continue to grow.

Discussion points

huntie commented 1 year ago

Hi @Risto-Stevcev, this is interesting! What's the main use case you see this enabling for a React Native app today?


Note for Metro maintainers: WICG/import-maps is distinct from Package Imports — the latter is considered by runtimes/bundlers interpreting package.json only and supports private import specifiers with a # prefix.

Risto-Stevcev commented 1 year ago

Hi @huntie,

I'm really excited about import maps because I think it provides a unique and low-effort way of reusing, securing, or even optimizing code.

For example, say you make a DateTimeRangePicker that uses this DateTimePicker library for React Native. You just implement your standard implementation as usual, using the DateTimePicker for the to/from dates, and validating that the range is acceptable (ie: the to date is earlier than the from date), and ship it. There's a problem though, because what if:

  1. DateTimePicker doesn't have the right UX for users of your library
    • The magic with import maps from a library consumer is that users of the library can now easily swap the internally used DateTimePicker for your own custom DateTimePicker that better matches your UX/business needs, fixing the picker and reusing everything else.
  2. DateTimePicker has a security vulnerability
    • You don't have to wait upstream for someone to patch it, blocking your progress -- just use import maps to shim in your security fix.
  3. DateTimePicker isn't as fast as it could be
    • It maybe uses a slow algorithm, which you could patch. Or even replace a slower library with a faster one that uses a similar API (ie: underscore -> lodash)

So it's not just react native -- I think this is a low effort way of potentially solving a lot of challenges because import maps allow you to contextualize the imports, effectively allowing you to dependency inject your own implementation of any module without having to fork your own custom copy of the library.