reshape-component
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -16,4 +16,6 @@ /* eslint-disable complexity */ | ||
* @param {ReshapeAstNode} componentNode - '<component>' node | ||
* @param {boolean|string} preserveType - If 'true', the 'type' attributed will preserved; if a string value | ||
* the 'type' attribute will be copied to an attribute named by the value | ||
*/ | ||
function copyAttributes (templateAst, componentNode) { | ||
function copyAttributes (templateAst, componentNode, preserveType) { | ||
for (let attributeName in componentNode.attrs) { | ||
@@ -30,2 +32,12 @@ if (!(attributeName in skippedAttributes)) { | ||
} | ||
if (preserveType) { | ||
const typeAttribute = preserveType === true ? 'type' : preserveType; | ||
const typeAttributeValue = componentNode.attrs.type; | ||
if (typeAttributeValue) { | ||
templateAst[0].attrs = templateAst[0].attrs || {}; | ||
templateAst[0].attrs[typeAttribute] = typeAttributeValue; | ||
} | ||
} | ||
} | ||
@@ -195,3 +207,3 @@ } | ||
const templateAst = parser(src, { filename: componentPath }, { filename: componentPath }); | ||
copyAttributes(templateAst, node); | ||
copyAttributes(templateAst, node, options.preserveType); | ||
const tokenPromise = replaceTokens(templateAst, node); | ||
@@ -198,0 +210,0 @@ |
{ | ||
"name": "reshape-component", | ||
"description": "Include components in reshape templates", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"author": "msssk", | ||
@@ -6,0 +6,0 @@ "bugs": "https://github.com/mkdecisiondev/reshape-component/issues", |
@@ -66,10 +66,35 @@ # Reshape Component | ||
The path is configurable in the options passed to the plugin via the `componentPath` property. The default value is: | ||
The path is configurable in the options passed to the plugin via the `componentPath` property. | ||
## Options | ||
Name | Default | Description | ||
----------------- | ------- | ----------- | ||
`componentPath` | 'components/$type/$type.html' | Specifies the path to use to include components specified by the `type` attribute. Any occurrences of `$type` in the path will be replaced with the value of the `type` attribute. | ||
`preserveType` | false | If `true` the `type` attribute will be copied from the `component` tag to the first top-level node in the template. If a string value is provided it will be used as the name of the target attribute. This can be useful for JavaScript that wants to query the DOM for component nodes. | ||
### `preserveType` example | ||
```javascript | ||
// configuring reshape-component: | ||
reshapeComponent({ preserveType: 'data-component-type' }) | ||
``` | ||
'components/$type/$type.html' | ||
```html | ||
<!-- defining a component template: --> | ||
<div> | ||
<p>${content}</p> | ||
</div> | ||
<!-- using a component --> | ||
<component type="Card"> | ||
<content>Card One</content> | ||
</component> | ||
<!-- result --> | ||
<div data-component-type="Card"> | ||
<p>Card One</p> | ||
</div> | ||
``` | ||
Any occurrences of `$type` in the path will be replaced with the value of the `type` attribute. | ||
## Token substitution | ||
@@ -76,0 +101,0 @@ |
19639
394
152