-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds
sveltekit-adapter
add-on (#346)
* implement `sveltekit-adapter` add-on * short description * add alias * fiy typo * add to docs * fix add-on flags * tweak * no concurrent storybook test in CI * test --------- Co-authored-by: Manuel Serret <[email protected]>
- Loading branch information
1 parent
cf2d2bc
commit 32deaf0
Showing
7 changed files
with
143 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'sv': patch | ||
--- | ||
|
||
feat: add `sveltekit-adapter` add-on |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { expect } from '@playwright/test'; | ||
import { setupTest } from '../_setup/suite.ts'; | ||
import sveltekitAdapter from '../../sveltekit-adapter/index.ts'; | ||
|
||
const addonId = sveltekitAdapter.id; | ||
const { test, variants, prepareServer } = setupTest({ [addonId]: sveltekitAdapter }); | ||
|
||
const kitOnly = variants.filter((v) => v.includes('kit')); | ||
test.concurrent.for(kitOnly)('core - %s', async (variant, { page, ...ctx }) => { | ||
const cwd = await ctx.run(variant, { [addonId]: { adapter: 'node' } }); | ||
|
||
const { close } = await prepareServer({ cwd, page }); | ||
// kill server process when we're done | ||
ctx.onTestFinished(async () => await close()); | ||
|
||
expect(true).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { defineAddon, defineAddonOptions } from '@sveltejs/cli-core'; | ||
import { exports, functions, imports, object, type AstTypes } from '@sveltejs/cli-core/js'; | ||
import { parseJson, parseScript } from '@sveltejs/cli-core/parsers'; | ||
|
||
type Adapter = { | ||
id: string; | ||
package: string; | ||
version: string; | ||
}; | ||
|
||
const adapters: Adapter[] = [ | ||
{ id: 'node', package: '@sveltejs/adapter-node', version: '^5.2.9' }, | ||
{ id: 'static', package: '@sveltejs/adapter-static', version: '^3.0.6' }, | ||
{ id: 'vercel', package: '@sveltejs/adapter-vercel', version: '^5.5.0' }, | ||
{ id: 'cloudflare-pages', package: '@sveltejs/adapter-cloudflare', version: '^4.8.0' }, | ||
{ id: 'cloudflare-workers', package: '@sveltejs/adapter-cloudflare-workers', version: '^2.6.0' }, | ||
{ id: 'netlify', package: '@sveltejs/adapter-netlify', version: '^4.4.0' } | ||
]; | ||
|
||
const options = defineAddonOptions({ | ||
adapter: { | ||
type: 'select', | ||
question: 'Which SvelteKit adapter would you like to use?', | ||
options: adapters.map((p) => ({ value: p.id, label: p.id, hint: p.package })), | ||
default: 'node' | ||
} | ||
}); | ||
|
||
export default defineAddon({ | ||
id: 'sveltekit-adapter', | ||
alias: 'adapter', | ||
shortDescription: 'deployment', | ||
homepage: 'https://svelte.dev/docs/kit/adapters', | ||
options, | ||
setup: ({ kit, unsupported }) => { | ||
if (!kit) unsupported('Requires SvelteKit'); | ||
}, | ||
run: ({ sv, options }) => { | ||
const adapter = adapters.find((a) => a.id === options.adapter)!; | ||
|
||
// removes previously installed adapters | ||
sv.file('package.json', (content) => { | ||
const { data, generateCode } = parseJson(content); | ||
const devDeps = data['devDependencies']; | ||
|
||
for (const pkg of Object.keys(devDeps)) { | ||
if (pkg.startsWith('@sveltejs/adapter-')) { | ||
delete devDeps[pkg]; | ||
} | ||
} | ||
|
||
return generateCode(); | ||
}); | ||
|
||
sv.devDependency(adapter.package, adapter.version); | ||
|
||
sv.file('svelte.config.js', (content) => { | ||
const { ast, generateCode } = parseScript(content); | ||
|
||
// finds any existing adapter's import declaration | ||
const importDecls = ast.body.filter((n) => n.type === 'ImportDeclaration'); | ||
const adapterImportDecl = importDecls.find( | ||
(importDecl) => | ||
typeof importDecl.source.value === 'string' && | ||
importDecl.source.value.startsWith('@sveltejs/adapter-') && | ||
importDecl.importKind === 'value' | ||
); | ||
|
||
let adapterName = 'adapter'; | ||
if (adapterImportDecl) { | ||
// replaces the import's source with the new adapter | ||
adapterImportDecl.source.value = adapter.package; | ||
adapterName = adapterImportDecl.specifiers?.find((s) => s.type === 'ImportDefaultSpecifier') | ||
?.local?.name as string; | ||
} else { | ||
imports.addDefault(ast, adapter.package, adapterName); | ||
} | ||
|
||
const { value: config } = exports.defaultExport(ast, object.createEmpty()); | ||
const kitConfig = config.properties.find( | ||
(p) => p.type === 'ObjectProperty' && p.key.type === 'Identifier' && p.key.name === 'kit' | ||
) as AstTypes.ObjectProperty | undefined; | ||
|
||
if (kitConfig && kitConfig.value.type === 'ObjectExpression') { | ||
// only overrides the `adapter` property so we can reset it's args | ||
object.overrideProperties(kitConfig.value, { | ||
adapter: functions.callByIdentifier(adapterName, []) | ||
}); | ||
} else { | ||
// creates the `kit` property when absent | ||
object.properties(config, { | ||
kit: object.create({ | ||
adapter: functions.callByIdentifier(adapterName, []) | ||
}) | ||
}); | ||
} | ||
|
||
return generateCode(); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters