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-import-alias-resolver

v0.1.1

Published

A tool to easily create custom alias resolver for postcss-import

Downloads

7,926

Readme

A tool to easily create custom alias resolver for postcss-import.

There are already modules that do this, but I encountered a situation when I couldn't make my setup to resolve everything correctly, either aliases or modules or direct link were not resolving. So I made my own configurable resolver.

Install

npm i postcss-import-alias-resolver

Update Log

  • 0.1.1 added resolution via package.json main field
  • 0.1.0 initial working sample

Usage

Webpack example

const resolver = require('postcss-import-alias-resolver');
const postcssLoader = {
  loader: 'postcss-loader',
  options: {
    plugins: {
      'postcss-import': {
        resolve: resolver(options)
      }
    }
  }
}

You can define aliases, extensions and modules directories to look in. You can also pass webpack config and it will take those settings from config.resolve object. By default aliases are prefixed with '~' so '@': 'src/assets' will become '~@': 'src/assets', this is done for compatibility with some environments and IDEs and can be disabled.

Options example

resolver({
  alias: {
    '@': path.resolve(__dirname, 'assets'),
    'comps': path.resolve(__dirname, 'src/components'),
    'static': path.resolve(__dirname, '/public'),
  },
  extensions: ['.css'],
  modules: ['assets/libs', 'node_modules'],
  
  dontPrefix: false, // do not enforce '~' prefix to aliases
  
  webpackConfig: require('./webpack.conf.js'),
  mergeAlias: 'extend',
  mergeModules: 'extend',
  mergeExtensions: 'replace',
  
  onResolve(id, base, resolvedPath) {
    console.log(`id ${id} resolved to ${resolvedPath}`)
    // if you want to override then return new path
    if (override === true)
      return 'some/new/path'
  },
  onFail(id, base) {
    console.log(`failed to resolve id ${id} from ${base}`)
    // if you want to override then return new path
    if (override === true)
      return 'some/new/path'     
  },
})

Structure example

/Users/user/dev/project
├── webpack.conf.js
├── node_modules
│   └── bulma
│       └── package.json
│           └── "main": "./lib/index.css"
├── public
│   └── file.css
├── assets
│   └── some.css
│   └── libs
│       └── bootstrap
│           └── index.css
└── src
    └── components
        ├── base.css
        └── theme.css

Resolve example with above config and structure

@import "~static/file.css";
/* /Users/user/dev/project/public//file.css */

@import "~@/some.css";
/* /Users/user/dev/project/assets/some.css */

@import "~bootstrap";
/* /Users/user/dev/project/assets/libs/bootstrap/index.css */

@import "~bulma";
/* /Users/user/dev/project/node_modules/bulma/lib/index.css */

@import "~comps/theme";
/* /Users/user/dev/project/src/components/theme.css */

API

resolver(options)

Creates a resolver with set options Options object accepts:

  • alias: an object with key/value pairs as alias/path:
    let alias = {
      '@': path.resolve(__dirname, 'assets'),
      'src': path.resolve(__dirname, 'src'),
      'static': path.resolve(__dirname, '/public'),
    }
  • extensions: list of extensions to try when looking for a file, if not passed and no webpack config then defaults to ['.css']
  • modules: list of directories to look into when aliases didn't match, if not passed and no webpack config then defaults to ['node_modules']
  • moduleFields: list of fields to look in package.json, default ['main']
  • webpackConfig: an object with webpack configuration that contains resolve field
  • mergeAlias: merge strategy for aliases 'extend' or 'replace', defaults to 'extend'
  • mergeModules: merge strategy for modules 'extend' or 'replace', defaults to 'extend'
  • mergeExtensions: merge strategy for extensions 'extend' or 'replace', defaults to 'replace'
  • dontPrefix: bool, if true then ~ prefix wont be enforced on aliases and it will look for exact match
  • logging: 'none' 'fail' 'match' 'all'
  • onResolve: function that will be called on each successful resolve, receives id, base, resolvedPath, if it returns a string then it will replace resolved path.
  • onFail: function that will be called on each failed resolve, receives id, base, if it returns a string then it will replace resolved path.