Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] adapter API breaking changes #9

Merged
merged 2 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

## Usage

Install with `npm i -D sveltekit-adapter-browser-extension`, then add the adapter to your `svelte.config.js`, and set the appDir to something without an underscore:
Install with `npm i -D sveltekit-adapter-browser-extension`, then add the adapter to your `svelte.config.js`.

Some additional configuration is required for this adapter to work - you need to (1) set ``appDir`` to something without an underscore and (2) tell kit to prerender its pages by default:

```js
// svelte.config.js
Expand All @@ -13,7 +15,10 @@ import adapter from 'sveltekit-adapter-browser-extension';
export default {
kit: {
adapter: adapter(),
appDir: 'ext' // This is important - chrome extensions can't handle the default _app directory name.
appDir: 'ext', // This is important - chrome extensions can't handle the default _app directory name.
prerender: {
default: true
}
}
};
```
Expand Down
28 changes: 16 additions & 12 deletions adapter-browser-extension.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,29 @@ function load_manifest () {
return {}
}


/** @type {import('.')} */
export default function ({ pages = 'build', assets = pages, fallback } = {}) {
return {
name: 'sveltekit-adapter-browser-extension',

async adapt({ utils }) {
async adapt(builder) {
if (!fallback && !builder.config.kit.prerender.default) {
builder.log.warn(
'You should set `config.kit.prerender.default` to `true` if no fallback is specified'
);
}


const provided_manifest = load_manifest()

utils.rimraf(assets)
utils.rimraf(pages)
builder.rimraf(assets)
builder.rimraf(pages)

utils.copy_static_files(assets)
utils.copy_client_files(assets)
builder.writeStatic(assets)
builder.writeClient(assets)

await utils.prerender({
fallback,
all: !fallback,
dest: pages
})
builder.writePrerendered(pages, { fallback });

const index_page = join(assets, 'index.html')
const index = readFileSync(index_page)
Expand All @@ -69,7 +73,7 @@ export default function ({ pages = 'build', assets = pages, fallback } = {}) {
const merged_manifest = applyToDefaults(generated_manifest, provided_manifest, { nullOverride: true })

writeFileSync(join(assets, manifest_filename), JSON.stringify(merged_manifest))
utils.rimraf(join(assets, '_app'))
builder.rimraf(join(assets, '_app'))
}
}
}
}