Skip to content

Commit

Permalink
Nested component tested
Browse files Browse the repository at this point in the history
  • Loading branch information
Damir committed Oct 11, 2022
1 parent ec6eae5 commit 8a1d117
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 11 deletions.
22 changes: 18 additions & 4 deletions 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
Expand Up @@ -21,7 +21,7 @@
],
"dependencies": {
"deepmerge": "^4.2.2",
"posthtml": "^0.16.5",
"posthtml-attrs-parser": "^0.1.1",
"posthtml-expressions": "^1.9.0",
"posthtml-parser": "^0.11.0"
},
Expand Down
2 changes: 0 additions & 2 deletions src/find-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ function findPathByNamespace(tag, [namespace, fileNameFromTag], options) {
function findPathByRoot(tag, fileNameFromTag, options) {
let root = options.roots.find(root => fs.existsSync(path.join(options.root, root, fileNameFromTag)));

options.roots.find(root => console.warn(path.join(options.root, root, fileNameFromTag)));

if (!root) {
// Check if module exist in folder `tag-name/index.html`
fileNameFromTag = fileNameFromTag
Expand Down
23 changes: 19 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
const fs = require('fs');
const path = require('path');
const expressions = require('posthtml-expressions');
const scriptDataLocals = require('posthtml-expressions/lib/locals');
const {parser: parseToPostHtml} = require('posthtml-parser');
const parseAttrs = require('posthtml-attrs-parser')
const {match} = require('posthtml/lib/api');
const merge = require('deepmerge');
const findPathFromTagName = require('./find-path');
Expand Down Expand Up @@ -32,12 +34,24 @@ function processNodes(tree, options, messages) {

delete attributes.locals;

options.expressions.locals = merge(options.expressions.locals, attributes);
const plugins = [...options.plugins, expressions(options.expressions)];
attributes = merge(options.expressions.locals, attributes);

const layoutPath = path.resolve(options.root, node.attrs.src);
const layoutHtml = fs.readFileSync(layoutPath, options.encoding);
const layoutTree = processNodes(applyPluginsToTree(parseToPostHtml(layoutHtml), plugins), options, messages);
const parsedHtml = parseToPostHtml(layoutHtml);

// Retrieve default locals from <script defaultLocals> and merge with attributes
const {locals: defaultLocals} = scriptDataLocals(parsedHtml, {localsAttr: options.scriptLocalAttribute, removeScriptLocals: true, locals: attributes});
if (defaultLocals) {
attributes = merge(defaultLocals, attributes);
}

console.log(`defaultLocals %s`, defaultLocals);

options.expressions.locals = attributes;
const plugins = [...options.plugins, expressions(options.expressions)];

const layoutTree = processNodes(applyPluginsToTree(parsedHtml, plugins), options, messages);

node.tag = false;
node.content = mergeSlots(layoutTree, node, options.strict, options.slotTagName);
Expand Down Expand Up @@ -162,7 +176,8 @@ module.exports = (options = {}) => {
expressions: {},
plugins: [],
encoding: 'utf8',
strict: false
strict: false,
scriptLocalAttribute: 'defaultLocals'
},
...options
};
Expand Down
7 changes: 7 additions & 0 deletions test/templates/components/component-mapped-attributes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script defaultLocals>
module.exports = {
title: 'Default title',
body: 'Default body'
}
</script>
<div class="text-dark m-3" $attributes>{{ title }} {{ body }}</div>
9 changes: 9 additions & 0 deletions test/test-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,12 @@ test('Must output html of component with custom tag', async t => {

t.is(html, expected);
});

test('Must output html of component with mapped attributes', async t => {
const actual = `<component src="components/component-mapped-attributes.html" class="bg-light p-2" title="My Title"></component>`;
const expected = `<div class="text-dark m-3" $attributes="">My Title Default body</div>`;

const html = await posthtml([plugin({root: './test/templates/', roots: 'components/'})]).process(actual).then(result => clean(result.html));

t.is(html, expected);
});

0 comments on commit 8a1d117

Please sign in to comment.