Skip to content

Commit

Permalink
select adapter immediately on startup (#4299)
Browse files Browse the repository at this point in the history
* select adapter immediately on startup

* simplify

* changesets

* lint
  • Loading branch information
Rich-Harris authored Mar 11, 2022
1 parent 2f21125 commit 818b489
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 29 deletions.
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

0 comments on commit 818b489

Please sign in to comment.