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.
- 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 @types/node @types/express
- Update the scripts in packages.json with following changes -
- It is optional to include start , so for start command use this: node dist/index.js
- For build command use this: tsc -p tsconfig.json
- By now it should working if you run the following commands.
-
Now you can just refactor the code according to make it to TS.
-
Also now you can create a dev command and include this : ts-node src/index.js
-
If you are facing problems and not able to execute these steps. Click on this Link.