vue-template-babel-compiler
Advanced tools
Comparing version 0.7.0 to 1.0.0-0
@@ -78,7 +78,4 @@ 'use strict'; | ||
const matchWithRegex = new RegExp(escapeRegExp(`/*${WithStatementReplaceComment}*/`), 'g'); | ||
module.exports = function transpile(code) { | ||
// console.log('input code = ', code) | ||
// TODO opts | ||
let output = babel.transformSync(code, { | ||
function compileRenderCode(code) { | ||
const output = babel.transformSync(code, { | ||
filename: 'compiledTemplate', | ||
@@ -89,4 +86,74 @@ // not enable strict mode, in order to parse WithStatement | ||
}); | ||
output.code = output.code.replace(matchWithRegex, 'var _vm=this;\nvar _h=_vm.$createElement;\nvar _c=_vm._self._c||_h;\n'); | ||
return output.code; | ||
return output.code.replace(matchWithRegex, 'var _vm=this;\n var _h=_vm.$createElement;\n var _c=_vm._self._c||_h;\n'); | ||
} | ||
const compiler = require('vue-template-compiler'); | ||
function toFunction(code, isFunctional) { | ||
return `function (${isFunctional ? `_h,_vm` : ``}) {${code}}`; | ||
} | ||
function getMarkRange(code, startMark, endMark) { | ||
if (!code || !code.indexOf) { | ||
// TODO handle edge error | ||
return false; | ||
} | ||
return { | ||
start: code.indexOf(startMark) + 1, | ||
end: code.lastIndexOf(endMark) | ||
}; | ||
} | ||
function getFunctionBody(code) { | ||
const range = getMarkRange(code, '{', '}'); | ||
return code.substring(range.start, range.end); | ||
} | ||
function getArrayItems(code) { | ||
const range = getMarkRange(code, '[', ']'); | ||
return code.substring(range.start, range.end).split('function').filter(functionBodyStr => Boolean(functionBodyStr)).map(getFunctionBody); | ||
} | ||
const renderSeparator = '/* renderSeparator */'; | ||
function compileTemplateCode(source, options) { | ||
var _options$transforms; | ||
const isFunctional = options === null || options === void 0 ? void 0 : (_options$transforms = options.transforms) === null || _options$transforms === void 0 ? void 0 : _options$transforms.stripWithFunctional; | ||
const { | ||
ast, | ||
render, | ||
staticRenderFns, | ||
tips, | ||
errors | ||
} = compiler.compile(source, options); // TODO rm semicolon && \n : https://babeljs.io/docs/en/options#minified | ||
let code = `var render = ${toFunction(render, isFunctional)}` + ';' + renderSeparator; | ||
const hasStaticRenders = staticRenderFns.length; | ||
if (hasStaticRenders) { | ||
code += `var staticRenderFns = [${staticRenderFns.map(render => toFunction(render, isFunctional))}]`; | ||
} | ||
const [compiledRender, compiledStaticRenders] = compileRenderCode(code).split(renderSeparator); | ||
return { | ||
ast, | ||
render: getFunctionBody(compiledRender), | ||
staticRenderFns: hasStaticRenders ? getArrayItems(compiledStaticRenders) : staticRenderFns, | ||
tips, | ||
errors | ||
}; | ||
} | ||
module.exports = function (source, options) { | ||
let ret = {}; | ||
const compileRenderMode = Boolean(options === null || options === void 0 ? void 0 : options.transforms); | ||
if (compileRenderMode) { | ||
ret = compileRenderCode(source); | ||
} else { | ||
ret = compileTemplateCode(source, options); | ||
} | ||
return ret; | ||
}; |
{ | ||
"name": "vue-template-babel-compiler", | ||
"version": "0.7.0", | ||
"version": "1.0.0-0", | ||
"description": "Post compiler for Vue template render functions to support ES features with Babel", | ||
@@ -18,3 +18,3 @@ "main": "lib/index.js", | ||
"test": " npm run build && jest", | ||
"inspectTest": "npm run build && node --inspect-brk ./node_modules/jest/bin/jest.js", | ||
"inspect": "npm run build && node --inspect-brk ./node_modules/jest/bin/jest.js", | ||
"prepublishOnly": "jest && npm run build" | ||
@@ -21,0 +21,0 @@ }, |
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
9574
131