@bigcommerce/stencil-paper
Advanced tools
Comparing version 3.0.0-rc.42 to 3.0.0-rc.43
# Changelog | ||
## 3.0.0-rc.43 (2021-05-18) | ||
- Created an endpoint for precaching lang helper function [#235](https://github.com/bigcommerce/paper/pull/235) | ||
## 3.0.0-rc.42 (2021-05-11) | ||
@@ -4,0 +7,0 @@ - Improved filtering language object by key for langJSON helper, that was struggling with perfomance on GraalVM [#236](https://github.com/bigcommerce/paper/pull/236) |
@@ -292,4 +292,13 @@ 'use strict'; | ||
} | ||
/** | ||
* Precompiles translations to string representations of translation function source code | ||
* | ||
* @param {Object} translations transformed translations object | ||
*/ | ||
precompileTranslations(translations) { | ||
return Translator.precompileTranslations(translations); | ||
} | ||
} | ||
module.exports = Paper; |
@@ -27,3 +27,3 @@ 'use strict'; | ||
} | ||
} else { | ||
} else if (key !== 'compiledTranslations') { | ||
result[key] = language[key]; | ||
@@ -30,0 +30,0 @@ } |
@@ -29,4 +29,5 @@ 'use strict'; | ||
this.logger = logger; | ||
this.omitTransforming = omitTransforming; | ||
const languages = omitTransforming ? allTranslations : Transformer.transform(allTranslations, DEFAULT_LOCALE, this.logger); | ||
const languages = this.omitTransforming ? allTranslations : Transformer.transform(allTranslations, DEFAULT_LOCALE, this.logger); | ||
/** | ||
@@ -38,7 +39,3 @@ * @private | ||
/** | ||
* @private | ||
* @type {Object.<string, string>} | ||
*/ | ||
this._language = languages[this._locale] || {}; | ||
this.setLanguage(languages); | ||
@@ -72,2 +69,49 @@ /** | ||
/** | ||
* Precompile translation functions | ||
* @param {Object} translations | ||
* @returns {Object} | ||
*/ | ||
Translator.precompileTranslations = function (translations) { | ||
const compiled = {}; | ||
Object.keys(translations).forEach(language => { | ||
compiled[language] = {} | ||
Object.keys(translations[language]).forEach(categoryKey => { | ||
compiled[language][categoryKey] = translations[language][categoryKey]; | ||
if (categoryKey === 'translations') { | ||
compiled[language].compiledTranslations = {}; | ||
Object.keys(translations[language][categoryKey]).forEach(key => { | ||
compiled[language].compiledTranslations[key] = Translator.compileFormatterFunction(translations[language], key); | ||
}) | ||
} | ||
}); | ||
}); | ||
return compiled; | ||
} | ||
/** | ||
* Precompile translation functions | ||
* @param {Object} language | ||
* @param {String} key | ||
* @returns {Object} | ||
*/ | ||
Translator.compileFormatterFunction = function (language, key) { | ||
const locale = language.locales[key]; | ||
const formatter = new MessageFormat(locale) | ||
try { | ||
return formatter.compile(language.translations[key]).toString(); | ||
} catch (err) { | ||
if (err.name === 'SyntaxError') { | ||
this.logger.error(`Syntax Error during Formatter function precompilation: ${err.message} for key "${key}"`, err.expected); | ||
return () => ''; | ||
} | ||
throw err; | ||
} | ||
} | ||
/** | ||
* Get translated string | ||
@@ -84,3 +128,3 @@ * @param {string} key | ||
if (typeof this._formatFunctions[key] === 'undefined') { | ||
if (!this.omitTransforming && typeof this._formatFunctions[key] === 'undefined') { | ||
this._formatFunctions[key] = this._compileTemplate(key); | ||
@@ -90,2 +134,7 @@ } | ||
try { | ||
if (this.omitTransforming) { | ||
const fn = new Function(`return ${language.compiledTranslations[key]}`)() | ||
return fn(parameters); | ||
} | ||
return this._formatFunctions[key](parameters); | ||
@@ -121,2 +170,10 @@ } catch (err) { | ||
/** | ||
* Set language object | ||
* @param {Object} [languages] object | ||
*/ | ||
Translator.prototype.setLanguage = function (languages) { | ||
this._language = languages[this._locale] || {}; | ||
}; | ||
/** | ||
* Get formatter | ||
@@ -123,0 +180,0 @@ * @private |
{ | ||
"name": "@bigcommerce/stencil-paper", | ||
"version": "3.0.0-rc.42", | ||
"version": "3.0.0-rc.43", | ||
"description": "A Stencil plugin to load template files and render pages using backend renderer plugins.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -312,2 +312,4 @@ 'use strict'; | ||
const translator = Translator.create(locale, flattenedLanguages, console, true); | ||
const precompiledTranslations = Translator.precompileTranslations(flattenedLanguages); | ||
translator.setLanguage(precompiledTranslations) | ||
expect(translator.translate('hello', {name: 'User'})).to.equal('Hello User'); | ||
@@ -314,0 +316,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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
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
58520
1089
1