Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# [1.9.0](https://github.com/posthtml/posthtml-expressions/compare/v1.8.1...v1.9.0) (2021-08-23)


### Features

* remove script locals, close [#120](https://github.com/posthtml/posthtml-expressions/issues/120) ([023fcb9](https://github.com/posthtml/posthtml-expressions/commit/023fcb9845939524265589d5b59741e41ea73aa4))


### Performance Improvements

* default locals attr ([73dc216](https://github.com/posthtml/posthtml-expressions/commit/73dc216c2adae6de9582c1c65a83d460df836f0a))



## [1.8.1](https://github.com/posthtml/posthtml-expressions/compare/v1.8.0...v1.8.1) (2021-08-10)


Expand Down
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ module.exports = function postHTMLExpressions (options) {
loopTags: ['each'],
scopeTags: ['scope'],
ignoredTag: 'raw',
strictMode: true
strictMode: true,
localsAttr: 'locals',
removeScriptLocals: false
}, options)

// set tags
Expand Down
6 changes: 5 additions & 1 deletion lib/locals.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const ctx = vm.createContext({ module })
*/
function scriptDataLocals (tree, options) {
const locals = {}
const localsAttr = options.localsAttr || 'locals'
const localsAttr = options.localsAttr

match.call(tree, matchHelper(`script[${localsAttr}]`), node => {
if (node.content) {
Expand All @@ -34,6 +34,10 @@ function scriptDataLocals (tree, options) {
} catch {};
}

if (options.removeScriptLocals) {
return ''
}

return node
})

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "posthtml-expressions",
"version": "1.8.1",
"version": "1.9.0",
"description": "Expressions Plugin for PostHTML",
"engines": {
"node": ">=10"
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ You have full control over the delimiters used for injecting locals, as well as
| **unescapeDelimiters** | `['{{{', '}}}']` | Array containing beginning and ending delimiters for unescaped locals |
| **locals** | `{}` | Object containing any local variables you want to be available inside your expressions |
| **localsAttr** | `locals` | Attribute name for the tag `script` which contains ***[locals](#locals)***|
| **removeScriptLocals** | `false` | Will remove tag `script` which contains ***[locals](#locals)***|
| **conditionalTags** | `['if', 'elseif', 'else']` | Array containing names for tags used for `if/else if/else` statements |
| **switchTags** | `['switch', 'case', 'default']` | Array containing names for tags used for `switch/case/default` statements |
| **loopTags** | `['each']` | Array containing names for `for` loops |
Expand Down
1 change: 1 addition & 0 deletions test/expect/script-locals-remove.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>My name: Scrum</div>
7 changes: 7 additions & 0 deletions test/fixtures/script-locals-remove.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script locals>
module.exports = {
name: 'Scrum'
}
</script>

<div>My name: {{name}}</div>
4 changes: 4 additions & 0 deletions test/test-locals.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ function clean (html) {
test('Basic', (t) => {
return process(t, 'script-locals')
})

test('Remove script locals', (t) => {
return process(t, 'script-locals-remove', { removeScriptLocals: true })
})