diff --git a/lib/index.js b/lib/index.js index 8e444e7..3e6602e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -21,7 +21,6 @@ module.exports = (options = {}) => { let subtree; let source; const posthtmlExpressionsOptions = options.posthtmlExpressionsOptions || {locals: false}; - if (options.delimiters) { posthtmlExpressionsOptions.delimiters = options.delimiters; } @@ -32,7 +31,8 @@ module.exports = (options = {}) => { try { const localsRaw = node.attrs.locals || (node.content ? node.content.join().replace(/\n/g, '') : false); - posthtmlExpressionsOptions.locals = JSON.parse(localsRaw); + const localsJson = JSON.parse(localsRaw); + posthtmlExpressionsOptions.locals = posthtmlExpressionsOptions.locals ? Object.assign(localsJson, posthtmlExpressionsOptions.locals) : localsJson; } catch {} if (posthtmlExpressionsOptions.locals) { diff --git a/package-lock.json b/package-lock.json index 38823c2..4c300e1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "posthtml-include", - "version": "1.7.1", + "version": "1.7.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -3428,9 +3428,9 @@ } }, "hosted-git-info": { - "version": "2.8.8", - "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.8.tgz", - "integrity": "sha512-f/wzC2QaWBs7t9IYqB4T3sR1xviIViXJRJTWBlx2Gf3g0Xi5vI7Yy4koXQ1c9OYDGHN9sBy1DQ2AB8fqZBWhUg==", + "version": "2.8.9", + "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.9.tgz", + "integrity": "sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==", "dev": true }, "html-escaper": { @@ -4208,9 +4208,9 @@ } }, "lodash": { - "version": "4.17.20", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", - "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==", + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "dev": true }, "lodash.flattendeep": { @@ -4688,9 +4688,9 @@ "dev": true }, "normalize-url": { - "version": "4.5.0", - "resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-4.5.0.tgz", - "integrity": "sha512-2s47yzUxdexf1OhyRi4Em83iQk0aPvwTddtFz4hnSSw9dCEsLEGf6SwIO8ss/19S9iBb5sJaOuTvTGDeZI00BQ==", + "version": "4.5.1", + "resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-4.5.1.tgz", + "integrity": "sha512-9UZCFRHQdNrfTpGg8+1INIg93B6zE0aXMVFkw1WFwvO4SlZywU6aLg5Of0Ap/PgcbSw4LNxvMWXMeugwMCX0AA==", "dev": true }, "npm-run-path": { @@ -5156,9 +5156,9 @@ "dev": true }, "path-parse": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", - "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", + "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", "dev": true }, "path-type": { @@ -6626,9 +6626,9 @@ } }, "trim-newlines": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/trim-newlines/-/trim-newlines-3.0.0.tgz", - "integrity": "sha512-C4+gOpvmxaSMKuEf9Qc134F1ZuOHVXKRbtEflf4NTtuuJDEIJ9p5PXsalL8SkeRw+qit1Mo+yuvMPAKwWg/1hA==", + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/trim-newlines/-/trim-newlines-3.0.1.tgz", + "integrity": "sha512-c1PTsA3tYrIsLGkJkzHF+w9F2EyxfXGo4UyJc4pFL++FMjnq0HJS69T3M7d//gKrFKwy429bouPescbjecU+Zw==", "dev": true }, "trim-off-newlines": { diff --git a/package.json b/package.json index 4b003b4..f8ada5e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "posthtml-include", - "version": "1.7.1", + "version": "1.7.2", "description": "Include files in HTML", "license": "MIT", "author": "Ivan Voischev ", diff --git a/test/expected/global-variables.html b/test/expected/global-variables.html new file mode 100644 index 0000000..de6c93d --- /dev/null +++ b/test/expected/global-variables.html @@ -0,0 +1,3 @@ +
Should merge global locals
+
local
+
global
diff --git a/test/fixtures/global-variables.html b/test/fixtures/global-variables.html new file mode 100644 index 0000000..fbd9701 --- /dev/null +++ b/test/fixtures/global-variables.html @@ -0,0 +1,6 @@ +
Should merge global locals
+ + { + "localVariable": "local" + } + diff --git a/test/fixtures/includes/6.html b/test/fixtures/includes/6.html new file mode 100644 index 0000000..35e0cf1 --- /dev/null +++ b/test/fixtures/includes/6.html @@ -0,0 +1,2 @@ +
{{ localVariable }}
+
{{ globalVariable }}
diff --git a/test/test.js b/test/test.js index acf8eea..d9d85b2 100644 --- a/test/test.js +++ b/test/test.js @@ -53,6 +53,13 @@ test('Shoult not cut children elements', t => { return process(t, 'not-clear-rest') }) +test('Should merge global locals', t => { + return process(t, 'global-variables', { + posthtmlExpressionsOptions: { + locals: {globalVariable: 'global'} + }}) +}) + test('addDependency message', t => { const includePath = require('path').resolve('./test/fixtures/blocks/button/button.html')