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

select adapter immediately on startup #4299

Merged
merged 4 commits into from
Mar 11, 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
5 changes: 5 additions & 0 deletions .changeset/cool-crabs-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Allow adapter.adapt to be synchronous
5 changes: 5 additions & 0 deletions .changeset/tasty-squids-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-auto': patch
---

Select adapter immediately
68 changes: 40 additions & 28 deletions packages/adapter-auto/index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
import { adapters } from './adapters.js';

/** @type {import('.')} **/
export default function () {
return {
name: '@sveltejs/adapter-auto',
/** @type {import('./index')} */
let fn;

for (const candidate of adapters) {
if (candidate.test()) {
/** @type {{ default: () => import('@sveltejs/kit').Adapter }} */
let module;

async adapt(builder) {
for (const candidate of adapters) {
if (candidate.test()) {
builder.log.info(`Detected environment: ${candidate.name}. Using ${candidate.module}`);

let module;

try {
module = await import(candidate.module);
} catch (error) {
if (
error.code === 'ERR_MODULE_NOT_FOUND' &&
error.message.startsWith(`Cannot find package '${candidate.module}'`)
) {
throw new Error(
`It looks like ${candidate.module} is not installed. Please install it and try building your project again.`
);
}

throw error;
try {
module = await import(candidate.module);

fn = () => {
const adapter = module.default();
return {
...adapter,
adapt: (builder) => {
builder.log.info(`Detected environment: ${candidate.name}. Using ${candidate.module}`);
return adapter.adapt(builder);
}
};
};

const adapter = module.default();
return adapter.adapt(builder);
}
break;
} catch (error) {
if (
error.code === 'ERR_MODULE_NOT_FOUND' &&
error.message.startsWith(`Cannot find package '${candidate.module}'`)
) {
throw new Error(
`It looks like ${candidate.module} is not installed. Please install it and try building your project again.`
);
}

throw error;
}
}
}

if (!fn) {
fn = () => ({
name: '@sveltejs/adapter-auto',
adapt: (builder) => {
builder.log.warn(
'Could not detect a supported production environment. See https://kit.svelte.dev/docs/adapters to learn how to configure your app to run on the platform of your choosing'
);
}
};
});
}

export default fn;
3 changes: 3 additions & 0 deletions packages/adapter-auto/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"checkJs": true,
"noEmit": true,
"noImplicitAny": true,
"module": "esnext",
"target": "esnext",
"moduleResolution": "node",
"baseUrl": ".",
"paths": {
"@sveltejs/kit": ["../kit/types/index"]
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { SSRNodeLoader, SSRRoute, ValidatedConfig } from './internal';

export interface Adapter {
name: string;
adapt(builder: Builder): Promise<void>;
adapt(builder: Builder): MaybePromise<void>;
}

export interface Builder {
Expand Down