Skip to content

Commit d95fc32

Browse files
authored
Merge pull request #76 from nokazn/fix/multiple-include
Fix: multiple `<include>` tag with global locals evaluate locals uncorrectry
2 parents 592eee4 + 469bc41 commit d95fc32

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

lib/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = (options = {}) => {
2020
let content;
2121
let subtree;
2222
let source;
23-
const posthtmlExpressionsOptions = options.posthtmlExpressionsOptions || {locals: false};
23+
let posthtmlExpressionsOptions = options.posthtmlExpressionsOptions || {locals: false};
2424
if (options.delimiters) {
2525
posthtmlExpressionsOptions.delimiters = options.delimiters;
2626
}
@@ -32,7 +32,10 @@ module.exports = (options = {}) => {
3232
try {
3333
const localsRaw = node.attrs.locals || (node.content ? node.content.join().replace(/\n/g, '') : false);
3434
const localsJson = JSON.parse(localsRaw);
35-
posthtmlExpressionsOptions.locals = posthtmlExpressionsOptions.locals ? Object.assign(localsJson, posthtmlExpressionsOptions.locals) : localsJson;
35+
posthtmlExpressionsOptions = {
36+
...posthtmlExpressionsOptions,
37+
locals: posthtmlExpressionsOptions.locals ? Object.assign(localsJson, posthtmlExpressionsOptions.locals) : localsJson
38+
};
3639
} catch {}
3740

3841
if (posthtmlExpressionsOptions.locals) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<div>local1</div>
2+
<div>global</div>
3+
4+
<div>local2</div>
5+
<div>global</div>
6+
7+
<div>local3</div>
8+
<div>global</div>
9+
10+
<div>local4</div>
11+
<div>global</div>
12+
13+
<div>local5</div>
14+
<div>global</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<include src="./test/fixtures/includes/6.html">{ "localVariable": "local1" }</include>
2+
<include src="./test/fixtures/includes/6.html">{ "localVariable": "local2" }</include>
3+
<include src="./test/fixtures/includes/6.html">{ "localVariable": "local3" }</include>
4+
<include src="./test/fixtures/includes/6.html">{ "localVariable": "local4" }</include>
5+
<include src="./test/fixtures/includes/6.html">{ "localVariable": "local5" }</include>

test/test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const test = require('ava')
2-
const plugin = require('../lib')
2+
const plugin = require('../lib/index.js')
33
const posthtml = require('posthtml')
44

55
const path = require('path')
@@ -60,6 +60,13 @@ test('Should merge global locals', t => {
6060
}})
6161
})
6262

63+
test('Should merge global variables and evaluate each local variable', t => {
64+
return process(t, 'multiple-include', {
65+
posthtmlExpressionsOptions: {
66+
locals: {globalVariable: 'global'}
67+
}})
68+
})
69+
6370
test('addDependency message', t => {
6471
const includePath = require('path').resolve('./test/fixtures/blocks/button/button.html')
6572

0 commit comments

Comments
 (0)