Skip to content

Commit 8ce8dde

Browse files
committed
Allow to merge locals with posthtml-expressions config locals
1 parent 69853f8 commit 8ce8dde

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ module.exports = (options = {}) => {
2121
let subtree;
2222
let source;
2323
const posthtmlExpressionsOptions = options.posthtmlExpressionsOptions || {locals: false};
24-
2524
if (options.delimiters) {
2625
posthtmlExpressionsOptions.delimiters = options.delimiters;
2726
}
@@ -32,7 +31,8 @@ module.exports = (options = {}) => {
3231

3332
try {
3433
const localsRaw = node.attrs.locals || (node.content ? node.content.join().replace(/\n/g, '') : false);
35-
posthtmlExpressionsOptions.locals = JSON.parse(localsRaw);
34+
const localsJson = JSON.parse(localsRaw);
35+
posthtmlExpressionsOptions.locals = posthtmlExpressionsOptions.locals ? Object.assign(localsJson, posthtmlExpressionsOptions.locals) : localsJson;
3636
} catch {}
3737

3838
if (posthtmlExpressionsOptions.locals) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>Should merge global locals</div>
2+
<div>local</div>
3+
<div>global</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div>Should merge global locals</div>
2+
<include src="./test/fixtures/includes/6.html">
3+
{
4+
"localVariable": "local"
5+
}
6+
</include>

test/fixtures/includes/6.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div>{{ localVariable }}</div>
2+
<div>{{ globalVariable }}</div>

test/test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ test('Shoult not cut children elements', t => {
5353
return process(t, 'not-clear-rest')
5454
})
5555

56+
test('Should merge global locals', t => {
57+
return process(t, 'global-variables', {
58+
posthtmlExpressionsOptions: {
59+
locals: {globalVariable: 'global'}
60+
}})
61+
})
62+
5663
test('addDependency message', t => {
5764
const includePath = require('path').resolve('./test/fixtures/blocks/button/button.html')
5865

0 commit comments

Comments
 (0)