Code Monkey home page Code Monkey logo

esbuild-plugin-css's Introduction

@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:

./dist/my_module.js
// index.ts
var my_div = document.createElement("div");
my_div.id = "mydiv";
document.body.append(my_div);
./dist/my_module.css
/* 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:

./dist/my_module.js
// 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);

esbuild-plugin-css's People

Contributors

omar-azmi avatar actions-user avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.