3

I am having trouble splitting code across multiple files using transcrypt (version 3.6.95). As a basic example, I have the following files in the same directory:

index.htm

<html>
  <head>
    <meta charset="utf-8">
    <title>Transcrypt test</title>
</head>
<body>
  <div id="box"></div>
  <button onclick="myscript.set_box_content()">Set box content</button>
</body>
<script src="__javascript__/myscript.js"></script>    
</html>

mymodule.py

def helloworld():
    return "Hello world!"

myscript.py

from mymodule import helloworld

def set_box_content():
    document.getElementById("box").innerHTML = helloworld()

I then run

python -m transcrypt -n mymodule.py
python -m transcrypt -n myscript.py

Which runs without error and generates mymodule.js, mymodule.mod.js, myscript.js and myscript.mod.js in the directory __javascript__.

When I open index.htm in Firefox 58 and open the console it says 'TypeError: module is undefined'. I have tried adding <script src="__javascript__/mymodule.js"></script> to the HTML but this does not help. I read through this part of the transcrypt documentation, but the -u switch does not appear in the list of available commands when I type python -m transcrypt -h.

Dominic Price
  • 1,111
  • 8
  • 19

1 Answers1

2

Units (compilation units, components) are a relatively new feature, as opposed to modules, that have been in Transcrypt from the start. You need Transcrypt 3.6.101 to use units. Note that since CPython is an interpreter rather than a compiler, the concept of a compilation unit plays no role there.

The use of units in combination with modules is shown in:

https://transcrypt.org/docs/html/special_facilities.html#transcrypt-s-unit-mechanism-and-creating-native-javascript-component-frameworks

This example should get you started, if not please let me know in a comment or edit.

[EDIT] All units (as opposed to modules) should be compiled separately, so in the example:

transcrypt -u .run animals.py

transcrypt -u .com cats.py

transcrypt -u .com dogs.py

So the module containing the runtime with the .run option and the other components with the .com option. The -n switch can be added if desired.

Modules that are used in multiple units should be added to the runtime unit, that is the one compiled with the -u .run switch.

Jacques de Hooge
  • 6,750
  • 2
  • 28
  • 45
  • Downgrading to 3.6.101 has given me the -u switch; I now run `transcrypt -n -u .run myscript.py` which generates the myscript_loader which I load into the HTML and inside more script tags I have `myscript_loader(['__javascript__/myscript.js', '__javascript__/mymodule.js']);`. This now outputs two 'XML Parsing Error: syntax error' at myscript.js:1:13 and mymodule.js:1:13 before echoing the 'TypeError: module is undefined' again. I get the same errors when I tried to copy the example from the docs. – Dominic Price Feb 21 '18 at 18:12
  • Please see my EDIT! All units (that can each consist of multiple modules) must be compiled separately. I've once again tried the example in the docs and could not reproduce the problem. Curious what's going on, though... – Jacques de Hooge Feb 21 '18 at 19:56
  • I tried on a Linux VM and got the example to work, I’ll have to investigate further why it’s not working on my normal setup. Thanks for all your help! – Dominic Price Feb 22 '18 at 11:35
  • You're welcome, please keep me informed, as we try to iron out these things or mention them in the docs. – Jacques de Hooge Feb 22 '18 at 11:38