New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

weex-vue-precompiler

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

weex-vue-precompiler - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

87

hooks/style-binding.js

@@ -0,1 +1,8 @@

const esprima = require('esprima')
const escodegen = require('escodegen')
/**
* to wrap object leterals with _px2rem()
*/
const bindingStyleNamesForPx2Rem = [

@@ -27,4 +34,38 @@ 'width',

const ruleReg =/,?([^,:]+):([^,(]+(?:\([^)]+\)[^,}]*)?)/g
// // for test
// const exp = `a = { 'width': 'w', height: h, bgColor: _px2rem(bgColor, 75)}`
// const ast = esprima.parse(exp).body[0].expression.right
// console.log(JSON.stringify(ast, null, 2))
// transformObject(ast)
// let res = escodegen.generate(ast, {
// format: {
// indent: {
// style: ' '
// },
// newline: '',
// }
// })
// console.log('res:', res)
function transformObject (obj) {
const properties = obj.properties
for (let i = 0, l = properties.length; i < l; i++) {
const prop = properties[i]
const keyNode = prop.key
const keyType = keyNode.type
const key = keyType === 'Literal' ? keyNode.value : keyNode.name
const valNode = prop.value
if (bindingStyleNamesForPx2Rem.indexOf(key) > -1) {
prop.value = {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: '_px2rem'
},
arguments: [valNode, { type: 'Literal', value: 75 }]
}
}
}
}
module.exports = function styleBindingHook (

@@ -41,22 +82,32 @@ el,

}
const content = styleBinding.trim()
.replace(/\s*[{}]\s*/g, '')
let match
let res = '{'
while ((match = ruleReg.exec(content))) {
try {
let k = match[1]
let v = match[2]
k = k && k.trim()
v = v && v.trim()
if (k && bindingStyleNamesForPx2Rem.indexOf(k) > -1) {
v = `_px2rem(${v},75)`
const statement = `a = ${styleBinding.trim()}`
const ast = esprima.parse(statement).body[0].expression.right
if (ast.type === 'Identifier') {
return
}
if (ast.type === 'ArrayExpression') {
const elements = ast.elements
for (let i = 0, l = elements.length; i < l; i++) {
const element = elements[i]
if (element.type === 'ObjectExpression') {
transformObject(element)
}
res += `${k}:${v},`
} catch (err) {
console.error('[weex-vue-precompiler] style binding match error:', str)
}
}
res = res.substr(0, res.length - 1) + '}'
el.styleBinding = res
else if (ast.type === 'ObjectExpression') {
transformObject(ast)
}
else {
return
}
el.styleBinding = escodegen.generate(ast, {
format: {
indent: {
style: ' '
},
newline: '',
}
})
}
{
"name": "weex-vue-precompiler",
"version": "0.1.3",
"version": "0.1.4",
"description": "a precompiler for weex-vue-render.",

@@ -18,2 +18,4 @@ "main": "index.js",

"autoprefixer": "^7.1.6",
"escodegen": "^1.9.0",
"esprima": "^4.0.0",
"postcss": "^6.0.14",

@@ -20,0 +22,0 @@ "postcss-plugin-px2rem": "^0.7.0"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc