Skip to content

Commit

Permalink
feat: add test; add debugMode params;
Browse files Browse the repository at this point in the history
  • Loading branch information
huixisheng committed Jul 6, 2018
1 parent e747bb2 commit aef2afc
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wepy-plugin-image",
"version": "0.0.2",
"version": "0.0.3",
"description": "wepy插件根据图片大小是否转base64、上传七牛",
"main": "src/index.js",
"scripts": {
Expand Down
31 changes: 26 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,40 @@ const validator = require('validator');

class ImageParse {
constructor(cfg = {}) {
// TODO filter
this.config = Object.assign({
limit: 1024,
debugMode: false,
}, cfg);
}

apply(op) {
const { code, file } = op;
const config = this.config;
console.log(file);
const { debugMode } = config;
if (debugMode) {
console.log('wepy-plugin-image file', file);
}

const reg = /url\((.*?)\)/gi;
if (!code) {
console.error('code is null');
if (debugMode) {
console.error('code is null');
}
op.next();
} else {
const bgPaths = code.match(reg) || [];
console.log('bgPaths', bgPaths);
if (debugMode) {
console.log('wepy-plugin-image bgPaths', bgPaths);
}

const base64List = [];
const uploadList = [];
bgPaths.forEach((item) => {
const bgImage = item.replace('url(', '').replace(/\)$/g, '');
const bgImage = item.replace(/'/g, '').replace(/"/g, '').replace('url(', '').replace(/\)$/g, '');
// TODO 本身是base64, 绝对地址的图片
if (!validator.isURL(bgImage)) {
// 图片做忽略,不做拷贝,取的是src的图片
const bgPath = path.join(path.dirname(file.replace('dist', 'src')), bgImage);
const stats = fs.statSync(bgPath);
if (fs.existsSync(bgPath)) {
Expand All @@ -45,7 +56,9 @@ class ImageParse {
});
}
} else {
console.error('%s不存在', bgPath);
if (debugMode) {
console.error('%s不存在', bgPath);
}
}
}
});
Expand All @@ -60,6 +73,14 @@ class ImageParse {
uploadList.forEach((uploadfile) => {
promiseUploadList.push(packQiniu(uploadfile.path, uploadfile));
});
// 无图片的时候
if (!promiseUploadList.length) {
if (debugMode) {
console.log('wepy-plugin-image no upload image');
}
op.next();
return;
}
Promise.all(promiseUploadList).then((resultList) => {
resultList.forEach((item) => {
const bgUrl = item.options.bg;
Expand Down
Binary file added test/fixtures/image/alipay.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/image/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions test/fixtures/test.wxss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.alipay { background-image: url(image/alipay.png); }
.icon { background-image: url(image/icon.png); }
2 changes: 2 additions & 0 deletions test/fixtures/test1.wxss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.alipay { width: 20px; height: 20px; }
.icon { display: flex; justify-content: center; align-items: center; }
2 changes: 2 additions & 0 deletions test/fixtures/test2.wxss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.alipay { background-image: url("image/alipay.png"); }
.icon { background-image: url('image/icon.png'); }
92 changes: 86 additions & 6 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,95 @@
const ImageParse = require('../src/index');
const { expect } = require('chai');
const fs = require('fs');
const path = require('path');

// TODO添加测试用例
describe('正则匹配', function () {
it('1 加 1 应该等于 2', function () {
describe('正常数据', function () {
it('无单引和双引的数据', function (done) {
const instance = new ImageParse();
const filepath = path.join(__dirname, 'fixtures/test.wxss');
const code = fs.readFileSync(filepath, 'utf8');
const data = {
// code: '.panel {\n width: 100%;\n margin-top: 20rpx;\n text-align: left;\n font-size: 12px;\n padding-top: 20rpx;\n padding-left: 50rpx;\n padding-bottom: 20rpx;\n border: 1px solid #ccc;\n}\n.panel .title {\n padding-bottom: 20rpx;\n font-size: 14px;\n font-weight: bold;\n}\n.panel .info {\n padding: 15rpx;\n}\n.panel .testcounter {\n margin-top: 15rpx;\n position: absolute;\n}\n.panel .counterview {\n margin-left: 120rpx;\n}\n.panel .image {\n width: 20px;\n height: 20px;\n background-image: url(image/icon-super@2x.png);\n}\n',
// file: '/Users/huixisheng/Workspaces/dist/components/member-card.wxss',
code,
file: filepath,
next() {
const expectValue = `.alipay { background-image: url(https://img0.cosmeapp.com/Fg0s0GU_xDPd2rOgAYYikJDTiEhL); }
.icon { background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAAAXNSR0IArs4c6QAAAD1JREFUSA1jYBgFoyEwGgKjITAaAkMuBBhxufjs2bP/cckRI25sbIzVbCZiNI+qGQ2B0RAYDYHREBjhIQAADF0EBNEtJ20AAAAASUVORK5CYII=); }`;
expect(this.code).to.be.equal(expectValue);
done();
},
};
instance.apply(data);
});

it('单引和双引的数据', function (done) {
const instance = new ImageParse();
const filepath = path.join(__dirname, 'fixtures/test2.wxss');
const code = fs.readFileSync(filepath, 'utf8');
const data = {
code,
file: filepath,
next() {
const expectValue = `.alipay { background-image: url("https://img0.cosmeapp.com/Fg0s0GU_xDPd2rOgAYYikJDTiEhL"); }
.icon { background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAAAXNSR0IArs4c6QAAAD1JREFUSA1jYBgFoyEwGgKjITAaAkMuBBhxufjs2bP/cckRI25sbIzVbCZiNI+qGQ2B0RAYDYHREBjhIQAADF0EBNEtJ20AAAAASUVORK5CYII='); }`;
expect(this.code).to.be.equal(expectValue);
done();
},
};
instance.apply(data);
});

it('debugMode', function (done) {
const instance = new ImageParse({
debugMode: true,
});
const filepath = path.join(__dirname, 'fixtures/test.wxss');
const code = fs.readFileSync(filepath, 'utf8');
const data = {
code,
file: filepath,
next() {
const expectValue = `.alipay { background-image: url(https://img0.cosmeapp.com/Fg0s0GU_xDPd2rOgAYYikJDTiEhL); }
.icon { background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAAAXNSR0IArs4c6QAAAD1JREFUSA1jYBgFoyEwGgKjITAaAkMuBBhxufjs2bP/cckRI25sbIzVbCZiNI+qGQ2B0RAYDYHREBjhIQAADF0EBNEtJ20AAAAASUVORK5CYII=); }`;
expect(this.code).to.be.equal(expectValue);
done();
},
};
instance.apply(data);
});

it('code null', function (done) {
const instance = new ImageParse({
debugMode: true,
});
const filepath = path.join(__dirname, 'fixtures/test.wxss');
const data = {
code: null,
file: filepath,
next() {
expect(this.code).to.be.equal(null);
done();
},
};
instance.apply(data);
});


it('no upload image', function (done) {
const instance = new ImageParse({
debugMode: true,
});
const filepath = path.join(__dirname, 'fixtures/test1.wxss');
const code = fs.readFileSync(filepath, 'utf8');
const data = {
code,
file: filepath,
next() {
const expectValue = `.alipay { width: 20px; height: 20px; }
.icon { display: flex; justify-content: center; align-items: center; }`;
expect(this.code).to.be.equal(expectValue);
done();
},
};
instance.apply(data);
expect(2).to.be.equal(2);
});
});

0 comments on commit aef2afc

Please sign in to comment.