DEV Community

Cover image for Preact, Vite, & Docker Compose in 5 easy steps
Amin
Amin

Posted on

Preact, Vite, & Docker Compose in 5 easy steps

Requirements

  • Docker
  • Docker Compose
  • A terminal emulator
  • A text editor

Docker Compose

Add these lines to docker-compose.yaml.

version: "3"

services:
  yarn:
    image: node:16.0.0
    user: node
    working_dir: /home/node
    tty: true
    stdin_open: true
    entrypoint: yarn
    command: --help
    volumes:
      - .:/home/node
Enter fullscreen mode Exit fullscreen mode

Vite

Add these lines to vite.config.js.

export default {
  esbuild: {
    jsxFactory: "h",
    jsxFragment: "Fragment"
  }
};
Enter fullscreen mode Exit fullscreen mode

Preact

Add these lines to index.jsx.

import {h, render} from "preact";
import {useCallback} from "preact/hooks";

const App = () => {
  const onButtonClick = useCallback(() => alert("Hello, Preact"), []);
  return (
    <button onClick={onButtonClick}>
      Hello
    </button>
  );
};

const app = document.getElementById("app");

if (app) {
  render(<App />, app);
}
Enter fullscreen mode Exit fullscreen mode

HTML

Add these lines to index.html.

<!DOCTYPE html>
<html>
  <body>
    <div id="app"></div>
    <script src="./index.jsx" type="module"></script>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Run

Run the following commands.

$ docker-compose run yarn add preact vite
$ docker-compose run --publish 3000:3000 yarn vite --host
$ open http://localhost:3000
Enter fullscreen mode Exit fullscreen mode

That's it

You can now build your next awesome Preact application using the awesome Vite bundler running anywhere with Docker Compose.

Use this command to generate your optimized build.

$ docker-compose run yarn vite build
Enter fullscreen mode Exit fullscreen mode

Thank you Evan, I can now dev on my 2 cores @1GHz laptop using Docker Compose without going for a coffee break between each updates on my projects.

Top comments (0)