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

bun-style-loader

v0.4.0

Published

Bun plugin to allow loading css, sass files, and css modules

Downloads

395

Readme

Bun Style Loader

Bun plugin to allow loading css, sass files, and css modules

Usage

To get begin, install the bun-style-loader package using the following commands:

npm install --save-dev bun-style-loader
npm install --save-dev sass # Required for compiling Sass (only when needed)

Next, add the plugin to your build script:

import styleLoader from 'bun-style-loader';

Bun.build({
  ...
  plugins: [
    styleLoader(),
  ],
  ...
});

Now, you can import CSS, SASS files, and CSS modules in your code:

import styles from './styles.css';

console.log(styles); // string of the css file

By following these steps, you integrate bun-style-loader into your project, allowing you to effortlessly import and use CSS, SASS, and CSS modules in your application.

Using at Runtime

To incorporate the bun-style-loader at runtime, follow these steps. Assume you are creating a preloaded script named preload.ts:

// preload.ts
import { plugin } from 'bun';
import styleLoader from 'bun-style-loader';

await plugin(styleLoader(/* options here */));

Next, include preload.ts in the preload property within your bunfig.toml configuration file:

# bunfig.toml
preload = ["./preload.ts"]

Please note that it is crucial to insert the await keyword before the plugin call to ensure proper functionality. Neglecting to do so may result in issues due to a reported problem, as outlined in this GitHub issue.

Configuration

targets

Lightning CSS, which bun-style-loader relies on, does not perform automatic transpilation of CSS syntax for older browsers by default. To support older browsers, you can manually specify target browsers as follows:

import styleLoader from 'bun-style-loader';

Bun.build({
  ...
  plugins: [
    styleLoader({
      targets: [
        'chrome 108',
        'ie 11',
        'safari 15.6',
        'ios_saf 15.6',
      ]
    }),
  ],
  ...
});

Alternatively, you can easily generate the list of target browsers using the browserslist package:

import styleLoader from 'bun-style-loader';
import browserslist from 'browserslist';

Bun.build({
  ...
  plugins: [
    styleLoader({
      targets: browserslist('> 0.25%'),
    }),
  ],
  ...
});

This approach streamlines the configuration process, ensuring that your styles are appropriately transpiled for a broader range of browsers. To see how it works, refer to the runtime-ts example in the repository.

Insert CSS to DOM

The plugin does NOT automatically insert the CSS into the DOM. Instead, it provides the CSS either as a string or as key-value pairs in the case of CSS modules. To incorporate the CSS into the DOM, you need to manually utilize the insertStyleElement`` function from bun-style-loader/utils`.

Example for plain CSS:

import { insertStyleElement } from 'bun-style-loader/utils';
import styles from './styles.css';

insertStyleElement(styles);

Example for CSS modules:

/* styles.module.css */
.blue {
  color: blue;
}
// app.js
import { insertStyleElement } from 'bun-style-loader/utils';
import styles, { code } from './styles.module.css';

insertStyleElement(styles, code);

export default function render() {
  return `<div class="${styles.blue}">Hello World</div>`;
}

These examples demonstrate how to use the insertStyleElement function to manually insert CSS into the DOM for both plain CSS files and CSS modules.

TypeScript

You may need to add a custom type declaration.

For CSS modules:

declare module '*.module.css' {
  const content: Record<string, string>;
  export default content;
  export const code: string;
}

For plain CSS files:

declare module '*.css' {
  const content: string;
  export default content;
}

These type declarations allow you to use CSS modules and plain CSS files in your TypeScript project with proper typings.

Support

If you encounter any issues, or have question regarding the bun-style-loader, please visit the GitHub Issues page to review existing topics or to file a new one.

License

MIT