-
Notifications
You must be signed in to change notification settings - Fork 921
/
types.ts
405 lines (373 loc) · 12.5 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import type {InstallOptions as EsinstallOptions, InstallTarget} from 'esinstall';
import type {Loader} from 'esbuild';
import type * as net from 'net';
import type * as http from 'http';
import type * as http2 from 'http2';
import type {EsmHmrEngine} from './hmr-server-engine';
// RawSourceMap is inlined here for bundle purposes.
// import type {RawSourceMap} from 'source-map';
export interface RawSourceMap {
version: number;
sources: string[];
names: string[];
sourceRoot?: string;
sourcesContent?: string[];
mappings: string;
file: string;
}
export type DeepPartial<T> = {
[P in keyof T]?: T[P] extends Array<infer U>
? Array<DeepPartial<U>>
: T[P] extends ReadonlyArray<infer U>
? ReadonlyArray<DeepPartial<U>>
: DeepPartial<T[P]>;
};
export interface ServerRuntimeConfig {
config: SnowpackConfig;
load: (url: string) => Promise<LoadResult<string>>;
}
export interface ServerRuntime {
importModule: <T = any>(url: string) => Promise<ServerRuntimeModule<T>>;
invalidateModule: (url: string) => void;
}
export interface ServerRuntimeModule<T> {
exports: T;
css: string[];
}
export interface LoadResult<T = Buffer | string> {
contents: T;
imports: InstallTarget[];
originalFileLoc: string | null;
contentType: string | false;
checkStale?: () => Promise<void>;
}
export type OnFileChangeCallback = ({filePath: string}) => any;
export interface LoadUrlOptions {
isSSR?: boolean;
isHMR?: boolean;
isResolve?: boolean;
allowStale?: boolean;
encoding?: undefined | BufferEncoding | null;
importMap?: ImportMap;
}
export interface SnowpackDevServer {
port: number;
hmrEngine?: EsmHmrEngine;
rawServer?: http.Server | http2.Http2Server | undefined;
loadUrl: {
(reqUrl: string, opt?: (LoadUrlOptions & {encoding?: undefined}) | undefined): Promise<
LoadResult<Buffer | string> | undefined
>;
(reqUrl: string, opt: LoadUrlOptions & {encoding: BufferEncoding}): Promise<
LoadResult<string> | undefined
>;
(reqUrl: string, opt: LoadUrlOptions & {encoding: null}): Promise<
LoadResult<Buffer> | undefined
>;
};
handleRequest: (
req: http.IncomingMessage,
res: http.ServerResponse,
options?: {handleError?: boolean},
) => Promise<void>;
sendResponseFile: (
req: http.IncomingMessage,
res: http.ServerResponse,
{contents, originalFileLoc, contentType}: LoadResult,
) => void;
getServerRuntime: (options?: {invalidateOnChange?: boolean}) => ServerRuntime;
sendResponseError: (req: http.IncomingMessage, res: http.ServerResponse, status: number) => void;
getUrlForFile: (fileLoc: string) => string | null;
getUrlForPackage: (packageSpec: string) => Promise<string>;
onFileChange: (callback: OnFileChangeCallback) => void;
shutdown(): Promise<void>;
markChanged: (fileLoc: string) => void;
}
export type SnowpackBuildResultFileManifest = Record<
string,
{source: string | null; contents: string | Buffer}
>;
export interface SnowpackBuildResult {
// result: SnowpackBuildResultFileManifest;
onFileChange: (callback: OnFileChangeCallback) => void;
shutdown(): Promise<void>;
}
export type SnowpackBuiltFile = {
code: string | Buffer;
map?: string;
};
export type SnowpackBuildMap = Record<string, SnowpackBuiltFile>;
/** Standard file interface */
export interface SnowpackSourceFile<Type = string | Buffer> {
/** base extension (e.g. `.js`) */
baseExt: string;
/** file contents */
contents: Type;
/** if no location on disk, assume this exists in memory */
locOnDisk: string;
/** project "root" directory */
root: string;
}
export interface PluginLoadOptions {
/** The absolute file path of the source file, on disk. */
filePath: string;
/** A helper for just the file extension of the source file (ex: ".js", ".svelte") */
fileExt: string;
/** True if builder is in dev mode (`snowpack dev` or `snowpack build --watch`) */
isDev: boolean;
/** True if HMR is enabled (add any HMR code to the output here). */
isHmrEnabled: boolean;
/** True if builder is in SSR mode */
isSSR: boolean;
/** True if file being transformed is inside of a package. */
isPackage: boolean;
}
export interface PluginTransformOptions {
/** The absolute file path of the source file, on disk. */
id: string;
/** The extension of the file */
fileExt: string;
/** Contents of the file to transform */
contents: string | Buffer;
/** True if builder is in dev mode (`snowpack dev` or `snowpack build --watch`) */
isDev: boolean;
/** True if HMR is enabled (add any HMR code to the output here). */
isHmrEnabled: boolean;
/** True if builder is in SSR mode */
isSSR: boolean;
/** True if file being transformed is inside of a package. */
isPackage: boolean;
}
export interface PluginRunOptions {
isDev: boolean;
}
/** map of extensions -> code (e.g. { ".js": "[code]", ".css": "[code]" }) */
export type PluginLoadResult = SnowpackBuildMap;
export type PluginTransformResult = {contents: string; map: string | RawSourceMap};
export interface PluginOptimizeOptions {
buildDirectory: string;
}
export interface SnowpackPlugin {
/** name of the plugin */
name: string;
/** Tell Snowpack how the load() function will resolve files. */
resolve?: {
/**
file extensions that this load function takes as input (e.g. [".jsx",
".js", …])
*/
input: string[];
/**
file extensions that this load function outputs (e.g. [".js", ".css"])
*/
output: string[];
};
/** load a file that matches resolve.input */
load?(options: PluginLoadOptions): Promise<PluginLoadResult | string | null | undefined | void>;
/** transform a file that matches resolve.input */
transform?(
options: PluginTransformOptions,
): Promise<PluginTransformResult | string | null | undefined | void>;
/** runs a command, unrelated to file building (e.g. TypeScript, ESLint) */
run?(options: PluginRunOptions): Promise<unknown>;
/** optimize the entire built application */
optimize?(options: PluginOptimizeOptions): Promise<void>;
/** cleanup any long-running instances/services before exiting. */
cleanup?(): void | Promise<void>;
/** Known dependencies that should be installed */
knownEntrypoints?: string[];
/** read and modify the Snowpack config object */
config?(snowpackConfig: SnowpackConfig): void;
/** Called when a watched file changes during development. */
onChange?({filePath}: {filePath: string}): void;
/** (internal interface, not set by the user) Mark a file as changed. */
markChanged?(file: string): void;
}
/** Snowpack Build Plugin type */
export type SnowpackPluginFactory<PluginOptions = object> = (
snowpackConfig: SnowpackConfig,
pluginOptions?: PluginOptions,
) => SnowpackPlugin;
export type MountEntry = {
url: string;
static: boolean;
resolve: boolean;
};
export interface OptimizeOptions {
entrypoints: 'auto' | string[] | ((options: {files: string[]}) => string[]);
preload: boolean;
bundle: boolean;
loader?: {[ext: string]: Loader};
sourcemap: boolean | 'both' | 'inline' | 'external';
splitting: boolean;
treeshake: boolean;
manifest: boolean;
minify: boolean;
target: 'es2020' | 'es2019' | 'es2018' | 'es2017';
}
export interface RouteConfigObject {
src: string;
dest: string | ((req: http.IncomingMessage, res: http.ServerResponse) => void) | undefined;
upgrade: ((req: http.IncomingMessage, socket: net.Socket, head: Buffer) => void) | undefined;
match: 'routes' | 'all';
_srcRegex: RegExp;
}
export interface PackageOptionsLocal
extends Omit<
EsinstallOptions,
'alias' | 'dest' | 'sourcemap' | 'verbose' | 'logger' | 'cwd' | 'dest' | 'treeshake'
> {
source: 'local' | 'remote-next' | {[key: string]: string};
external: string[];
knownEntrypoints: string[];
}
export interface PackageOptionsRemote {
source: 'remote';
external: string[];
knownEntrypoints: string[];
origin: string;
cache: string;
types: boolean;
}
// interface this library uses internally
export interface SnowpackConfig {
root: string;
mode: 'test' | 'development' | 'production';
workspaceRoot?: string | false;
extends?: string;
exclude: string[];
env?: Record<string, string | boolean | undefined>;
mount: Record<string, MountEntry>;
alias: Record<string, string>;
plugins: SnowpackPlugin[];
dependencies: Record<string, string>;
devOptions: {
secure: boolean | {cert: string | Buffer; key: string | Buffer};
hostname: string;
port: number;
openUrl?: string;
open?: string;
output?: 'stream' | 'dashboard';
hmr?: boolean;
hmrDelay: number;
hmrPort: number | undefined;
hmrErrorOverlay: boolean;
tailwindConfig?: string;
};
buildOptions: {
out: string;
baseUrl: string;
metaUrlPath: string;
cacheDirPath: string;
clean: boolean;
sourcemap: 'inline' | false | undefined;
watch: boolean;
htmlFragments: boolean;
jsxFactory: string | undefined;
jsxFragment: string | undefined;
jsxInject: string | undefined;
ssr: boolean;
resolveProxyImports: boolean;
};
testOptions: {
files: string[];
};
packageOptions: PackageOptionsLocal | PackageOptionsRemote;
/** Optimize your site for production. */
optimize?: OptimizeOptions;
/** Configure routes during development. */
routes: RouteConfigObject[];
/** EXPERIMENTAL - This section is experimental and not yet finalized. May change across minor versions. */
experiments: {
/* intentionally left blank */
};
_extensionMap: Record<string, string[]>;
}
export type SnowpackUserConfig = {
root?: string;
mode?: SnowpackConfig['mode'];
workspaceRoot?: string;
install?: string[];
env?: Record<string, string>;
extends?: string;
exclude?: string[];
mount?: Record<string, string | Partial<MountEntry>>;
alias?: Record<string, string>;
plugins?: (string | [string, any])[];
dependencies?: Record<string, string>;
devOptions?: Partial<SnowpackConfig['devOptions']>;
buildOptions?: Partial<SnowpackConfig['buildOptions']>;
testOptions?: Partial<SnowpackConfig['testOptions']>;
packageOptions?: Partial<SnowpackConfig['packageOptions']>;
optimize?: Partial<SnowpackConfig['optimize']>;
routes?: Pick<RouteConfigObject, 'src' | 'dest' | 'match'>[];
experiments?: {
/* intentionally left blank */
};
};
export interface CLIFlags {
help?: boolean; // display help text
version?: boolean; // display Snowpack version
reload?: boolean;
root?: string; // manual path to project root
config?: string; // manual path to config file
env?: string[]; // env vars
open?: string[];
secure?: boolean;
verbose?: boolean;
quiet?: boolean;
[flag: string]: any;
}
export interface ImportMap {
imports: {[specifier: string]: string};
}
export interface LockfileManifest {
dependencies: {[packageName: string]: string};
lock: {[specifier: string]: string};
}
export interface CommandOptions {
config: SnowpackConfig;
lockfile?: LockfileManifest | null;
}
export type LoggerLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'; // same as Pino
export type LoggerEvent = 'debug' | 'info' | 'warn' | 'error';
export interface LoggerOptions {
/** (optional) change name at beginning of line */
name?: string;
/** (optional) do some additional work after logging a message, if log level is enabled */
task?: Function;
}
/** PackageSource - a common interface for loading and interacting with dependencies. */
export interface PackageSource {
/**
* Do any work needed before starting the dev server or build. Either will wait
* for this to complete before continuing. Example: For "local", this involves
* running esinstall (if needed) to prepare your local dependencies as ESM.
*/
prepare(): Promise<void>;
/**
* Like prepare(), but only looks at a single file and meant to run at anytime,
* usually after the server has already started and is running.
*/
prepareSingleFile(fileLoc: string): Promise<void>;
/**
* Load a dependency with the given spec (ex: "/pkg/react" -> "react")
* If load fails or is unsuccessful, reject the promise.
*/
load(
spec: string,
options: {isSSR: boolean},
): Promise<undefined | {contents: Buffer | string; imports: InstallTarget[]}>;
/** Resolve a package import to URL (ex: "react" -> "/pkg/react") */
resolvePackageImport(
spec: string,
options?: {source?: string; importMap?: ImportMap; depth?: number},
): Promise<string>;
/** Modify the build install config for optimized build install. */
modifyBuildInstallOptions(
installOptions: EsinstallOptions,
installTargets: InstallTarget[],
): Promise<EsinstallOptions>;
getCacheFolder(): string;
clearCache(): void | Promise<void>;
}