diff --git a/src/Parser.js b/src/Parser.js index c3f73268826..b061f9dd330 100644 --- a/src/Parser.js +++ b/src/Parser.js @@ -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]); diff --git a/src/assets/WebManifestAsset.js b/src/assets/WebManifestAsset.js new file mode 100644 index 00000000000..f737cd0ef74 --- /dev/null +++ b/src/assets/WebManifestAsset.js @@ -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; diff --git a/test/html.js b/test/html.js index 91096b0858c..af424421cf5 100644 --- a/test/html.js +++ b/test/html.js @@ -301,7 +301,7 @@ describe('html', function() { assert(html.includes('')); }); - it('Should detect virtual paths', async function() { + it('should detect virtual paths', async function() { let b = await bundle( __dirname + '/integration/html-virtualpath/index.html' ); @@ -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, { @@ -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: [] + } + ] + } + ] + }); + }); }); diff --git a/test/integration/webmanifest/index.html b/test/integration/webmanifest/index.html new file mode 100644 index 00000000000..9c6cd6298ad --- /dev/null +++ b/test/integration/webmanifest/index.html @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/test/integration/webmanifest/manifest.webmanifest b/test/integration/webmanifest/manifest.webmanifest new file mode 100644 index 00000000000..8e5fba0db35 --- /dev/null +++ b/test/integration/webmanifest/manifest.webmanifest @@ -0,0 +1,11 @@ +{ + "name": "example", + "icons": [ + { + "src": "some.txt", + "sizes": "192x192", + "type": "image/png" + } + ], + "display": "standalone" +} \ No newline at end of file diff --git a/test/integration/webmanifest/some.txt b/test/integration/webmanifest/some.txt new file mode 100644 index 00000000000..db0248434df --- /dev/null +++ b/test/integration/webmanifest/some.txt @@ -0,0 +1 @@ +working \ No newline at end of file