1
1
// This file is a rewrite of `@sveltejs/vite-plugin-svelte` without the `Vite`
2
2
// parts: https://github.com/sveltejs/vite-plugin-svelte/blob/e8e52deef93948da735c4ab69c54aced914926cf/packages/vite-plugin-svelte/src/utils/load-svelte-config.ts
3
- import { fileURLToPath , pathToFileURL } from 'url' ;
3
+ import fs from 'node:fs' ;
4
+ import { createRequire } from 'node:module' ;
5
+ import path from 'node:path' ;
6
+ import { fileURLToPath , pathToFileURL } from 'node:url' ;
4
7
5
- import { createRequire } from 'module' ;
6
- import fs from 'fs' ;
7
8
import { logger } from '@storybook/node-logger' ;
8
- import path from 'path' ;
9
- import { pathExists } from 'fs-extra' ;
9
+ import type { Config } from '@sveltejs/kit' ;
10
10
11
11
/**
12
12
* Try find svelte config and then load it.
13
13
*
14
- * @returns { import('@sveltejs/kit').Config | undefined }
14
+ * @returns
15
15
* Returns the svelte configuration object.
16
16
*/
17
- export async function loadSvelteConfig ( ) {
17
+ export async function loadSvelteConfig ( ) : Promise < Config | undefined > {
18
18
const configFile = await findSvelteConfig ( ) ;
19
19
20
20
// no need to throw error since we handle projects without config files
21
21
if ( configFile === undefined ) {
22
22
return undefined ;
23
23
}
24
24
25
- let err ;
25
+ let err : unknown ;
26
26
27
27
// try to use dynamic import for svelte.config.js first
28
28
if ( configFile . endsWith ( '.js' ) || configFile . endsWith ( '.mjs' ) ) {
@@ -61,13 +61,13 @@ const importSvelteOptions = (() => {
61
61
/**
62
62
* Try import specified svelte configuration.
63
63
*
64
- * @param { string } configFile
64
+ * @param configFile
65
65
* Absolute path of the svelte config file to import.
66
66
*
67
- * @returns { import('@sveltejs/kit').Config }
67
+ * @returns
68
68
* Returns the svelte configuration object.
69
69
*/
70
- return async ( configFile ) => {
70
+ return async ( configFile : string ) : Promise < Config > => {
71
71
const result = await dynamicImportDefault (
72
72
pathToFileURL ( configFile ) . href ,
73
73
fs . statSync ( configFile ) . mtimeMs
@@ -81,19 +81,18 @@ const importSvelteOptions = (() => {
81
81
} ) ( ) ;
82
82
83
83
const requireSvelteOptions = ( ( ) => {
84
- /** @type {NodeRequire } */
85
- let esmRequire ;
84
+ let esmRequire : NodeRequire ;
86
85
87
86
/**
88
87
* Try import specified svelte configuration.
89
88
*
90
- * @param { string } configFile
89
+ * @param configFile
91
90
* Absolute path of the svelte config file to require.
92
91
*
93
- * @returns { import('@sveltejs/kit').Config }
92
+ * @returns
94
93
* Returns the svelte configuration object.
95
94
*/
96
- return ( configFile ) => {
95
+ return ( configFile : string ) : Config => {
97
96
// identify which require function to use (esm and cjs mode)
98
97
const requireFn = import . meta. url
99
98
? ( esmRequire = esmRequire ?? createRequire ( import . meta. url ) )
@@ -114,10 +113,10 @@ const requireSvelteOptions = (() => {
114
113
* Try find svelte config. First in current working dir otherwise try to
115
114
* find it by climbing up the directory tree.
116
115
*
117
- * @returns { Promise<string | undefined> }
116
+ * @returns
118
117
* Returns the absolute path of the config file.
119
118
*/
120
- async function findSvelteConfig ( ) {
119
+ async function findSvelteConfig ( ) : Promise < string | undefined > {
121
120
const lookupDir = process . cwd ( ) ;
122
121
let configFiles = await getConfigFiles ( lookupDir ) ;
123
122
@@ -140,10 +139,10 @@ async function findSvelteConfig() {
140
139
* Returning the first found. Should solves most of monorepos with
141
140
* only one config at workspace root.
142
141
*
143
- * @returns { Promise<string[]> }
142
+ * @returns
144
143
* Returns an array containing all available config files.
145
144
*/
146
- async function getConfigFilesUp ( ) {
145
+ async function getConfigFilesUp ( ) : Promise < string [ ] > {
147
146
const importPath = fileURLToPath ( import . meta. url ) ;
148
147
const pathChunks = path . dirname ( importPath ) . split ( path . sep ) ;
149
148
@@ -164,22 +163,21 @@ async function getConfigFilesUp() {
164
163
/**
165
164
* Gets all svelte config from a specified `lookupDir`.
166
165
*
167
- * @param { string } lookupDir
166
+ * @param lookupDir
168
167
* Directory in which to look for svelte files.
169
168
*
170
- * @returns { Promise<string[]> }
169
+ * @returns
171
170
* Returns an array containing all available config files.
172
171
*/
173
- async function getConfigFiles ( lookupDir ) {
174
- /** @type {[string, boolean][] } */
175
- const fileChecks = await Promise . all (
172
+ async function getConfigFiles ( lookupDir : string ) : Promise < string [ ] > {
173
+ const fileChecks : Array < [ string , boolean ] > = await Promise . all (
176
174
knownConfigFiles . map ( async ( candidate ) => {
177
175
const filePath = path . resolve ( lookupDir , candidate ) ;
178
- return [ filePath , await pathExists ( filePath ) ] ;
176
+ return [ filePath , fs . existsSync ( filePath ) ] ;
179
177
} )
180
178
) ;
181
179
182
- return fileChecks . reduce ( ( files , [ file , exists ] ) => {
180
+ return fileChecks . reduce ( ( files : string [ ] , [ file , exists ] ) => {
183
181
if ( exists ) files . push ( file ) ;
184
182
return files ;
185
183
} , [ ] ) ;
0 commit comments