-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
宋晶
committed
May 30, 2018
0 parents
commit 0b5b0ef
Showing
5 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/.idea | ||
/node_modules | ||
*.log | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Wed May 30 2018 14:22:54 GMT+0800 (中国标准时间) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |