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

postcss-redirect-import

v2.0.0

Published

Postcss import substitution

Downloads

12

Readme

postcss-redirect-import

It is decoration for plugin postcss-import, which helps to create resolve rules.

Installation

$ npm install postcss-redirect-import --D

Usage

First, you should learn how to use post-import, because the plugin uses its functionality and acts in the same way, with one exception: in the options you can specify the instructions for the path substitution.

// dependencies
var fs = require("fs")
var postcss = require("postcss")
var subImport = require("postcss-redirect-import")

// css to be processed
var css = fs.readFileSync("css/input.css", "utf8")

// process css
postcss()
  .use(subImport([
    {
      match: {
        base: /components/
      },
      use: {
        base: '<root>/custom/<folder>'
      }
    }
  ]))
  .process(css, {
    // `from` option is required so relative import can work from input dirname
    from: "css/input.css"
  })
  .then(function (result) {
    var output = result.css

    console.log(output)
  })

How it works

The rule consists of two parts:

  1. Conditions in which cases a rule should work. To do this, you specify a regular expression for request (query string) and/or base (path to module which requires resource).

For example, you have style.css inside directory app/components/Button, which imports colors.css.

@import "colors.css";

Button {
  background-color: $bgColor;
}

Here:

  • colors.css is request;
  • ./app/components/Button is base;

Use regular expressions to create the condition, based on these values:

subImport([{
  match: {
    request: /colors\.css/,
    base: /components\/Button/,
  },
  use: {
    ...
  }
}]);

Next property specify a new values of request and/or base.

Use a property to when you want to specify a particular file.

subImport([{
  match: {
    request: /colors\.css/,
    base: /components\/Button/,
  },
  use: {
    request: "./customized/components/Button/colors.css",
    base: "<root>"
  }
}]);

This way will give you:

/* content of ./customized/components/colors.css
instead of ./app/components/Button/colors.css */

Button {
  background-color: $bgColor;
}

Using substrings

You can use the substrings retrieved from request or base by the regular expression to form the path. To insert a substring of a concrete source there are special aliases, that are created based on the pattern <{source}:{index}>.

subImport([
  {
    match: {
      request: /([a-z0-9\.\-]*\.css)$/i,
      base: /components/([a-z0-9]*)\/assets\/([a-z0-9]*)$/i,
    },
    use: {
      request: '~/theme/components/<base:1>/assets/<base:2>/<request:1>'
    }
  }
]);

In this case, we get the three aliases:

  • ([a-z0-9\.\-]*\.css) will become <request:1> and will contain required filename;
  • First ([a-z0-9]*) will become <base:1> and will contain component name;
  • Second ([a-z0-9]*) will become <base:2> and will contain some directory inside assets.

Usage of approach of the regular expression is limited only by your imagination.

In addition, you can use predefined placeholders, such as <root>, <id>, <basename> (read Predefined placeholders).

Not strict substitution

A nice feature of the plugin is the fact that if the overridden path does not exists, it will use the standard method of file resolving. It allows you to create an environment in which any style can work fine without substitution (for example, in the case of using the classic postcss-import plugin). When you replace postcss-import to postcss-redirect-import, you have the opportunity to customize the styles without spoiling the original sources.

Appending

In some cases, it is important not to replace imported resource, and to add to the default. For these cases, there is an option append.

subImport([
    {
      match: {
        request: /variables\.css/,
      },
      use: {
        request: "<root>/global/style.css",
      },
      append: true
    }
])

The added resource will work exactly the same as if you add it as a second import.

@import "variables.css";
@import "style.css"; /* Appended style */

Properties of rules

  • match {object} The values for matching:
    • request {RegExp} Regular expression to match and test request;
    • base {RegExp} Regular expression to match and test base.
  • use {object} The substituted values:
    • request {string} Path to target file;
    • base {string} Path of directory to resolve with.
  • append {bool} Enable append mode.

Designation

  • request The string passed to import; @import "it_is_id";
  • base The absolute path to the directory, relative to which the file will be resolved.

Predefined placeholders

"~": root, "": request, "": root, "": base, "": path.parse(request).base, "": basename,

  • <root>, ~ Root directory of the project (process.cwd() by default);
  • <request> The string passed to import;
  • <id> The name of requested file;
  • <base> The path to the folder where an import was performed from;
  • <basename> The name of the folder where an import was performed from.

Using original postcss-import options

You can define original postcss-import options as well as the usual. But in this case, the rules for a postcss-redirect-import is specified in the property sub.

subImport({
    root: path.join(process.cwd(), 'app'),
    redirect: {
      match: {...},
      use: {...}
    }
});

If you wanna to specify your own resolve function, keep in mind that your function will be called only in case of failure of sub.

Examples

Common theme

Append to all theme.css common theme.

git clone https://github.com/morulus/postcss-redirect-import.git
cd postcss-redirect-import/examples/common-theme
npm install
npm start

License

Under MIT license, 2016-2017, Vladimir Kalmykov [email protected]