vue-template-babel-compiler
Advanced tools
Comparing version 0.3.0-0 to 0.4.0-0
'use strict'; | ||
var babel$2 = require("@babel/core"); | ||
var t$2 = require('@babel/types'); | ||
var t$2 = babel$2.types; | ||
var vueModelName = '_vm'; | ||
var createEleName = '_h'; | ||
var renderFuncName = '_c'; | ||
var WithStatementReplaceComment = '__VUE_TEMPLATE_BABEL_COMPILER_WITH_PLACEHOLDER__'; | ||
function parseWithStatementToVm() { | ||
@@ -13,4 +13,5 @@ return { | ||
WithStatement(path) { | ||
var curNodeBody = path.node.body.body[0]; | ||
path.replaceWithMultiple([t$2.variableDeclaration('var', [t$2.variableDeclarator(t$2.identifier(vueModelName), t$2.thisExpression())]), t$2.variableDeclaration('var', [t$2.variableDeclarator(t$2.identifier(createEleName), t$2.memberExpression(t$2.identifier(vueModelName), t$2.identifier('$createElement')))]), t$2.variableDeclaration('var', [t$2.variableDeclarator(t$2.identifier(renderFuncName), t$2.logicalExpression('||', t$2.memberExpression(t$2.memberExpression(t$2.identifier(vueModelName), t$2.identifier('_self')), t$2.identifier(renderFuncName)), t$2.identifier(createEleName)))]), curNodeBody]); | ||
var withStatementReturnBody = path.node.body.body[0]; | ||
t$2.addComment(withStatementReturnBody, "leading", WithStatementReplaceComment); | ||
path.replaceWithMultiple([withStatementReturnBody]); | ||
} | ||
@@ -40,2 +41,3 @@ | ||
var t$1 = require('@babel/types'); | ||
var RENDER_NAME = '__render__'; | ||
@@ -66,5 +68,4 @@ var STATIC_RENDER_FNS_NAME = '__staticRenderFns__'; | ||
return isRenderFunc(path.node); | ||
} // TODO support staticRenderFns = [renderFunc] | ||
} | ||
function shouldPrependVmNew(path) { | ||
@@ -77,58 +78,15 @@ var _scope$block$params; | ||
var scope = path.scope; | ||
var notProgramScope = !t$1.isProgram(scope.path); | ||
var notRenderFunc = !(t$1.isVariableDeclarator(parent) && nodeName === RENDER_NAME); | ||
var withinWith = notProgramScope && notPreserveName(nodeName) && withinRenderFunc(scope.path); | ||
/* | ||
// 2. not id of a Declaration: | ||
!(isDeclaration(identifier.parent.type) && identifier.parent.id === identifier) && | ||
*/ | ||
var notIdOfDeclaration = !t$1.isVariableDeclarator(parent); | ||
/* | ||
//3. not a params of a function | ||
!(isFunction(identifier.parent.type) && identifier.parent.params.indexOf(identifier) > -1) && | ||
*/ | ||
var notParamsOfFunction = !(t$1.isFunctionExpression(parent) && parent.params.indexOf(node) > -1); | ||
/* | ||
// 4. not a key of Property | ||
eg: var obj = {a: 1} 中的 a | ||
!(identifier.parent.type === 'Property' && identifier.parent.key === identifier && !identifier.parent.computed) && | ||
*/ | ||
var notKeyOfProperty = !(t$1.isObjectProperty(parent) && path.parent.key === node); | ||
/* | ||
// 5. not a property of a MemberExpression | ||
!(identifier.parent.type === 'MemberExpression' && identifier.parent.property === identifier && !identifier.parent.computed) && | ||
*/ | ||
var notPropertyOfMemberExpression = !(t$1.isMemberExpression(parent) && path.parent.property === node); | ||
/* | ||
// not in an Array destructure pattern | ||
!(identifier.parent.type === 'ArrayPattern') && | ||
*/ | ||
var notInArrayDestructure = !t$1.isArrayPattern(parent); | ||
/* | ||
// not in an Object destructure pattern | ||
!(identifier.parent.parent.type === 'ObjectPattern') && | ||
*/ | ||
var notInObjectDestructure = !t$1.isObjectPattern(parent.parent); | ||
/* | ||
// skip globals + commonly used shorthands | ||
!globals[identifier.name] && | ||
*/ | ||
var notGlobalShorthands = !hash[nodeName]; | ||
/* | ||
// not already in scope | ||
!identifier.findScope(false).contains(identifier.name) | ||
*/ | ||
var notFunctionParam = !(scope !== null && scope !== void 0 && (_scope$block$params = scope.block.params) !== null && _scope$block$params !== void 0 && _scope$block$params.find(node => node.name === nodeName)); | ||
var notAlreadyInScope = !scope.bindings[nodeName] && notFunctionParam; // TODO return directly | ||
var ret = notRenderFunc && withinWith && notIdOfDeclaration && notParamsOfFunction && notPropertyOfMemberExpression && notKeyOfProperty && notInArrayDestructure && notInObjectDestructure && notGlobalShorthands && notAlreadyInScope; | ||
return ret; | ||
if (!t$1.isProgram(scope.path) && !(t$1.isVariableDeclarator(parent) && nodeName === RENDER_NAME) && notPreserveName(nodeName) && withinRenderFunc(scope.path) // not id of a Declaration: | ||
&& !t$1.isVariableDeclarator(parent) // not a params of a function | ||
&& !(t$1.isFunctionExpression(parent) && parent.params.indexOf(node) > -1) // not a key of Property | ||
&& !(t$1.isObjectProperty(parent) && path.parent.key === node) // not a property of a MemberExpression | ||
&& !(t$1.isMemberExpression(parent) && path.parent.property === node) // not in an Array destructure pattern | ||
&& !t$1.isArrayPattern(parent) // not in an Object destructure pattern | ||
&& !t$1.isObjectPattern(parent.parent) // skip globals + commonly used shorthands | ||
&& !hash[nodeName] // not cur function param | ||
&& !(scope !== null && scope !== void 0 && (_scope$block$params = scope.block.params) !== null && _scope$block$params !== void 0 && _scope$block$params.find(node => node.name === nodeName)) // not already in scope | ||
&& !scope.bindings[nodeName]) { | ||
return true; | ||
} | ||
} | ||
@@ -152,16 +110,16 @@ | ||
var babel = require('@babel/core'); // TODO es6 module || cjs | ||
function escapeRegExp(string) { | ||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
} | ||
module.exports = function transpile(code, opts) { | ||
var babel = require('@babel/core'); | ||
var matchWithRegex = new RegExp(escapeRegExp(`/*${WithStatementReplaceComment}*/`), 'g'); | ||
module.exports = function transpile(code) { | ||
// console.log('input code = ', code) | ||
// TODO opts | ||
// if (opts) { | ||
// opts = Object.assign({}, defaultOptions, opts) | ||
// opts.transforms = Object.assign({}, defaultOptions.transforms, opts.transforms) | ||
// } else { | ||
// opts = defaultOptions | ||
// } | ||
var output = babel.transformSync(code, { | ||
filename: 'compiledTemplate', | ||
// not enable strict mode for WithStatement | ||
// not enable strict mode, in order to parse WithStatement | ||
sourceType: 'script', | ||
@@ -174,5 +132,6 @@ assumptions: { | ||
}], '@babel/plugin-transform-spread', '@babel/plugin-transform-arrow-functions', '@babel/plugin-transform-parameters', parseWithStatementToVm, prependVm] | ||
}); // console.log(output.code) | ||
}); | ||
output.code = output.code.replace(matchWithRegex, 'var _vm=this;\nvar _h=_vm.$createElement;\nvar _c=_vm._self._c||_h;\n'); // console.log(output.code) | ||
return output.code; | ||
}; |
{ | ||
"name": "vue-template-babel-compiler", | ||
"version": "0.3.0-0", | ||
"version": "0.4.0-0", | ||
"description": "Post compiler for Vue template render functions to support ES features with Babel", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8114
107