Skip to content

Commit

Permalink
2018.5.30
Browse files Browse the repository at this point in the history
  • Loading branch information
宋晶 committed May 30, 2018
0 parents commit 0b5b0ef
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.idea
/node_modules
*.log
package-lock.json
1 change: 1 addition & 0 deletions .npminstall.done
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Wed May 30 2018 14:22:54 GMT+0800 (中国标准时间)
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var utils = require('./utils');

module.exports = function (source) {
if (!this.cacheable || !this.query) {
return source;
}
this.cacheable();

var query = Object.assign({},{
viewportWidth: 750,
viewportUnit: 'vw',
minPixelValue:1,
decimal:3
},utils.getQueryString(this.query));

var matchPXExp = /([0-9.]+px)([;,| |}|'|"\)\r|\n])/g;

return source.replace(matchPXExp, function (match, m1, m2) {
var pixels = parseFloat(m1.slice(0, m1.length - 2));
if (pixels <= query.minPixelValue) {
return match;
}
return utils.createPxReplace(pixels, query.viewportWidth, query.decimal, query.viewportUnit) + m2;
});

};
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "webpack-px2vw-loader",
"version": "1.0.0",
"description": "transform px to vw",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index"
},
"repository": {
"type": "git",
"url": "https://github.com/ashleygwilliams/my_package.git"
},
"author": "herorest",
"license": "ISC"
}
24 changes: 24 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var util = require('util');

var utils = {
createPxReplace : function(pixels, viewportSize, unitPrecision, viewportUnit) {
return (pixels / viewportSize * 100).toFixed(unitPrecision) + viewportUnit;
},

getQueryString : function(query){
if(!query) {
return {};
}
if(typeof query !== "string" && typeof query !== 'object') {
util.error('query need use json type');
return {};
}
if(typeof query === "string" && query.substr(0, 1) === "{" && query.substr(-1) === "}"){
return JSON.parse(query);
}else{
return query;
}
}
}

module.exports = utils;

0 comments on commit 0b5b0ef

Please sign in to comment.