A template for kick starting a Cloudflare worker project, optimized for use in Replit.
Learn more about Workers
src/index.js
is the content of the Workers script.
Here are some commands that this repo comes with. Feel free to add your own, or add a Pull Request to add some to this repo. Note that all these commands must be added to the run field of the .replit
file, then press the run button to execute them, and each command must be prefixed with yarn. Also, the default command will automatically build and create a miniflare instance for you.
Utilizes Prettier to format your code. Configuration can be edited in the .prettierrc
file.
Runs Esbuild to build your script. Default is to bundle and minify. Configuration can be edited directly in the command in the package.json
.
Disables minifying to allow you to debug your code easier.
Automatically builds your script, then uses Miniflare to allow you to test your script locally, before deploying.
Removes the built script and node_modules
, to allow you to build from scratch.
- First run the following command in your console.
npx typescript --init
- Now a tsconfig.json file will be created and now make the following changes.
- Make outDir: './dist'
- Make rootDir: './src'
- Uncomment the following attibute named moduleResolution.
- Uncomment the attribute CheckJS so that it enables ts to type check for your js files.
-
Now you can just rename your js file to ts.
-
Check for the modules that you imported whether it supports both natively, if not them install it using @types. For example if express has no definations and is a seperate module for TS, then you can install it following this snippet.
npx -i -D typescript @cloudflare/workers-types
Update the scripts in packages.json by adding tsc &&
to the front of all commands.
-
Make sure in your js file you change your variable type to 'Any'.
-
Now you can just refactor the code according to make it to TS.
-
If you are facing problems and not able to execute these steps. Click on this Link.