Skip to content

Commit

Permalink
[feature] Add WebManifestAsset to handle W3C webmanifest (parcel-bund…
Browse files Browse the repository at this point in the history
  • Loading branch information
niicojs authored and devongovett committed Jan 30, 2018
1 parent 29ac70b commit 1d49e47
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Parser {
this.registerExtension('htm', './assets/HTMLAsset');
this.registerExtension('rs', './assets/RustAsset');

this.registerExtension('webmanifest', './assets/WebManifestAsset');

let extensions = options.extensions || {};
for (let ext in extensions) {
this.registerExtension(ext, extensions[ext]);
Expand Down
40 changes: 40 additions & 0 deletions src/assets/WebManifestAsset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const Asset = require('../Asset');

class WebManifestAsset extends Asset {
constructor(name, pkg, options) {
super(name, pkg, options);
this.type = 'webmanifest';
}

parse(content) {
return JSON.parse(content);
}

collectDependencies() {
if (Array.isArray(this.ast.icons)) {
for (let icon of this.ast.icons) {
icon.src = this.addURLDependency(icon.src);
}
}

if (Array.isArray(this.ast.screenshots)) {
for (let shot of this.ast.screenshots) {
shot.src = this.addURLDependency(shot.src);
}
}

if (this.ast.serviceworker && this.ast.serviceworker.src) {
this.ast.serviceworker.src = this.addURLDependency(
this.ast.serviceworker.src
);
}
}

generate() {
return {
webmanifest: JSON.stringify(this.ast)
};
}
}

module.exports = WebManifestAsset;
26 changes: 24 additions & 2 deletions test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ describe('html', function() {
assert(html.includes('<a href="#hash_link">'));
});

it('Should detect virtual paths', async function() {
it('should detect virtual paths', async function() {
let b = await bundle(
__dirname + '/integration/html-virtualpath/index.html'
);
Expand Down Expand Up @@ -439,7 +439,7 @@ describe('html', function() {
});
});

it('Should detect srcset attribute', async function() {
it('should detect srcset attribute', async function() {
let b = await bundle(__dirname + '/integration/html-srcset/index.html');

assertBundleTree(b, {
Expand All @@ -464,4 +464,26 @@ describe('html', function() {
]
});
});

it('should support webmanifest', async function() {
let b = await bundle(__dirname + '/integration/webmanifest/index.html');

assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'webmanifest',
assets: ['manifest.webmanifest'],
childBundles: [
{
type: 'txt',
assets: ['some.txt'],
childBundles: []
}
]
}
]
});
});
});
5 changes: 5 additions & 0 deletions test/integration/webmanifest/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!doctype html>
<html>
<head><link rel="manifest" href="manifest.webmanifest"></head>
<body></body>
</html>
11 changes: 11 additions & 0 deletions test/integration/webmanifest/manifest.webmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "example",
"icons": [
{
"src": "some.txt",
"sizes": "192x192",
"type": "image/png"
}
],
"display": "standalone"
}
1 change: 1 addition & 0 deletions test/integration/webmanifest/some.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
working

0 comments on commit 1d49e47

Please sign in to comment.