npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

img-alt-text-webpack-plugin

v1.0.1

Published

A Webpack plugin that automatically adds alt text to <img> elements in HTML output and injects JavaScript to fetch alt text for dynamically loaded images, enhancing accessibility and SEO.

Downloads

19

Readme

img-alt-text-webpack-plugin

Overview

A Webpack plugin that automatically adds alt text to <img> elements in HTML output and injects JavaScript to fetch alt text for dynamically loaded images, enhancing accessibility and SEO.

Features

  • Automatically sets the alt attribute for static <img> elements.
  • Supports dynamic images by fetching alt text when the image loads.
  • Easy integration with existing Webpack projects.
  • Set up with Google Gemini API for generating alt text.

Usage

Installation

npm install img-alt-txt-webpack-plugin -D

Set up webpack config

const ImgAltTextWebpackPlugin = require("img-alt-txt-webpack-plugin");

plugins: [
    new ImgAltTextPlugin({
        key: process.env.GEMINI_API_KEY,    // provide your gemini key
        jsInject: {
            observerJS: true,               // inject script for dynamically loaded images
            jsName: 'imageObserver',        // set name for emitted JS file
        }
    })
],

Set up host server

// server.js
require('dotenv').config();
const path = require('path');
const fs = require('fs');
const express = require('express');
const http = require('http');
const { GoogleGenerativeAI } = require("@google/generative-ai");


// ================ set-up gemini ====================
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash"});

// --------------- create similar middleware function -----------
async function getAltText(req, res, next) {
    // resolve filepath from query parameter
    const filepath = path.resolve(__dirname, `./fromServer/${req.query.file}`);

    // generate alt text if file is found
    if (fs.existsSync(filepath)) {
        const file = fs.readFileSync(filepath);
        // standard prompt to generating alt text
        const prompt = "Generate alt text for this image that can be inserted in img html element. Keep it concise."
        const imageParts = {
            inlineData: {
                data: Buffer.from(file).toString("base64"),
                mimeType: "image/png"
            },
        };
        const result = await model.generateContent([prompt, imageParts]);
        const response = await result.response;
        // add alt text generated to response
        res.altText = response.text();
    } else {
        // alt text does not exist
        res.altText = "alt text does not exist for this image";
    }
    next();
}



// ================ express server ====================
const app = express();
const server = http.createServer(app);

// using middleware to intercept alt text generation
app.get('/alttext', getAltText, (req, res) => {
    res.send(res.altText);
});

if (process.env.MODE === 'production') {
    app.use(express.static(path.join(__dirname, 'prodBuild')));
} else {
    app.use(express.static(path.join(__dirname, 'devBuild')));
}

const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});