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

cssidreplace-loader

v1.0.7

Published

Another solution to CSS Module

Downloads

5

Readme

npm i -D cssidreplace-loader

讲一下我司的另一种css模块化解决方案,给每个模块自定义一个css key,比如说customHeader,在该模块的所有css样式签名都套上.customHeader
e.g.这是模块html文件

<header>
	<h1>this is header</h1>
	<p>css模块化解决方案</p>
</header>

以下是该模块css代码

.customHeader header{
	width: 990px;
	margin: 10px auto;
}
.customHeader h1{
	font-style: italic;
}
.customHeader p{
	color: #ccc;
}

webpack打包的时候给该模块css key添加一个随机唯一的数值生成最终的cssId,e.g.customHeader-123456,生成最终文件代码如下:

<style type="text/css">
.customHeader-123456 header{
	width: 990px;
	margin: 10px auto;
}
.customHeader-123456 h1{
	font-style: italic;
}
.customHeader-123456 p{
	color: #ccc;
}
</style>
<div class="customHeader-123456">
	<header>
		<h1>this is header</h1>
		<p>css模块化解决方案</p>
	</header>
</div>

cssidreplace-loader的作用就是在webpack打包时把css代码中的css key替换成css Id(即是把customHeader替换成customHeader-123456)。
可配置参数有两个:regexsubregex指待替换css key,sub指css Id,在webpack配置文件中代码如下:

{
  test: /\.css$/,
  use: [
	{
	  loader: "style-loader"
	},
	{
	  loader: "css-loader"
	},
	{
	  loader: 'cssidreplace-loader',
	  options: {
	    regex: 'customHeader',
	    sub: 'customHeader-123456'
	  }
	}
  ]
}

每个模块的css Id都是不同的,咱们可以通过通过动态配置webpack给对应的模块替换上相应的css Id

// 注意:默认模块名称跟模块文件夹名称是一致的
// var modulesMap = [  // 举例,假设这个是所有模块的索引
//     {
//         name: 'header', // 模块名称
//         cssKey: 'customHeader',
//         cssId: 'customHeader-123456'
//     },
//     {
//         name: 'footer',
//         cssKey: 'customFooter',
//         cssId: 'customFooter-323122'
//     },
// ];
const webpack = require('webpack');
const webpack_config = require('./webpack.config');
const webpackDevServer = require('webpack-dev-server');
modulesMap.forEach((module) => {
  webpack_config.module.rules.unshift({
    test: eval("/\.css$/i"),
    include: eval(module.name),
    use: [
      	{
      	  loader: "style-loader"
      	},
      	{
      	  loader: "css-loader"
      	},
      	{
      	  loader: 'cssidreplace-loader',
      	  options: {
      	    regex: module.cssKey,
      	    sub: module.cssId
      	  }
      	}
    ]
  });
});
//webpack dev server
const compiler = webpack(webpack_config);
new webpackDevServer(compiler, {
  stats: {
    colors: true,
    chunks: false
  },
  noInfo: false,
  proxy: {
    '*': {
      target: 'http://localhost:3000',
    }
  }
}).listen(8080, function(){console.log('App (dev) is now running on port 8080!');});