Skip to content

Commit

Permalink
enhancement/issue 1255 frontmatter imports attributes (#1256)
Browse files Browse the repository at this point in the history
* supporting attributes from frontmatter imports

* docs

* clean up console logs

* quote formatting test case
  • Loading branch information
thescientist13 authored Jul 31, 2024
1 parent 205352e commit 978e7cc
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 10 deletions.
22 changes: 18 additions & 4 deletions packages/cli/src/lib/layout-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,29 @@ async function getAppLayout(pageLayoutContents, compilation, customImports = [],
const mergedStyles = [
...appRoot.querySelectorAll('head style'),
...[...(pageRoot && pageRoot.querySelectorAll('head style')) || []],
...customImports.filter(resource => resource.split('.').pop() === 'css')
.map(resource => `<link rel="stylesheet" href="${resource}"></link>`)
...customImports.filter(resource => resource.split(' ')[0].split('.').pop() === 'css')
.map((resource) => {
const [href, ...attributes] = resource.split(' ');
const attrs = attributes?.length > 0
? attributes.join(' ')
: '';

return `<link rel="stylesheet" href="${href}" ${attrs}></link>`;
})
].join('\n');

const mergedScripts = [
...appRoot.querySelectorAll('head script'),
...[...(pageRoot && pageRoot.querySelectorAll('head script')) || []],
...customImports.filter(resource => resource.split('.').pop() === 'js')
.map(resource => `<script src="${resource}" type="module"></script>`)
...customImports.filter(resource => resource.split(' ')[0].split('.').pop() === 'js')
.map((resource) => {
const [src, ...attributes] = resource.split(' ');
const attrs = attributes?.length > 0
? attributes.join(' ')
: '';

return `<script src="${src}" ${attrs}></script>`;
})
].join('\n');

const finalBody = pageLayoutContents
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/lifecycles/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async function optimizeStaticPages(compilation, plugins) {
}

// clean up optimization markers
const body = (await response.text()).replace(/data-gwd-opt=".*[a-z]"/g, '');
const body = (await response.text()).replace(/data-gwd-opt=".*?[a-z]"/g, '');

await fs.writeFile(new URL(`.${outputPath}`, outputDir), body);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ describe('Build Greenwood With: ', function() {
it('should output a heading tag from the custom element', function() {
expect(html).to.contain('<h3>My Counter</h3>');
});

it('should have expected attributes on the `<link>` tag from frontmatter imports', async function() {
const link = Array.from(dom.window.document
.querySelectorAll('head link'))
.find(link => link.getAttribute('href')?.startsWith('/components/counter/'));

expect(link.getAttribute('data-gwd-opt')).to.equal(null);
expect(link.getAttribute('foo')).to.equal('bar');
expect(link.getAttribute('baz')).to.equal('bar');
});
});

describe('Custom Multihyphen component', () => {
Expand All @@ -127,6 +137,15 @@ describe('Build Greenwood With: ', function() {
it('should have the expected prerendered content', function() {
expect(html).to.contain('I have multiple hyphens in my tag name!');
});

it('should have expected attributes on the `<script>` tag from frontmatter imports', async function() {
const script = Array.from(dom.window.document
.querySelectorAll('head script'))
.find(script => script.getAttribute('src')?.startsWith('/multi-hyphen'));

expect(script.getAttribute('foo')).to.equal('bar');
expect(script.getAttribute('type')).to.equal('module');
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: Demo Page
imports:
- /components/counter/counter.js
- /components/counter/counter.css
- /components/multi-hyphen/multi-hyphen.js
- /components/counter/counter.css data-gwd-opt='none' foo='bar' baz='bar'
- /components/multi-hyphen/multi-hyphen.js type="module" foo="bar"
---

## Demo Page Example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe('Serve Greenwood With: ', function() {
.filter(tag => !tag.getAttribute('data-gwd'))
.filter(tag => !tag.getAttribute('type'));

expect(scripts.length).to.equal(2);
expect(scripts.length).to.equal(3);
expect(scripts[1].textContent).to.contain('console.log');
});

Expand Down
6 changes: 4 additions & 2 deletions www/pages/docs/front-matter.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@ label: 'My Blog Post from 3/5/2020'
### Imports
If you want to include files on a _per **page** basis_, you can use the predefined `imports` feature from Greenwood. This is great for one off use cases where you don't want to ship a third party lib in all your layouts, but just for this one particular page. This is effectively a naive form of code splitting. 🤓

You can also add attributes by space delimiting them after the path.

#### Example
```md
---
imports:
- /components/my-component/component.js
- /components/my-component/component.js type="module" foo="bar"
- /components/my-component/component.css
---
```

You will then see the following emitted for file
```html
<script type="module" src="/components/my-component/component.js"></script>
<script type="module" src="/components/my-component/component.js" type="module" foo="bar"></script>
<link rel="stylesheet" href="/components/my-component/component.css"/>
```

Expand Down

0 comments on commit 978e7cc

Please sign in to comment.