pure-engine
Advanced tools
Comparing version 0.19.0 to 0.20.0
{ | ||
"name": "pure-engine", | ||
"version": "0.19.0", | ||
"version": "0.20.0", | ||
"description": "Compile HTML templates into JS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -17,2 +17,3 @@ 'use strict' | ||
const { addPlaceholders, removePlaceholders } = require('../utilities/keywords') | ||
const { GLOBAL_VARIABLE } = require('../utilities/enum') | ||
@@ -95,2 +96,10 @@ function canInlineTree ({ body }) { | ||
function isGlobalVariable (tree) { | ||
const node = tree.body[0] | ||
return node.type === 'ExpressionStatement' && | ||
node.expression.type === 'MemberExpression' && | ||
node.expression.object.type === 'Identifier' && | ||
node.expression.object.name === GLOBAL_VARIABLE | ||
} | ||
function optimizeCurlyTag (value, variables, newVariables) { | ||
@@ -100,2 +109,3 @@ value = addPlaceholders(value) | ||
const tree = new AbstractSyntaxTree(value) | ||
if (isGlobalVariable(tree)) { return curlyTag(value) } | ||
tree.replace({ enter: removePlaceholders }) | ||
@@ -102,0 +112,0 @@ tree.replace({ enter: (node, parent) => inlineVariables(node, parent, variables, newVariables) }) |
@@ -19,2 +19,3 @@ 'use strict' | ||
const Optimizer = require('./Optimizer') | ||
const Scope = require('./Scope') | ||
const { getLiteral } = require('./utilities/ast') | ||
@@ -113,2 +114,4 @@ | ||
tree.append(getTemplateReturnStatement()) | ||
const scope = new Scope() | ||
scope.flatten(tree) | ||
const optimizer = new Optimizer() | ||
@@ -115,0 +118,0 @@ optimizer.optimize(tree) |
@@ -9,5 +9,7 @@ 'use strict' | ||
const { findAsset } = require('../utilities/files') | ||
const { isCurlyTag } = require('../utilities/string') | ||
const { convertAttribute } = require('../utilities/convert') | ||
let asyncCounter = 0 | ||
module.exports = async function ({ tree, keys, attrs, fragment, assets, variables, promises, warnings, options }) { | ||
module.exports = async function ({ tree, keys, attrs, fragment, assets, variables, promises, warnings, filters, translations, languages, append, options }) { | ||
if (keys.includes('inline') || options.inline.includes('scripts')) { | ||
@@ -135,18 +137,21 @@ if (keys.includes('src')) { | ||
} else { | ||
let content = '<script' | ||
append(getLiteral('<script')) | ||
fragment.attributes.forEach(attribute => { | ||
if (attribute.value) { | ||
content += ` ${attribute.key}="${attribute.value}"` | ||
if (isCurlyTag(attribute.value)) { | ||
append(getLiteral(` ${attribute.key}="`)) | ||
append(convertAttribute(attribute.key, attribute.value, variables, filters, translations, languages)) | ||
append(getLiteral('"')) | ||
} else if (attribute.value) { | ||
append(getLiteral(` ${attribute.key}="${attribute.value}"`)) | ||
} else { | ||
content += ` ${attribute.key}` | ||
append(getLiteral(` ${attribute.key}`)) | ||
} | ||
}) | ||
content += '>' | ||
append(getLiteral('>')) | ||
fragment.children.forEach(node => { | ||
node.used = true | ||
content += node.content | ||
append(getLiteral(node.content)) | ||
}) | ||
content += '</script>' | ||
tree.append(getTemplateAssignmentExpression(options.variables.template, getLiteral(content))) | ||
append(getLiteral('</script>')) | ||
} | ||
} |
@@ -234,2 +234,5 @@ 'use strict' | ||
} | ||
function append (node) { | ||
tree.append(getTemplateAssignmentExpression(options.variables.template, node)) | ||
} | ||
try { | ||
@@ -308,3 +311,3 @@ if (fragment.used) return | ||
} else if (tag === 'script') { | ||
tags.script({ tree, fragment, keys, attrs, assets, variables, promises, warnings, options }) | ||
tags.script({ tree, fragment, keys, attrs, assets, variables, promises, warnings, filters, translations, languages, append, options }) | ||
} else if (tag === 'template') { | ||
@@ -340,3 +343,3 @@ tags.template({ tree, fragment, options }) | ||
} | ||
tree.append(getTemplateAssignmentExpression(options.variables.template, node)) | ||
append(node) | ||
}) | ||
@@ -348,7 +351,7 @@ collectChildren(fragment, tree) | ||
const property = attr.key === 'tag' ? attr.value.substring(1, attr.value.length - 1) : attr.value | ||
tree.append(getTemplateAssignmentExpression(options.variables.template, getLiteral('</'))) | ||
tree.append(getTemplateAssignmentExpression(options.variables.template, getObjectMemberExpression(property))) | ||
tree.append(getTemplateAssignmentExpression(options.variables.template, getLiteral('>'))) | ||
append(getLiteral('</')) | ||
append(getObjectMemberExpression(property)) | ||
append(getLiteral('>')) | ||
} else { | ||
tree.append(getTemplateAssignmentExpression(options.variables.template, getLiteral(`</${tag}>`))) | ||
append(getLiteral(`</${tag}>`)) | ||
} | ||
@@ -358,3 +361,3 @@ } | ||
const nodes = convertText(fragment.content, variables, filters, translations, languages) | ||
return nodes.forEach(node => tree.append(getTemplateAssignmentExpression(options.variables.template, node))) | ||
return nodes.forEach(node => append(node)) | ||
} else if (tag === 'if') { | ||
@@ -361,0 +364,0 @@ tags.if({ fragment, tree, attrs, variables, filters, translations, languages, warnings, depth, collectChildren }) |
@@ -7,2 +7,3 @@ 'use strict' | ||
ESCAPE_VARIABLE: '__e', | ||
GLOBAL_VARIABLE: 'globals', | ||
GLOBAL_VARIABLES: ['JSON', 'Math', 'Number', 'console', 'Date'], | ||
@@ -9,0 +10,0 @@ BOOLEAN_ATTRIBUTES: [ |
@@ -8,2 +8,3 @@ 'use strict' | ||
const { normalize } = require('./array') | ||
const { GLOBAL_VARIABLE } = require('./enum') | ||
@@ -19,3 +20,7 @@ const CONDITION_TAGS = ['if', 'elseif', 'unless', 'elseunless'] | ||
if (attr.type === 'Identifier' && !isCurlyTag(attr.key)) { | ||
// TODO this does not handle computed values | ||
// we should create a new abstract-syntax-tree and get the key that way | ||
// instead of string manipulation | ||
const key = attr.key.includes('.') ? attr.key.split('.')[0] : attr.key | ||
if (key === GLOBAL_VARIABLE) { return attr } | ||
const variable = localVariables && localVariables.find(variable => variable.key === key) | ||
@@ -22,0 +27,0 @@ if (variable && variable.local) { |
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
223098
92
5539