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

@oazmi/esbuild-plugin-css

v0.1.1

Published

esbuild plugin for bundling or injecting css (via js) for the Web and Deno.

Downloads

105

Readme

@oazmi/esbuild-plugin-css

Import CSS files in your javascript, and have them bundled by esbuild, either as css files or as a javascript style injection code. Works in Browser and Deno environments, and does not have any dependencies.

Example

suppose you've got the following two files in your current working directory:

/* FILE: ./my_styles.css */
@import url("https://unpkg.com/[email protected]/some-library.css");

#mydiv {
	width: 100vw;
	background-color: red;
}
// FILE: ./my_module.ts
import "./my_styles.css"
import "https://cdnjs.cloudflare.com/ajax/libs/[email protected]/some-other-library.css"

const my_div = document.createElement("div")
my_div.id = "mydiv"
document.body.append(my_div)

Bundle imported css files with Deno as a separate file

// FILE: ./build.ts
import * as esbuild from "https://deno.land/x/[email protected]/mod.js"
import { denoPlugins } from "jsr:@luca/[email protected]"
import { cssPlugin } from "jsr:@oazmi/esbuild-plugin-css"

const result = await esbuild.build({
	plugins: [cssPlugin({}), ...denoPlugins()],
	entryPoints: ["./my_module.ts"],
	outdir: "./dist/",
	bundle: true,
	// minify: true // this will also minify the css
})

esbuild.stop()

now, in your terminal, run:

deno run -A "./build.ts"

which will output:

// index.ts
var my_div = document.createElement("div");
my_div.id = "mydiv";
document.body.append(my_div);
/* oazmi-css:https://unpkg.com/[email protected]/some-library.css */
/*
  BUNDLED CONTENTS OF https://unpkg.com/[email protected]/some-library.css
*/

/* oazmi-css:file:///D:/projects/2024/esbuild-plugin-css/test/1/styles.css */
#mydiv {
  width: 100vw;
  background-color: red;
}

/* oazmi-css:https://cdnjs.cloudflare.com/ajax/libs/[email protected]/some-other-library.css */
/*
  BUNDLED CONTENTS OF https://cdnjs.cloudflare.com/ajax/libs/[email protected]/some-other-library.css
*/

Bundle imported css files with Deno as javascript injection code

Use the { mode: "inject" } plugin option to to have the bundled css become an injection code that'll inject the style tags into your document.

// FILE: ./build.ts
import * as esbuild from "https://deno.land/x/[email protected]/mod.js"
import { denoPlugins } from "jsr:@luca/[email protected]"
import { cssPlugin } from "jsr:@oazmi/esbuild-plugin-css"

const result = await esbuild.build({
	plugins: [cssPlugin({ mode: "inject" }), ...denoPlugins()],
	entryPoints: ["./my_module.ts"],
	outdir: "./dist/",
	bundle: true,
	// minify: true // this will also minify the css
})

esbuild.stop()

now, in your terminal, run:

deno run -A "./build.ts"

which will output:

// oazmi-css:file:///D:/projects/2024/esbuild-plugin-css/test/1/styles.css
var style_dom = document.createElement("style");
style_dom.textContent = `/* oazmi-css:https://unpkg.com/[email protected]/some-library.css */
/*
  BUNDLED CONTENTS OF https://unpkg.com/[email protected]/some-library.css
*/

/* oazmi-css:file:///D:/projects/2024/esbuild-plugin-css/test/1/styles.css */
#mydiv {
  width: 100vw;
  background-color: red;
}
`;
document.head.append(style_dom);

// oazmi-css:https://cdnjs.cloudflare.com/ajax/libs/[email protected]/some-other-library.css
var style_dom2 = document.createElement("style");
style_dom2.textContent = `/* oazmi-css:https://cdnjs.cloudflare.com/ajax/libs/[email protected]/some-other-library.css */
/*
  BUNDLED CONTENTS OF https://cdnjs.cloudflare.com/ajax/libs/[email protected]/some-other-library.css
*/
`;
document.head.append(style_dom2);

// my_module.ts
var my_div = document.createElement("div");
my_div.id = "mydiv";
document.body.append(my_div);