Skip to content

Commit 4f96e6d

Browse files
committed
add support for manifest merging
1 parent 099803b commit 4f96e6d

File tree

4 files changed

+59
-33
lines changed

4 files changed

+59
-33
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ Install the adapter and `npm run build`. Go to your browser's extension page and
2828

2929
If you get an error about `_app` being a disallowed folder, delete `_app` from within the build dir. It appears there sometimes and I'm not sure why - I'll fix as soon as possible!
3030

31+
## Configuration
32+
33+
To specify your own manifest information (it will be merged with the generated one), simply have a manifest file local within your app directory.
34+
3135
## Roadmap
3236

3337
I am looking for help to build and maintain this module. Roadmap is:
3438

3539
* Specifying the type of extension via config
3640
* Allowing icons and such to be driven by configuration
37-
* Bring-your-own manifest where you can merge a provided manifest with the generated one.
3841

3942
## License
4043

adapter-browser-extension.mjs

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,74 @@
11
import { join } from 'path'
2-
import { writeFileSync, readFileSync } from 'fs'
2+
import { writeFileSync, readFileSync, existsSync } from 'fs'
33
import sjcl from 'sjcl'
44
import cheerio from 'cheerio'
5+
import { applyToDefaults } from '@hapi/hoek'
56

6-
/** @type {import('.')} */
7-
export default function ({ pages = 'build', assets = pages, fallback } = {}) {
8-
function generate_manifest (html) {
9-
return JSON.stringify({
10-
browser_action: {
11-
default_title: 'SvelteKit',
12-
default_popup: 'index.html'
13-
},
14-
content_security_policy: generate_csp(html),
15-
manifest_version: 2,
16-
name: 'TODO',
17-
version: '0.1'
18-
})
19-
};
20-
21-
function hash_script (s) {
22-
const hashed = sjcl.hash.sha256.hash(s);
23-
return sjcl.codec.base64.fromBits(hashed);
7+
const manifest_filename = 'manifest.json'
8+
9+
function hash_script (s) {
10+
const hashed = sjcl.hash.sha256.hash(s);
11+
return sjcl.codec.base64.fromBits(hashed);
12+
}
13+
14+
function generate_csp (html) {
15+
const $ = cheerio.load(html)
16+
const csp_hashes = $('script[type="module"]')
17+
.map((i, el) => hash_script($(el).get()[0].children[0].data))
18+
.toArray()
19+
.map(h => `'sha256-${h}'`)
20+
.join(' ')
21+
return `script-src 'self' ${csp_hashes}; object-src 'self'`
22+
}
23+
24+
function generate_manifest (html) {
25+
return {
26+
browser_action: {
27+
default_title: 'SvelteKit',
28+
default_popup: 'index.html'
29+
},
30+
content_security_policy: generate_csp(html),
31+
manifest_version: 2,
32+
name: 'TODO',
33+
version: '0.1'
2434
}
35+
}
2536

26-
function generate_csp (html) {
27-
const $ = cheerio.load(html)
28-
const csp_hashes = $('script[type="module"]')
29-
.map((i, el) => hash_script($(el).get()[0].children[0].data))
30-
.toArray()
31-
.map(h => `'sha256-${h}'`)
32-
.join(' ')
33-
return `script-src 'self' ${csp_hashes}; object-src 'self'`
37+
function load_manifest () {
38+
if (existsSync(manifest_filename)) {
39+
return JSON.parse(readFileSync(manifest_filename, 'utf-8'))
3440
}
3541

42+
return {}
43+
}
44+
45+
/** @type {import('.')} */
46+
export default function ({ pages = 'build', assets = pages, fallback } = {}) {
3647
return {
3748
name: 'sveltekit-adapter-browser-extension',
3849

3950
async adapt({ utils }) {
51+
const provided_manifest = load_manifest()
52+
4053
utils.rimraf(assets)
4154
utils.rimraf(pages)
4255

43-
utils.copy_static_files(assets);
44-
utils.copy_client_files(assets);
56+
utils.copy_static_files(assets)
57+
utils.copy_client_files(assets)
4558

4659
await utils.prerender({
4760
fallback,
4861
all: !fallback,
4962
dest: pages
5063
})
5164

52-
const indexPage = join(assets, 'index.html')
53-
const index = readFileSync(indexPage)
65+
const index_page = join(assets, 'index.html')
66+
const index = readFileSync(index_page)
67+
68+
const generated_manifest = generate_manifest(index.toString())
69+
const merged_manifest = applyToDefaults(generated_manifest, provided_manifest, { nullOverride: true })
5470

55-
writeFileSync(join(assets, 'manifest.json'), generate_manifest(index.toString()))
71+
writeFileSync(join(assets, manifest_filename), JSON.stringify(merged_manifest))
5672
utils.rimraf(join(assets, '_app'))
5773
}
5874
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"author": "",
1010
"license": "MIT",
1111
"dependencies": {
12+
"@hapi/hoek": "^9.2.1",
1213
"cheerio": "^1.0.0-rc.10",
1314
"sjcl": "^1.0.8"
1415
}

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)