Trying to compile and run `hello` demo from source, but I get "ReferenceError: hello is not defined"
Not sure if this is the right place here, but I cannot get the hello demo get running from teh source code:
After installing transcrypt in my virtual env and activating the venv, I do the follwoing:
-
I copy
hello.htm&hello.pyto a new directorymyhello. -
In this directory I run
transcrypt hellowhich generates stuff in a
__target__subdirectory. -
Now I run the web server in myhello and access http://localhost:8000/hello.html.
-
When I click on any of the two buttons nothing happens and I get in the Firefox dev console the message "ReferenceError: hello is not defined".
What's going wrong here? I must miss something vital here...
The demo code works just fine.
- Are you using hello.htm or hello.html?
- Does the
./__target__/hello.jsfile exist? - Are there any other errors in the browser console?
- In the Firefox network tab, can you see
hello.jsbeing loaded without errors?
@JennaSys Thanks for your reply! :-)
To your questions:
-
I use
hello.html- Thehello.htmin the issue description was a typo, sorry. -
Yes,
./__target__/hello.jsexists. -
On loading I get in the browser console
The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol. -
No,
hello.jsis not loaded.
PS: I am using Firefox 45.0.2. - Is this too old? I have heard about that Transcrypt 3.7 needs super-new browsers. Is it right, that for compatibility with all HTML5 browsers I need to downgrade to the more conservative Transcrypt 3.6?
The warning about character encoding is because hello.html as provided isn't a valid HTML document. you can wrap in the proper tags to get rid of that warning:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
</body>
</html>
So you need to figure out why hello.js isn't loading.
- Is it a 404 error? A 500 error?
- is hello.html in the same folder as
__target__?
Can you try it on Chrome?
When writing the "PS" I went in the right direction. Firefox 45 seems to be too old - from another machine with Firefox 68 it works like a charm. So I guess its the type="module" thing?
Can I avoid this with Transcrypt 3.6? I need to provide compatibility;ty with more browsers thanthe latest and greatest...
Yes the type="module" in the script tag is definitely important to support. There may be other ways to load it, but I'm not sure what would work in your case.
What is your experience with Transcrypt 3.6? Better support for older browsers?
None, I just started using it. But I think this is more of an issue with getting js files to load into the browser, than an issue with Transcrypt itself.
True, but older browser do not have the notion of "modules"[^1], I think.
[^1]: I just reading through ES6 Modules in Depth...
FWIW I just used the Transcrypt Parcel plugin and it solved a lot of the messy JS library issues by letting you get rid of the <script/> includes and __pragma__('js') dynamic loading hacks. With Parcel you can just use something like React = require('react') as an import in your code to dynamically load the JS library and get a valid reference to the library in your python code. You need to use npm to work with the JS libraries, but once you get it set up, Parcel is really helpful. And I have to give a big thanks to @doconix for all the effort in getting that working! It really makes for a pretty unintrusive toolchain, to get from clean Python code to a modern web app.
Thanks @JennaSys for the Transcrypt Parcel plugin pointer. This can solve lots of issues, indeed.
However I have trouble to get Parcel + Transcrypt Parcel plugin to work: I always get the error: "Cannot find module 'parcel-bundler/src/Logger`".
Does it work for you with the current version of Parcel and the Transcrypt Parcel plugin? Or do you have a package.json with pacakge versions which works?
Here are the relevant entries from a working project I have:
"parcel-plugin-python": {
"command": "python3 -m transcrypt",
"arguments": [
"--nomin",
"--map",
"--verbose"
]
},
"devDependencies": {
"parcel-bundler": "^1.10.3",
"parcel-plugin-transcrypt": "^1.0.20"
},
For the module error, look at issue 694, I put the fix in there. The plug-in references 3 files that have been removed from the newer version of the bundler. Adding them back in is a workaround for now.
Thanks heaps, @JennaSys! Will try it out! :-)