Skip to content

Commit ed19421

Browse files
committed
优化清除注释代码 支持sass,less
1 parent a1f4b0c commit ed19421

File tree

8 files changed

+84
-31
lines changed

8 files changed

+84
-31
lines changed

README.CN.md

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,68 @@
1-
<!--
2-
* @Descripttion:
3-
* @Author: sunft
4-
* @Date: 2020-05-22 13:55:05
5-
* @LastEditTime: 2020-05-22 13:55:41
6-
-->
71
<h1 align="center">sprite-smith-loader</h1>
82

93

104
![image](https://img.shields.io/badge/license-MIT-green)
115
![image](https://img.shields.io/badge/webpack-%5E4.0.0-blue)
126
## 介绍
137
sprite-smith-loader是一款自动生成雪碧图工具,你可以在webpack中使用它,它会自动生成雪碧图并修改相应的css代码。
8+
9+
支持**CSS****SASS****LESS**
1410
## 安装
1511
下载sprite-smith-loader
1612

1713
```
1814
npm install --save-dev sprite-smith-loader
1915
```
2016
## 配置
21-
将loader添加到webpack的配置中,必须放在css-loader之后
17+
将loader添加到webpack的配置中,必须放在css-loader之后,别忘了添加file-loader或url-loader处理图片路径。
2218

2319
**webpack.config.js**
2420

2521
```
2622
module.exports = {
2723
module: {
2824
rules: [
29-
{
30-
test: /\.css$/i,
31-
use: ['style-loader', 'css-loader','sprite-smith-loader'],
32-
},
25+
{
26+
test: /\.(png|jpe?g|gif)$/i,
27+
loader: 'file-loader',
28+
options: {
29+
outputPath: 'images',
30+
},
31+
},
32+
{
33+
test: /\.css$/i,
34+
use: ['style-loader', 'css-loader','sprite-smith-loader'],
35+
}
36+
],
37+
},
38+
};
39+
```
40+
配合SASS
41+
42+
```
43+
module.exports = {
44+
module: {
45+
rules: [
46+
{
47+
test: /\.(png|jpe?g|gif)$/i,
48+
loader: 'file-loader',
49+
options: {
50+
outputPath: 'images',
51+
},
52+
},
53+
{
54+
test: /\.(sass|scss|css)/,
55+
use: ['style-loader', 'css-loader','sprite-smith-loader',"sass-loader"],
56+
}
3357
],
3458
},
3559
};
60+
3661
```
3762

63+
3864
## 使用
39-
修改你的css文件
65+
修改你的 CSS/SCSS/SASS/LESS 文件
4066

4167
```
4268
.my_bg_1{

README.md

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
<!--
2-
* @Descripttion:
3-
* @Author: sunft
4-
* @Date: 2020-05-22 13:54:30
5-
* @LastEditTime: 2020-05-22 13:57:44
6-
-->
71
<h1 align="center">sprite-smith-loader</h1>
82

93

@@ -13,31 +7,64 @@
137
:rocket: [中文文档点这里](https://github.com/sunft1996/sprite-smith-loader/blob/master/README.CN.md/)
148
## Introduce
159
Sprite Smith loader is a tool for automatically generating CSS Sprites. You can use it in webpack. It will automatically generate CSS Sprites and modify the corresponding CSS code.
10+
11+
supports **CSS****SASS****LESS**.
1612
## Getting Started
1713
To begin, you'll need to install sprite-smith-loader:
1814

1915
```
20-
npm install --save-dev css-loader
16+
npm install --save-dev sprite-smith-loader
2117
```
22-
Then add the plugin to your webpack config. For example:
18+
Then add the plugin to your webpack config. And don't forget to add the file-loader or url-loader.
19+
20+
For example:
2321

2422
**webpack.config.js**
2523

2624
```
2725
module.exports = {
2826
module: {
2927
rules: [
30-
{
31-
test: /\.css$/i,
32-
use: ['style-loader', 'css-loader','sprite-smith-loader'],
33-
},
28+
{
29+
test: /\.(png|jpe?g|gif)$/i,
30+
loader: 'file-loader',
31+
options: {
32+
outputPath: 'images',
33+
},
34+
},
35+
{
36+
test: /\.css$/i,
37+
use: ['style-loader', 'css-loader','sprite-smith-loader'],
38+
},
39+
],
40+
},
41+
};
42+
```
43+
with SaSS
44+
45+
```
46+
module.exports = {
47+
module: {
48+
rules: [
49+
{
50+
test: /\.(png|jpe?g|gif)$/i,
51+
loader: 'file-loader',
52+
options: {
53+
outputPath: 'images',
54+
},
55+
},
56+
{
57+
test: /\.(sass|scss|css)/,
58+
use: ['style-loader', 'css-loader','sprite-smith-loader',"sass-loader"],
59+
}
3460
],
3561
},
3662
};
63+
3764
```
3865

3966
## Usage
40-
Modify your CSS file
67+
Modify your CSS/SCSS/SASS/LESS file
4168
```
4269
.my_bg_1{
4370
height: 100px;

demo/loaders/sprite-smith-loader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ module.exports = function (source) {
88

99
let outputName = new Date().getTime() + '_smith.png';
1010
// 清除css中的注释
11-
const cleanSource = source.replace(/\/\*[\w\W]*\*\//g, '')
12-
11+
const cleanSource = source.replace(/(\/\*.*\*\/)|\/\/.*/g, '')
1312
let imgs = cleanSource.match(/url\(.+_sprite.+\)/g);
13+
if(!imgs) return cleanSource;
1414

1515
const callback = this.async();
1616

-10.8 KB
Binary file not shown.
-10.8 KB
Binary file not shown.

demo/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Descripttion:
33
* @Author: sunft
44
* @Date: 2020-03-24 11:19:44
5-
* @LastEditTime: 2020-05-22 14:58:48
5+
* @LastEditTime: 2020-05-22 18:12:59
66
*/
77
const path = require("path");
88
const webpack = require("webpack");

dist/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ module.exports = function (source) {
88

99
let outputName = new Date().getTime() + '_smith.png';
1010
// 清除css中的注释
11-
const cleanSource = source.replace(/\/\*[\w\W]*\*\//g, '')
12-
11+
const cleanSource = source.replace(/(\/\*.*\*\/)|\/\/.*/g, '')
1312
let imgs = cleanSource.match(/url\(.+_sprite.+\)/g);
13+
if(!imgs) return cleanSource;
1414

1515
const callback = this.async();
1616

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sprite-smith-loader",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"author": "sunft",
55
"description": "css sprites loader for webpack",
66
"main": "dist/index.js",

0 commit comments

Comments
 (0)