Developing a webpage similar to keycode.info and serving it in a Docker Container
Engin Polat
Happiest Senior Software Engineer at Microsoft ??♂? | C#, Typescript, Bicep, Docker, Kubernetes, Go, Terraform, Helm, DevOps, ...
Original article and the source code of the project published at Madrid repo on my GitHub account (polatengin).
Not : Makalenin Türk?e halini de Madrid repo i?erisinde bulabilirsiniz ??
You can find and use the running version of this project at https://polatengin-madrid.netlify.com/
The reason I made this project;
- To learn how to inject javascript and css into an html page by configuring Webpack and PostCSS
- Experience and understand how browsers are processing keyboard events
- Create and run a pipeline with GitHub Actions
- Gain the habit of project development within Visual Studio Code DevContainer
Technologies I used in this project are;
- Typescript
- WebPack
- PostCSS
- GitHub Actions
- Docker
- Netlify
- DevContainers
To create the project, let's run the following command in an empty directory
npm init --force
Now, open a Terminal window and run the following command to add the keycode npm package to package.json
npm i keycode --save
Now, we can require the keycode module in src/index.ts
const keycode = require('keycode');
By attaching to the keydown event of the window global object, we can capture all the pressed keys in the entire browser window.
window.addEventListener('keydown', (e) => { });
By calling the stopPropagation() and preventDefault() methods in the event handler, we prevent the keys from being processed by the browser.
So, even if the keys like F5, F12 are pressed, the browser will not process them.
We add the required html code into the src/index.html file so that a table will appear on the screen.
Also, we're providing the same editor settings to the developers (space/tab, end-of-line-character, etc.) with the .editorconfigfile.
In the tsconfig.json file, we give the compilerOptions.outDir property value of ./dist, so that when the webpack compiles, the compiled files will be created in the ./dist folder.
In the src/index.ts file, we capture all keydown events via the window.addEventListener() method.
We use the following two lines to prevent any keydown events we capture from being captured by the browser.
e.stopPropagation(); e.preventDefault();
We added the following plugins into the webpack.config.js file
- With CopyWebpackPlugin, we can copy files based on their extensions
- With HtmlMinifierPlugin, we can compress the html files
- With HtmlWebpackPlugin, the bundle.js file that is generated by compiling the ts files, and it's added into the index.html file
Also, with the hash option of the HtmlWebpackPlugin plugin, we added the compiled ts files into the index.html as bundle.js?{HASH}.
Thanks to Multi-Layered Dockerfile, we compile the project in node:12.11.1 image, then move all compiled files to nginx:1.17.0-alpine image and expose them.
At the end, we have a Docker Image that takes about 20MB in size.
Top 1.5% at StackOverflow | Lead Web3 Full-Stack Software Engineer | JS, TS, React, Next.js, Node.js, tRPC, DynamoDB, Clean Software Architecture
4 年Looks nice, I have actually just released a similar site with a few more options and soon mobile support and cross-browser compatibility information (particularly for IE and Safari): https://keyjs.dev/
Senior Frontend Developer
5 年This ia great! Congrats