Build a Web Extension with React.js

Ready to craft a web extension thatโ€™ll dazzle Chrome and Firefox users? ๐ŸŒ Letโ€™s dive into this step-by-step adventure with Vite, Tailwind CSS, and a sprinkle of zippy magic! ๐Ÿš€ Buckle up and letโ€™s make your browser shine! โœจ

๐ŸŒ Install Chrome & Firefox โ€” Your Testing Turf ๐ŸฆŠ๐Ÿ”ต

First, make sure youโ€™ve got Chrome and Firefox installed. These are your playgrounds for testing your epic extension. No browsers, no glory! ๐Ÿ˜Ž

๐Ÿ“ฆ Kick Things Off with Viteโ€™s Web Extension Plugin ๐Ÿ› ๏ธ

Letโ€™s get the party started by scaffolding your extension with a single command. Run this and watch the magic unfold! ๐ŸŽ‰

bash
npm create vite-plugin-web-extension

๐Ÿ›ค๏ธ Fix That Pesky Directory Switch ๐Ÿšง

If the command gods arenโ€™t cooperating, you might need to manually hop into your project folder. Try this, or click your way to victory! ๐Ÿ–ฑ๏ธ

bash
cd "app"

Pro tip: If itโ€™s acting stubborn, navigate to the folder manually like a true explorer. ๐Ÿงญ

๐Ÿ“ฅ Load Up the Node Goodies ๐Ÿ“ฆ

Feed your project the dependencies it craves. This command is like tossing a Happy Meal to your code! ๐Ÿ”

bash
npm install

๐ŸŽจ Sprinkle in Some Tailwind CSS Magic โœจ

Time to make your extension look slick with Tailwind CSS. Install these beauts and get ready to style! ๐Ÿ’…

bash
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

๐Ÿ–Œ๏ธ Add Tailwind to Your Popupโ€™s Style Sheet ๐ŸŽจ

Open pages/Popup.css and drop in these Tailwind directives to give your popup some serious flair! ๐Ÿ”ฅ

css
@tailwind base;
@tailwind components;
@tailwind utilities;

๐Ÿงฉ Wire Up Tailwind in pages/Popup.jsx ๐Ÿ› ๏ธ

Letโ€™s make your popup pop! Update pages/Popup.jsx with this code to import Tailwind and say โ€œHello Worldโ€ with style. ๐Ÿ˜Ž

jsx
import { useEffect } from "react";
import "./Popup.css";

export default function () {
  useEffect(() => {
    console.log("Hello from the popup! ๐Ÿ‘‹");
  }, []);

  return <h1 className="font-bold text-3xl underline">Hello world! Test ๐Ÿš€</h1>;
}

โš™๏ธ Tweak tailwind.config.js for Maximum Style ๐Ÿ’ช

Configure Tailwind to play nice with your files. Update tailwind.config.js like so to ensure your styles are applied everywhere they should be! ๐ŸŒŸ

javascript
module.exports = {
  content: [
    './public/**/*.html',
    './src/**/*.{js,jsx,ts,tsx}', // Point to your source files
  ],
  theme: {
    extend: {},
  },
  plugins: [],
};

๐Ÿƒโ€โ™‚๏ธ Run Your Extension in Dev Mode ๐Ÿ

Time to see your creation in action! Fire up the dev server and watch your extension come to life. ๐ŸŽฌ

bash
npm run dev

๐Ÿ“ฆ Zip It Up for the Big Leagues! ๐Ÿ—ณ๏ธ

Ready to package your extension for the world? Letโ€™s create a shiny extension.zip file. First, install the zipping tool:

bash
npm i zip-a-folder

Then, create a zipScript.js file to bundle your dist folder into a neat little zip package. ๐Ÿ“ฌ

javascript
import { zip } from "zip-a-folder";
await zip("dist", "extension.zip");

๐Ÿ› ๏ธ Update package.json for Easy Building ๐Ÿ› ๏ธ

Add these scripts to your package.json to streamline building and zipping. Itโ€™s like giving your project a turbo button! ๐Ÿš€

json
{
  "scripts": {
    "build": "vite build && node zipScript.js",
    "zip": "node zipScript.js"
  }
}

๐ŸŽ‰ Kaboom! Youโ€™re an Extension Extraordinaire! ๐ŸŒŸ

Congrats, youโ€™ve built a web extension thatโ€™s ready to rock Chrome and Firefox! ๐ŸŽŠ Load it up, test it out, and maybe add some extra pizzazz to make it yours. Now go conquer the browser world, you coding superhero! ๐Ÿฆธโ€โ™€๏ธ๐Ÿ’พ


Made with โค๏ธ in Aotearoa.