Skip to content

Commit

Permalink
feat(rollup-plugin-html): support video source tags as assets
Browse files Browse the repository at this point in the history
  • Loading branch information
daKmoR committed Aug 17, 2022
1 parent 5857c11 commit ab5295e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
11 changes: 11 additions & 0 deletions .changeset/great-hounds-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@web/rollup-plugin-html': minor
---

Detect `<source src="*">` tags as assets which means videos get copied and hashed.

```html
<video controls>
<source src="./my-video.mp4" type="video/mp4">
</video>
```
8 changes: 6 additions & 2 deletions packages/rollup-plugin-html/src/assets/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ function isAsset(node: Element) {
path = getAttribute(node, 'src') ?? '';
break;
case 'source':
path = extractFirstUrlOfSrcSet(node) ?? '';
if (getAttribute(node, 'src')) {
path = getAttribute(node, 'src') ?? '';
} else {
path = extractFirstUrlOfSrcSet(node) ?? '';
}
break;
case 'link':
if (linkRels.includes(getAttribute(node, 'rel') ?? '')) {
Expand Down Expand Up @@ -104,7 +108,7 @@ export function getSourceAttribute(node: Element) {
return 'src';
}
case 'source': {
return 'srcset';
return getAttribute(node, 'src') ? 'src' : 'srcset';
}
case 'link': {
return 'href';
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,50 @@ describe('injectedUpdatedAssetPaths()', () => {
expect(serialize(document).replace(/ {4}/g, '\n')).to.eql(expected);
});

it('handles video tag using source tags with src', () => {
const document = parse(
[
'<html>',
' <body>',
' <video controls>',
' <source src="./videos/typer-hydration.mp4" type="video/mp4">',
' </video>',
' </body>',
'</html>',
].join(''),
);

const input: InputData = {
html: '',
name: 'index.html',
moduleImports: [],
inlineModules: [],
assets: [],
filePath: '/root/index.html',
};
const hashed = new Map<string, string>();
hashed.set(
path.join(path.sep, 'root', 'videos', 'typer-hydration.mp4'),
'typer-hydration-xxx.mp4',
);

injectedUpdatedAssetPaths({
document,
input,
outputDir: '/root/dist/',
rootDir: '/root/',
emittedAssets: { static: new Map(), hashed },
});

const expected = [
'<html><head></head><body>',
'<video controls="">',
' <source src="typer-hydration-xxx.mp4" type="video/mp4">',
'</video> </body></html>',
].join('\n');
expect(serialize(document).replace(/ {4}/g, '\n')).to.eql(expected);
});

it('handles virtual files', () => {
const document = parse(
[
Expand Down

0 comments on commit ab5295e

Please sign in to comment.