-
-
Notifications
You must be signed in to change notification settings - Fork 329
Description
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
- It's not reasonable manually translate every ReactJS component to a Python.
module_from_templatecan cause ReactJS version mismatches resulting in the imported library not working- JavaScript Module API feels awkward to use.
Proposed 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:
- Hooks
- Events
- Components
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:
_Card = module_from_file("path/to/built/src/react-bootstrap.js", imports="Card")
@component
def Card(bsPrefix=None, className=None, bg=None, text=None, border=None, body=None, children=None, as='div', body=False):
return _Card(bsPrefix, className, bg, text, border, body, children, as, body)How to handle JavaScript
We have a few ways we can handle JS modules:
- Run JavaScript client side, and figure out how to communicate info back to the server
- Run JavaScript client side, but have the user only be able to interact with that code with other client-side code
- Run JavaScript server side. This will likely be transpiled into WASM and run within python.
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 a reactpy 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 if reactpy-package-manager is not installed.
build command
- User generated packages will need a
package.json, formatted in NPM parseable format - Users must declare their own
dependencieswithin apackage.json reactpy build --allwill auto discover allpackage.jsonfiles that have areactpyattribute within them.reactpy build <DIR>will build a single package.- Docs will show custom components using ReactJS, not Preact
- If we see
workspacesinpackage.json, throw an error. - We will output a
__init__.pyinto the given directory, unless a different output file is specified (in package.json) - Use
npmand automatically createnode_modules/.gitignorecontaining* - Always recreate
node_modules/.gitignoreif it does not exist - We should hide our output in
node_modules/@reactpy/_build/output.js - Add settings within
package.json:reactpysuch asoutput_path,node_modules_gitignore - The two mandatory
package.jsonfields aremainandreactpy
install command
- Should have a
--target-diroption (we should make sure to name it the same as the pip's equivalent) - Should have a
--exports=...option to limit what gets built - Should create a python module via cookiecutter.
- Installed modules are accessible within Python via
reactjs.pkg_name - Should automatically create a top level
package.jsonand start populate it for every installed package
uninstall command
uninstallcommand should have an--alloption
npm command
npmcommand is a direct passthrough of our local nodenpminstance
download-npm command
- Downloads
nodeto the current environment, similar to howseleniumdownloads Chromium. - If called a second time, will prompt the user if they want to delete then redownload