babel-plugin-htmlbars-inline-precompile
Advanced tools
Comparing version 4.0.1 to 4.1.0
@@ -42,2 +42,20 @@ 'use strict'; | ||
it('supports compilation that returns a non-JSON.parseable object', function () { | ||
precompile = (template) => { | ||
return `function() { return "${template}"; }`; | ||
}; | ||
let transpiled = transform( | ||
"import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`hello`;" | ||
); | ||
expect(transpiled).toMatchInlineSnapshot(` | ||
"var compiled = Ember.HTMLBars.template( | ||
/* | ||
hello | ||
*/ | ||
function() { return \\"hello\\"; });" | ||
`); | ||
}); | ||
it('passes options when used as a call expression', function () { | ||
@@ -44,0 +62,0 @@ let source = 'hello'; |
@@ -0,1 +1,10 @@ | ||
## v4.1.0 (2020-05-07) | ||
#### :rocket: Enhancement / :bug: Fix | ||
* [#216](https://github.com/ember-cli/babel-plugin-htmlbars-inline-precompile/pull/216) Avoid assuming ember-template-compiler result is JSON ([@rwjblue](https://github.com/rwjblue)) | ||
#### Committers: 1 | ||
- Robert Jackson ([@rwjblue](https://github.com/rwjblue)) | ||
## v4.0.1 (2020-05-06) | ||
@@ -2,0 +11,0 @@ |
46
index.js
@@ -9,42 +9,4 @@ 'use strict'; | ||
); | ||
const parsePrecompiledTemplate = babel.template('PRECOMPILED'); | ||
function buildExpression(value) { | ||
switch (typeof value) { | ||
case 'string': | ||
return t.stringLiteral(value); | ||
case 'number': | ||
return t.numberLiteral(value); | ||
case 'boolean': | ||
return t.booleanLiteral(value); | ||
case 'object': { | ||
if (Array.isArray(value)) { | ||
return buildArrayExpression(value); | ||
} else { | ||
return buildObjectExpression(value); | ||
} | ||
} | ||
default: | ||
throw new Error( | ||
`hbs compilation error; unexpected type from precompiler: ${typeof value} for ${JSON.stringify( | ||
value | ||
)}` | ||
); | ||
} | ||
} | ||
function buildObjectExpression(object) { | ||
let properties = []; | ||
for (let key in object) { | ||
let value = object[key]; | ||
properties.push(t.objectProperty(t.identifier(key), buildExpression(value))); | ||
} | ||
return t.objectExpression(properties); | ||
} | ||
function buildArrayExpression(array) { | ||
return t.arrayExpression(array.map((i) => buildExpression(i))); | ||
} | ||
function parseExpression(buildError, node) { | ||
@@ -108,6 +70,6 @@ switch (node.type) { | ||
let precompiled = JSON.parse(precompileResult); | ||
let templateExpression = parsePrecompiledTemplate({ | ||
PRECOMPILED: precompileResult, | ||
}).expression; | ||
let templateExpression = buildExpression(precompiled); | ||
t.addComment( | ||
@@ -114,0 +76,0 @@ templateExpression, |
{ | ||
"name": "babel-plugin-htmlbars-inline-precompile", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Babel plugin to replace tagged template strings with precompiled HTMLBars templates", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/ember-cli/babel-plugin-htmlbars-inline-precompile", |
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
40816
528