|
1 | 1 | import { adapters } from './adapters.js'; |
2 | 2 |
|
3 | | -/** @type {import('.')} **/ |
4 | | -export default function () { |
5 | | - return { |
6 | | - name: '@sveltejs/adapter-auto', |
| 3 | +/** @type {import('./index')} */ |
| 4 | +let fn; |
| 5 | + |
| 6 | +for (const candidate of adapters) { |
| 7 | + if (candidate.test()) { |
| 8 | + /** @type {{ default: () => import('@sveltejs/kit').Adapter }} */ |
| 9 | + let module; |
7 | 10 |
|
8 | | - async adapt(builder) { |
9 | | - for (const candidate of adapters) { |
10 | | - if (candidate.test()) { |
11 | | - builder.log.info(`Detected environment: ${candidate.name}. Using ${candidate.module}`); |
12 | | - |
13 | | - let module; |
14 | | - |
15 | | - try { |
16 | | - module = await import(candidate.module); |
17 | | - } catch (error) { |
18 | | - if ( |
19 | | - error.code === 'ERR_MODULE_NOT_FOUND' && |
20 | | - error.message.startsWith(`Cannot find package '${candidate.module}'`) |
21 | | - ) { |
22 | | - throw new Error( |
23 | | - `It looks like ${candidate.module} is not installed. Please install it and try building your project again.` |
24 | | - ); |
25 | | - } |
26 | | - |
27 | | - throw error; |
| 11 | + try { |
| 12 | + module = await import(candidate.module); |
| 13 | + |
| 14 | + fn = () => { |
| 15 | + const adapter = module.default(); |
| 16 | + return { |
| 17 | + ...adapter, |
| 18 | + adapt: (builder) => { |
| 19 | + builder.log.info(`Detected environment: ${candidate.name}. Using ${candidate.module}`); |
| 20 | + return adapter.adapt(builder); |
28 | 21 | } |
| 22 | + }; |
| 23 | + }; |
29 | 24 |
|
30 | | - const adapter = module.default(); |
31 | | - return adapter.adapt(builder); |
32 | | - } |
| 25 | + break; |
| 26 | + } catch (error) { |
| 27 | + if ( |
| 28 | + error.code === 'ERR_MODULE_NOT_FOUND' && |
| 29 | + error.message.startsWith(`Cannot find package '${candidate.module}'`) |
| 30 | + ) { |
| 31 | + throw new Error( |
| 32 | + `It looks like ${candidate.module} is not installed. Please install it and try building your project again.` |
| 33 | + ); |
33 | 34 | } |
34 | 35 |
|
| 36 | + throw error; |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +if (!fn) { |
| 42 | + fn = () => ({ |
| 43 | + name: '@sveltejs/adapter-auto', |
| 44 | + adapt: (builder) => { |
35 | 45 | builder.log.warn( |
36 | 46 | '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' |
37 | 47 | ); |
38 | 48 | } |
39 | | - }; |
| 49 | + }); |
40 | 50 | } |
| 51 | + |
| 52 | +export default fn; |
0 commit comments