Skip to content

Commit

Permalink
优化清除注释代码 支持sass,less
Browse files Browse the repository at this point in the history
  • Loading branch information
sunft1996 committed May 22, 2020
1 parent a1f4b0c commit ed19421
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 31 deletions.
50 changes: 38 additions & 12 deletions README.CN.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,68 @@
<!--
* @Descripttion:
* @Author: sunft
* @Date: 2020-05-22 13:55:05
* @LastEditTime: 2020-05-22 13:55:41
-->
<h1 align="center">sprite-smith-loader</h1>


![image](https://img.shields.io/badge/license-MIT-green)
![image](https://img.shields.io/badge/webpack-%5E4.0.0-blue)
## 介绍
sprite-smith-loader是一款自动生成雪碧图工具,你可以在webpack中使用它,它会自动生成雪碧图并修改相应的css代码。

支持**CSS****SASS****LESS**
## 安装
下载sprite-smith-loader

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

**webpack.config.js**

```
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader', 'css-loader','sprite-smith-loader'],
},
{
test: /\.(png|jpe?g|gif)$/i,
loader: 'file-loader',
options: {
outputPath: 'images',
},
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader','sprite-smith-loader'],
}
],
},
};
```
配合SASS

```
module.exports = {
module: {
rules: [
{
test: /\.(png|jpe?g|gif)$/i,
loader: 'file-loader',
options: {
outputPath: 'images',
},
},
{
test: /\.(sass|scss|css)/,
use: ['style-loader', 'css-loader','sprite-smith-loader',"sass-loader"],
}
],
},
};
```


## 使用
修改你的css文件
修改你的 CSS/SCSS/SASS/LESS 文件

```
.my_bg_1{
Expand Down
53 changes: 40 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
<!--
* @Descripttion:
* @Author: sunft
* @Date: 2020-05-22 13:54:30
* @LastEditTime: 2020-05-22 13:57:44
-->
<h1 align="center">sprite-smith-loader</h1>


Expand All @@ -13,31 +7,64 @@
:rocket: [中文文档点这里](https://github.com/sunft1996/sprite-smith-loader/blob/master/README.CN.md/)
## Introduce
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.

supports **CSS****SASS****LESS**.
## Getting Started
To begin, you'll need to install sprite-smith-loader:

```
npm install --save-dev css-loader
npm install --save-dev sprite-smith-loader
```
Then add the plugin to your webpack config. For example:
Then add the plugin to your webpack config. And don't forget to add the file-loader or url-loader.

For example:

**webpack.config.js**

```
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader', 'css-loader','sprite-smith-loader'],
},
{
test: /\.(png|jpe?g|gif)$/i,
loader: 'file-loader',
options: {
outputPath: 'images',
},
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader','sprite-smith-loader'],
},
],
},
};
```
with SaSS

```
module.exports = {
module: {
rules: [
{
test: /\.(png|jpe?g|gif)$/i,
loader: 'file-loader',
options: {
outputPath: 'images',
},
},
{
test: /\.(sass|scss|css)/,
use: ['style-loader', 'css-loader','sprite-smith-loader',"sass-loader"],
}
],
},
};
```

## Usage
Modify your CSS file
Modify your CSS/SCSS/SASS/LESS file
```
.my_bg_1{
height: 100px;
Expand Down
4 changes: 2 additions & 2 deletions demo/loaders/sprite-smith-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module.exports = function (source) {

let outputName = new Date().getTime() + '_smith.png';
// 清除css中的注释
const cleanSource = source.replace(/\/\*[\w\W]*\*\//g, '')

const cleanSource = source.replace(/(\/\*.*\*\/)|\/\/.*/g, '')
let imgs = cleanSource.match(/url\(.+_sprite.+\)/g);
if(!imgs) return cleanSource;

const callback = this.async();

Expand Down
Binary file removed demo/resource/sprites/1590130839494_smith.png
Binary file not shown.
Binary file removed demo/resource/sprites/1590130873790_smith.png
Binary file not shown.
2 changes: 1 addition & 1 deletion demo/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Descripttion:
* @Author: sunft
* @Date: 2020-03-24 11:19:44
* @LastEditTime: 2020-05-22 14:58:48
* @LastEditTime: 2020-05-22 18:12:59
*/
const path = require("path");
const webpack = require("webpack");
Expand Down
4 changes: 2 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module.exports = function (source) {

let outputName = new Date().getTime() + '_smith.png';
// 清除css中的注释
const cleanSource = source.replace(/\/\*[\w\W]*\*\//g, '')

const cleanSource = source.replace(/(\/\*.*\*\/)|\/\/.*/g, '')
let imgs = cleanSource.match(/url\(.+_sprite.+\)/g);
if(!imgs) return cleanSource;

const callback = this.async();

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sprite-smith-loader",
"version": "1.0.1",
"version": "1.0.2",
"author": "sunft",
"description": "css sprites loader for webpack",
"main": "dist/index.js",
Expand Down

0 comments on commit ed19421

Please sign in to comment.