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 🙏

© 2026 – Pkg Stats / Ryan Hefner

c-loader

v1.1.0

Published

组件开发时,使用Webpack自动引用组件样式文件

Readme

c-loader

c-loader是一个组件式开发的webpack loader,它可以根据模块引入时的参数来决定加载模块的依赖样式文件。

Install

npm install c-loader --save-dev or yarn add c-loader --dev

c-loader configs

  • css: tell c-loader to load style
  • postfix: tell c-loader what the style's postfix is, 'css' by default
  • module: [added in v0.4.0] tell c-loader what kind of import used, 'require' by default
  • onlycss: [added in v0.5.0] tell c-loader to load the style only (in react, when you just want to render the component's markup in server, not in client side, then you should load the style without the component's react codes)

css options Can use interpolate names now (>= v0.8.0)

Start with v0.8.0, you can use the interpolate names in c-loader. Exp.:

var dialog = require('./dialog?css=./[path][name].less&postfix=css');

c-loader will resolve the interpolate names, and use the resolved name as the request style name.

P.S. see interpolateName for more detail

Usage 1

// in webpack.config.js
  module: {
    loaders: [{
      test: /\.js(?:\?.+)?$/,
      loader: 'c-loader'
    }, {
      test: /\.css(?:\?.+)?$/,
      loader: 'style!css' // because c-loader will require the css for the required module, you need the style and css loader to handle the css
    }]
  }
  // in your module
  var dialog = require('./dialog?css&postfix=css');
  // then the c-loader will product a require expression for the dialog module. In this example, it will require index.css relative to dialog file.
  // In fact, in webpack, c-loader will product like this:
  require('./index.css');
  ...// Here is dialog file content.

Usage 2

Also, you can config the css and postfix in webpack.config.js instead of in require or import statement.

// in webpack.config.js
  module: {
    loaders: [{
      test: /\.js(?:\?.+)?$/,
      loader: 'c-loader?css&postfix=less'
    }, {
      test: /\.css(?:\?.+)?$/,
      loader: 'style!css' // because c-loader will require the css for the required module, you need the style and css loader to handle the css
    }]
  }

Then in your module

var dialog = require('./dialog'); // no css and postfix here

or

import dialog from './dialog'); // no css and postfix here

How To Determin The Style File To Be Used

c-loader will resolve the require query, and determin the style file to be used. c-loader resolves the query and gets the 'css' value, then require the file to the value.

require('./dialog')

If there is no 'css' query in the require statement, c-loader will not load any style.

require('./dialog?css') | require('./dialog?css=')

If the 'css' query gets null or empty, c-loader will load the default style - index.css.

require('./dialog?css=styles/my-dialog')

If the 'css' query gets a value, c-loader will load the given style - styles/my-dialog.css.

Also, you can use less/sass or any other style. Just add the postfix, if not, c-loader will recognize it as a '.css' file.

Like this:

require('./dialog?css=styles/my-dialog.less');

Then c-loader will keep the less, and load styles/my-dialog.less.

REMEMBER: c-loader does not resolve the style, it just load it. You need use corresponding loaders to resolve them.

How To Determin The Style File's Context

After c-loader gets the 'css' query, it needs determin the context to get the file. For local module and NPM module, c-loader has a rule for them. This rule depends on the 'css' value.

'css' Has A Value With './' prefixed [Added in v0.8.0]

If the required module gets a 'css' value and 'css' starts with './', then use the 'context of the moudle' as the style's context. Exp:

var localDialog = require('./dialog?css=./[name].less'); // Local Module

var npmDialog = require('dialog?css=./[name].less'); // NPM Module

then, c-loader will load '{dialog_context}/dialog.less' for localDialog.

And load 'node_modules/dialog/dialog.less' for npmDialog.

'css' Has A Value

If the required module gets a 'css' value, use the 'context' option in the webpack.config.js as the style's context. Exp:

// in webpack.config.js
module.exports = {
  context: __dirname,
  // ... other configs
}

Then c-loader will get __dirname as the style's context. If we have:

require('./dialog?css=styles/my-dialog.less'); // Local Module
// or
require('dialog?css=styles/my-dialog.less'); // NPM Module

then, c-loader will load the '{webpack.config.context}/styles/my-dialog.less'.

'css' is Null or Empty

If the required module gets a null or empty 'css', use the 'context of the module' as the style's context. Exp:

var localDialog = require('./dialog?css'); // Local Module

var npmDialog = require('dialog?css'); // NPM Module

then, c-loader will load '{dialog_context}/styles/my-dialog.less' for localDialog.

And load 'node_modules/dialog/styles/my-dialog.less' for npmDialog.

REMEMBER: c-loader does not resolve the style, it just load it. You need use corresponding loaders to resolve them.