Better compatibility with ReactJS Packages #786
Labels
priority-1-high
Should be resolved ASAP.
type-feature
About new capabilities
type-investigation
About research and gathering information
TLDR
We want users to be able to use ReactJS components from NPM in a similar level of complexity to a
pip install
. This will require developing ReactPy Package Manager. This tool will pull packages down using NPM, statically build/analyze them, then auto-generate a Python file containing the usable interface.For users that really want to dive into the nitty-gritty of writing their components in JavaScript, we will re-engineer the JavaScript API to leverage any standard JavaScript tooling. Ideally, it should feel exactly like writing a normal ReactJS component.
Current Situation
Right now there's a few factors that make using ReactJS components a bit awkward/infeasible
module_from_template
can cause ReactJS version mismatches resulting in the imported library not workingProposed Actions
We need to investigate how to best automatically interface with, let's say, the top 100 React libraries on NPM.
The main question is, how do we make sure things written for ReactJS are compatible with ReactPy? This will involve is having to rethink our compatibility layer for the following:
Design Concept
Create a ReactPy build step can automatically generate Python wrappers of JavaScript functionality. Ideally, this machine generated Python would also include type hints that are automatically inferred from the JavaScript code. We can potentially deduce type hints using a JavaScript AST parser.
A generated Python file for a react-bootstrap Card could potentially look something like this:
How to handle JavaScript
We have a few ways we can handle JS modules:
The NPM Dependency Issue
For ease of use, we should bundle precompiled NPM binaries within ReactPy Package Manager. This would be far less janky than requiring the user to externally install NPM. We may also want to consider using bun instead of NPM.
We should probably follow the same precedence as Playwright/Selenium. If the user tries to use
reactpy build
, we should first check if our NPM binary has been installed. If it hasn't, we will tell the user to run areactpy download-npm
command.The "children as props" issue
As mentioned in #989, we need to spend some additional thought on how to convert JavaScript components to Python components.
This is due to libraries like MaterialUI existing that have components as props. This might involve inventing some way to serialize React components in a reproducible way.
react-serialize
could potentially work for this.Command Line Interface
The CLI should be accessible within the
reactpy <command>
namespace. All package manager commands will print out an error ifreactpy-package-manager
is not installed.build
commandpackage.json
, formatted in NPM parseable formatdependencies
within apackage.json
reactpy build --all
will auto discover allpackage.json
files that have areactpy
attribute within them.reactpy build <DIR>
will build a single package.workspaces
inpackage.json
, throw an error.__init__.py
into the given directory, unless a different output file is specified (in package.json)npm
and automatically createnode_modules/.gitignore
containing*
node_modules/.gitignore
if it does not existnode_modules/@reactpy/_build/output.js
package.json:reactpy
such asoutput_path
,node_modules_gitignore
package.json
fields aremain
andreactpy
install
command--target-dir
option (we should make sure to name it the same as the pip's equivalent)--exports=...
option to limit what gets builtreactjs.pkg_name
package.json
and start populate it for every installed packageuninstall
commanduninstall
command should have an--all
optionnpm
commandnpm
command is a direct passthrough of our local nodenpm
instancedownload-npm
commandnode
to the current environment, similar to howselenium
downloads Chromium.The text was updated successfully, but these errors were encountered: