Skip to content

Commit

Permalink
feat: 支持背景图片为http的地址,支持图片不存在的提示
Browse files Browse the repository at this point in the history
  • Loading branch information
huixisheng committed May 22, 2018
1 parent 5eebaf9 commit e747bb2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
node_modules/
dist/
npm-debug.log
package-lock.json
2 changes: 1 addition & 1 deletion .gitmessage
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# <body>用于详细描述本次变更的内容
#
# <footer>提供解决问题的文章地址或者测试的链接地址
# 例如: closes #traup_id fix #traup_id http://test.h5.cosmeapp.net/group/thread/250411
# 例如: closes #traup_id fix #traup_id
#
# <type类型>
# build: 构建工具生成资源
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wepy-plugin-image",
"version": "0.0.1",
"version": "0.0.2",
"description": "wepy插件根据图片大小是否转base64、上传七牛",
"main": "src/index.js",
"scripts": {
Expand All @@ -9,7 +9,6 @@
"eslint": "cross-env NODE_ENV=production node_modules/eslint/bin/eslint.js --max-warnings 10 --ext .js,.vue static src build config test fis-conf.js",
"build:dev": "cross-env NODE_ENV=development webpack --progress --hide-modules --colors",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules --colors",
"preinstall": "git config --local commit.template ./.gitmessage",
"commitmsg": "commitlint -e $GIT_PARAMS"
},
"keywords": [],
Expand All @@ -18,7 +17,8 @@
"dependencies": {
"babel-preset-es2015": "^6.24.1",
"mime": "^2.2.0",
"pack-qiniu": "^1.0.1"
"pack-qiniu": "^1.0.1",
"validator": "^10.2.0"
},
"husky": {
"hooks": {
Expand Down
32 changes: 20 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const path = require('path');
const fs = require('fs');
const mime = require('mime');
// TODO key 没有配置的提示
const packQiniu = require('pack-qiniu');
const validator = require('validator');

class ImageParse {
constructor(cfg = {}) {
Expand All @@ -27,18 +29,24 @@ class ImageParse {
const uploadList = [];
bgPaths.forEach((item) => {
const bgImage = item.replace('url(', '').replace(/\)$/g, '');
const bgPath = path.join(path.dirname(file.replace('dist', 'src')), bgImage);
const stats = fs.statSync(bgPath);
if (stats.size > config.limit) {
uploadList.push({
path: bgPath,
bg: bgImage,
});
} else {
base64List.push({
path: bgPath,
bg: bgImage,
});
if (!validator.isURL(bgImage)) {
const bgPath = path.join(path.dirname(file.replace('dist', 'src')), bgImage);
const stats = fs.statSync(bgPath);
if (fs.existsSync(bgPath)) {
if (stats.size > config.limit) {
uploadList.push({
path: bgPath,
bg: bgImage,
});
} else {
base64List.push({
path: bgPath,
bg: bgImage,
});
}
} else {
console.error('%s不存在', bgPath);
}
}
});
base64List.forEach((base64file) => {
Expand Down
1 change: 1 addition & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const ImageParse = require('../src/index');
const { expect } = require('chai');

// TODO添加测试用例
describe('正则匹配', function () {
it('1 加 1 应该等于 2', function () {
const instance = new ImageParse();
Expand Down

0 comments on commit e747bb2

Please sign in to comment.