@ordergroove/smi-precompile
Advanced tools
Comparing version 1.1.10 to 1.2.0
@@ -6,2 +6,19 @@ # Change Log | ||
# [1.2.0](https://github.com/ordergroove/plush-toys/compare/@ordergroove/smi-precompile@1.1.10...@ordergroove/smi-precompile@1.2.0) (2021-06-02) | ||
### Bug Fixes | ||
* **smi-precompile:** Make loader config optional ([cd5e038](https://github.com/ordergroove/plush-toys/commit/cd5e038c44802963291493b65c0f941a8631570e)) | ||
* **smi-precompile:** Support translation variables in nested values ([13e1b0a](https://github.com/ordergroove/plush-toys/commit/13e1b0af204396fae68bc0d624e5d5cd26619d05)) | ||
### Features | ||
* **smi-precompile:** Support arbitrary extra script in conbination with |js tag to extend behavious and add polyfills ([ad9584c](https://github.com/ordergroove/plush-toys/commit/ad9584c95dfe5d28f4586065c5e2a038a2600eba)) | ||
## [1.1.10](https://github.com/ordergroove/plush-toys/compare/@ordergroove/smi-precompile@1.1.9...@ordergroove/smi-precompile@1.1.10) (2021-05-20) | ||
@@ -8,0 +25,0 @@ |
@@ -37,2 +37,12 @@ const { parser } = require('lit-nunjucks'); | ||
*/ | ||
function resolveScript(scriptPath) { | ||
if (fs.existsSync(scriptPath)) { | ||
this.addDependency(scriptPath); | ||
return fs.readFileSync(scriptPath, { encoding: 'utf8' }); | ||
} | ||
return ''; | ||
} | ||
/** | ||
* returns an object where keys refer to the template name and values are the template content | ||
*/ | ||
function resolveViews(viewsPath) { | ||
@@ -75,12 +85,10 @@ const sources = fs | ||
const preprocessedSource = hardPreprocess(source); | ||
const { viewsPath, localesPath, settingsPath } = getLoaderConfig(this); | ||
const { viewsPath, localesPath, settingsPath, scriptPath } = getLoaderConfig(this); | ||
const resolvePathRelativeToResource = p => path.resolve(path.dirname(this.resourcePath), p); | ||
const [viewsPathAbs, settingsPathAbs, localesPathAbs] = [viewsPath, settingsPath, localesPath].map( | ||
resolvePathRelativeToResource | ||
); | ||
const partials = resolveViews.call(this, viewsPathAbs); | ||
const settings = resolveSettings.call(this, settingsPathAbs); | ||
const locales = resolveLocales.call(this, localesPathAbs); | ||
const partials = viewsPath ? resolveViews.call(this, resolvePathRelativeToResource(viewsPath)) : undefined; | ||
const settings = settingsPath ? resolveSettings.call(this, resolvePathRelativeToResource(settingsPath)) : undefined; | ||
const locales = localesPath ? resolveLocales.call(this, resolvePathRelativeToResource(localesPath)) : undefined; | ||
const script = scriptPath ? resolveScript.call(this, resolvePathRelativeToResource(scriptPath)) : undefined; | ||
@@ -95,3 +103,3 @@ return ` | ||
*/ | ||
export default ${precompile(preprocessedSource, partials, locales, settings)};`; | ||
export default ${precompile(preprocessedSource, partials, locales, settings, 'en', script)};`; | ||
}; |
{ | ||
"name": "@ordergroove/smi-precompile", | ||
"version": "1.1.10", | ||
"version": "1.2.0", | ||
"description": "Prcompilers for smi-core", | ||
@@ -32,3 +32,3 @@ "author": "Brian Lewis <brian.lewis@ordergroove.com>", | ||
}, | ||
"gitHead": "5f3273ddeee18e3ee19156f2d1e8d95bedf81c3f" | ||
"gitHead": "b4d8291cfe9b99e566e8ea08420f2cb24024d750" | ||
} |
@@ -24,3 +24,5 @@ /* eslint-disable no-shadow */ | ||
return t.objectExpression( | ||
Object.entries(source).map(([key, val]) => t.objectProperty(t.identifier(key), t.stringLiteral(val))) | ||
Object.entries(source).map(([key, val]) => | ||
t.objectProperty(t.identifier(key), parseTranslation(val, key, locale)) | ||
) | ||
); | ||
@@ -72,3 +74,3 @@ } | ||
*/ | ||
function precompile(source, partials = {}, locales = [], settings = {}, defaultLocale = 'en') { | ||
function precompile(source, partials = {}, locales = [], settings = {}, defaultLocale = 'en', script) { | ||
const ast = t.file(t.program([parse(source, { partials: mapKeys(partials, filenameWithoutExt) })])); | ||
@@ -168,3 +170,7 @@ | ||
}); | ||
let extras = []; | ||
if (script) { | ||
const scriptAst = babel.parse(script); | ||
extras = [...extras, ...scriptAst.program.body]; | ||
} | ||
const program = t.file( | ||
@@ -176,3 +182,4 @@ t.program([ | ||
t.blockStatement([...result, t.returnStatement(t.objectExpression(localizedTeamplates))]) | ||
) | ||
), | ||
...extras | ||
]) | ||
@@ -179,0 +186,0 @@ ); |
30345
614