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

rollup-plugin-keep-css-imports

v1.0.0

Published

Rollup plugin that allows to maintain the original structure of style imports without altering them during the bundling process

Downloads

393

Readme

Rollup Plugin: Keep CSS Imports

rollup-plugin-keep-css-imports is a Rollup plugin that allows to maintain the original structure of style imports (CSS, SCSS, or SASS) without altering them during the bundling process.

Why?

It will be helpful for building a components library and want to keep all CSS module imports untouched so consumer decide how to bundle or tree shake them. This plugin support both bundled and preserved modules ESM outputs.

Features

  • Keeps style imports unchanged
  • Supports CSS, SCSS, and SASS files
  • Allows the use of custom output paths and extensions
  • Emits assets to the specified output directory
  • Supports source maps
  • Compatible with PostCSS

Installation

Install via npm:

npm install rollup-plugin-keep-css-imports --save-dev

or via yarn:

yarn add rollup-plugin-keep-css-imports --dev

Usage

Add the plugin to Rollup configuration file:

import keepCssImports from "rollup-plugin-keep-css-imports"

export default {
  input: "src/index.js",
  output: {
    file: "dist/bundle.js",
    format: "esm",
  },
  plugins: [
    keepCssImports({
      // Plugin options go here
    }),
  ],
}

Options

Input Options

includeRegexp

| | | | -------- | -------------------- | | Type: | RegExp | | Default: | /\.(?:s[ca]|c)ss$/ |

Regular expression to test if an import should be processed by this plugin

OutputOptions

outputExt

| | | | -------- | -------- | | Type: | string | | Default: | ".css" |

Specifies the file extension for the output CSS files. This is typically ".css" but can be changed if needed.

outputDir

| | | | -------- | -------- | | Type: | string | | Default: | "./" |

Specifies the output directory for the generated CSS files. Relative to Rollup output folder.

outputPath

| | | | -------- | ---------------------------------- | | Type: | "keep" | string | function | | Default: | "keep" |

Specifies the output path relative to outputDir for the generated CSS files. The default value, "keep", preserves the original file paths. It is also possible to provide a custom function to generate output paths based on the input file.

Example:

import keepCssImports from "rollup-plugin-keep-css-imports"
import path from "path"

export default {
  input: "src/index.js",
  output: {
    file: "dist/bundle.js",
    format: "esm",
  },
  plugins: [
    keepCssImports({
      outputPath: (assetId) => {
        // Generate a custom output path based on the input assetId

        // Make the assetId path relative to the current working directory (CWD)
        const relativePath = path.relative(process.cwd(), assetId)

        // Replace 'src' with 'styles' in the relativePath
        const newPath = relativePath.replace("src", "styles")

        // Add a '.min' suffix before the file extension,
        // extension will be replaced with `outputExt` by the plugin
        return newPath.replace(/(\.s[ca]ss)$/, ".min$1")
      },
    }),
  ],
}

sourceMap

| | | | -------- | ----------------------- | | Type: | boolean | "inline" | | Default: | false |

Specifies whether to generate source maps for the compiled CSS. Use "inline" to inline source maps into CSS files.

skipCurrentFolderPart

| | | | -------- | ----------------------- | | Type: | boolean | RegExp | | Default: | false |

By default CSS paths will be prefixed with current folder mark ./. To avoid this for CSS files use true or specify RegExp filter. If RegExp filter matches ./ won't be added to the path. This option may be helpful if you have some issues with external modules imports from node_modules

Extensions' Options

includePaths

| | | | -------- | ------------------- | | Type: | Array<string> | | Default: | ["node_modules/"] |

Specifies the list of include paths for SASS to search when resolving imports.

sass

| | | | -------- | --------------- | | Type: | Sass compiler | | Default: | Dart Sass |

An optional object that allows you to provide a custom SASS implementation, such as Dart SASS or Node SASS. If not specified you must install Dart SASS (npm install sass --save-dev / yarn add sass --dev)

sassOptions

| | | | -------- | ----------- | | Type: | Object | | Default: | undefined |

An optional object that allows to provide additional options for the SASS compiler.

postProcessor

| | | | -------- | ----------- | | Type: | function | | Default: | undefined |

An optional function that allows you to perform additional processing on the generated CSS, such as applying PostCSS plugins.

Example: Use PostCSS with the postcss-url plugin to inline the URLs in the CSS files:

import keepCssImports from "rollup-plugin-keep-css-imports"
import path from "path"
import postcss from "postcss"
import url from "postcss-url"

export default {
  input: "src/index.js",
  output: {
    file: "dist/bundle.js",
    format: "esm",
  },
  plugins: [
    keepCssImports({
      postProcessor: () =>
        postcss([
          url({
            url: "inline",
            basePath: [path.resolve("src"), path.resolve("node_modules")],
          }),
        ]),
    }),
  ],
}

Notes

  • Plugin reads preserveModulesRoot property of Rollup output settings, so it can be used to make output tree depth smaller

Supported Rollup versions

Tested with v3 and v4

License

MIT

Copyright (c) 2024 Alexandr Yeskov