TranscryptOrg / Transcrypt

Python 3.9 to JavaScript compiler - Lean, fast, open!
https://www.transcrypt.org
Apache License 2.0
2.82k stars 215 forks source link

imported javascript module can't find 3rd lib except reactjs and threejs #830

Closed nightsailer closed 1 year ago

nightsailer commented 1 year ago

Hi,

I read docs section:

6.8. Example: Using the Parcel.js bundler to package a set of modules written in diverse programming languages.

It shows that transcrypt support python module import javascript module directly.

I write a demo to test following docs, but failed.

test example layout:

`-- src/app.py:

import demo

DemoEditor = demo.DemoEditor

def App(): return DemoEditor()

--- src/demo.js

import React, { useRef, useState } from "react"; import { Canvas, useFrame } from "@react-three/fiber";

export function DemoEditor(props) { return

hello
; } `

I compile with following cmd:

$> python -m transcrypt --nomin --map --verbose ./src/app.py

then error occurred, messages:

` (flarekit) ➜ demo git:(develop) ✗ python -m transcrypt --nomin --map --verbose ./src/app.py

Transcrypt (TM) Python to JavaScript Small Sane Subset Transpiler Version 3.9.0 Copyright (C) Geatec Engineering. License: Apache 2.0

Parsing module: /opt/data/mambaforge/envs/flarekit/lib/python3.9/site-packages/transcrypt/modules/org/transcrypt/runtime.py Generating code for module: /Users/night/working/demo/src/target/org.transcrypt.runtime.js Saving source map in: /Users/night/working/demo/src/target/org.transcrypt.runtime.map Saving target code in: /Users/night/working/demo/src/target/org.transcrypt.runtime.js Parsing module: ./src/app.py Generating code for module: /Users/night/working/demo/src/target/app.js Saving target code in: /Users/night/working/demo/src/target/demo.js Parsing module: /opt/data/mambaforge/envs/flarekit/lib/python3.9/site-packages/transcrypt/modules//init.py Generating code for module: /Users/night/working/demo/src/target/.js Saving source map in: /Users/night/working/demo/src/target/.map Saving target code in: /Users/night/working/demo/src/target/.js

Error while compiling (offending file last): File './src/app.py', line 1, at import of: File 'demo', line 1, namely:

    Import error, can't find any of:
            ./src/eact-three/fi.py
            ./src/eact-three/fi.js
            /opt/data/mambaforge/envs/flarekit/lib/python3.9/site-packages/transcrypt/modules/eact-three/fi.py
            /opt/data/mambaforge/envs/flarekit/lib/python3.9/site-packages/transcrypt/modules/eact-three/fi.js
            /Users/night/working/demo/eact-three/fi.py
            /Users/night/working/demo/eact-three/fi.js
            /opt/data/mambaforge/envs/flarekit/lib/python3.9/eact-three/fi.py
            /opt/data/mambaforge/envs/flarekit/lib/python3.9/eact-three/fi.js
            /opt/data/mambaforge/envs/flarekit/lib/python3.9/lib-dynload/eact-three/fi.py
            /opt/data/mambaforge/envs/flarekit/lib/python3.9/lib-dynload/eact-three/fi.js
            /opt/data/mambaforge/envs/flarekit/lib/python3.9/site-packages/eact-three/fi.py
            /opt/data/mambaforge/envs/flarekit/lib/python3.9/site-packages/eact-three/fi.js
            /Users/night/working/o3cloud/flarekit/eact-three/fi.py
            /Users/night/working/o3cloud/flarekit/eact-three/fi.js
            /Users/night/working/o3cloud/yoda/eact-three/fi.py
            /Users/night/working/o3cloud/yoda/eact-three/fi.js
            /Volumes/data/mambaforge/envs/flarekit/lib/python3.9/site-packages/eact-three/fi.py
            /Volumes/data/mambaforge/envs/flarekit/lib/python3.9/site-packages/eact-three/fi.js

Aborted `

I've install required npm packages.

It's weird, if I remove

import { Canvas, useFrame } from "@react-three/fiber";

It works!

So, I reproduced to add/remove some libs, finally, only three/reactjs will pass , other libs can't find.

Is it any limit? How can I import 3rd libs from these local js module?

JennaSys commented 1 year ago

Based on the fact that it's looking for "eact-three" instead of "react-three" it looks like this might be related to issue #766

I don't run into this issue myself in practice as I usually use the require() function for my JavaScript imports, For example:

React = require('react')
useState = React.useState
useRef = React.useRef

From there, React, useState, and useRef can be imported into other modules and used just as plain Python objects/functions.

The only "tricky" part of doing it this way is determining how the JavaScript functions were exported in the JS library in order to get the require() syntax correct. Sometime you have to use

require('libName')[default]

or

require('libName')[NameOfFunction]

to get it to map properly. The biggest benefit to doing it this way IMO is that it keeps almost all of your code in the Python world, and there is less JavaScript to deal with.

nightsailer commented 1 year ago

@JennaSys Thanks!

Yes, I known the tricky of require. I just read doc example and try to figure why my stuff not works.

I search and browse a lot of opened/closed issues about transcrypt, but not found any related.

It's confirmed the compiler bug, I should not import js local module anymore.