Python to JavaScript transformer for Parcel V2 using the Transcrypt transpiler
npm install parcel-transformer-transcryptIf you find a problem with this Parcel transformer, please submit an issue on GitHub and help make it better.
If you just have a question about how to use it, you can also post a message in GitHub discussions.
bash
npm install parcel-transformer-transcrypt --save-dev
`To get the latest unpublished version, you can also install it directly from the GitHub repository:
`bash
npm install https://github.com/TranscryptOrg/parcel-transformer-transcrypt --save-dev
`$3
Obviously Parcel v2 must be installed:`bash
npm install parcel -D
`
This plugin also requires installation of the Transcrypt transpiler. In order for Transcrypt to properly parse the AST of your Python code, the version of Python you use with Transcrypt must match the version of Transcrypt it was designed for.It is recommended to install Transcrypt into a virtual environment for your project. This can be accomplished with the following commands:
For Python 3.9:
`bash
python3.9 -m venv venv
. ./venv/bin/activate
python -m pip install transcrypt
`For Python 3.7:
`bash
python3.7 -m venv venv
. ./venv/bin/activate
python -m pip install transcrypt==3.7.16
`Note that when run, before the build process starts, this transformer will automatically detect the version of Python and Transcrypt being used by running the following commands in the background:
-
python --version
- transcrypt --helpTo bypass this auto-detect behavior, you can explicitly specify the Transcrypt version in the _package.json_ file as indicated in the next section.
$3
You will need to create a _.parcelrc_ file in the same folder as the _package.json_ file to let Parcel know how to handle the Python files:
_.parcelrc_
`json
{
"extends": ["@parcel/config-default"],
"transformers": {
"*.py": ["parcel-transformer-transcrypt"]
}
}
`To override the default settings for Transcrypt, you can add a
"parcel-transformer-transcrypt" key to the _package.json_ file with the desired CLI configuration information:
`json
"parcel-transformer-transcrypt": {
"transcryptVersion": "3.9",
"watchAllFiles": true,
"command": "python -m transcrypt",
"arguments": [
"--nomin",
"--map",
"--verbose"
]
}
`
The "transcryptVersion", "watchAllFiles", "command", and "arguments" keys are all optional. Default values will be used if not supplied.If the
watchAllFiles key is missing or set to true, all Python files that are processed by Transcrypt will be added to Parcel's file watch. If this key is set to false, only the initial entry point file will be watched.Transcrypt normally puts the files it generates in a folder called
__target__ that is created in the same folder as the source files you are processing. This default behavior may not be desired in many cases.If you are using Transcrypt 3.9 however, this Parcel transformer will put Transcrypt's generated files in a folder named
.build that will be created in the root folder of the project (where the _package.json_ file resides and where you run npm commands from).
You can override the location of this build folder by adding an argument to the configuration as shown above:
`json
"arguments": [
"--nomin",
"--map",
"--outdir src/__target__"
]
`
The output folder you specify in the arguments should be relative to the project root folder. _Note that the
--outdir directive is not valid for Transcrypt version 3.7 and will be ignored in that case._You can also set up build scripts in the _package.json_ file for Parcel that might look similar to this:
`json
"scripts": {
"start": "NODE_ENV=development parcel --log-level info src/index.html --dist-dir dist/dev --port 8080",
"build": "NODE_ENV=production parcel build --log-level info src/index.html --no-source-maps --dist-dir dist/prod --no-cache"
}
`
The npm start script would run Parcel in development mode that starts up a development web server and watches source files for changes.The
npm run build script builds the application for production then exits.
$3
- _What does this Parcel plugin do?_
Instead of having to run Transcrypt manually and then use those generated JavaScript files as the input to what you want to bundle with Parcel, you can just run Parcel and it will run Transcrypt for you when it sees a file with a .py file extension.
For example, if Parcel sees a Python file specified as the src` in an HTML script tag, it will have Transcrypt generate the Javascript file and then automatically update the HTML script tag with the name of the generated JavaScript file. _But there are also a few differences:_
- The output folder is configurable
- Parcel file watch works on _all_ transpiled Python files in development mode and not just the entry point
- It doesn't need to be patched before using it :-)
- _What if I don't want to use Transcrypt?_
Then you will have to use a different Parcel transformer for Python other than this one. It is a hard dependency.
If you are using Linux and start getting errors stating "No space left on device", see the Parcel website for how to fix it.