UNPKG

11.4 kBTypeScriptView Raw
1/// <reference types="node" />
2import type { InstallOptions as EsinstallOptions } from '../vendor/types/esinstall';
3import type * as http from 'http';
4export interface RawSourceMap {
5 version: number;
6 sources: string[];
7 names: string[];
8 sourceRoot?: string;
9 sourcesContent?: string[];
10 mappings: string;
11 file: string;
12}
13export declare type DeepPartial<T> = {
14 [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]>;
15};
16export interface SSRLoaderConfig {
17 load: (url: string) => Promise<{
18 contents: string;
19 }>;
20}
21export interface SSRLoader {
22 importModule: <T = any>(url: string) => Promise<ESMRuntimeModule<T>>;
23 invalidateModule: (url: string) => void;
24}
25export interface ESMRuntimeModule<T> {
26 exports: T;
27 css: string[];
28}
29export interface LoadResult<T = Buffer | string> {
30 contents: T;
31 originalFileLoc: string | null;
32 contentType: string | false;
33 checkStale?: () => Promise<void>;
34}
35export declare type OnFileChangeCallback = ({ filePath: string }: {
36 filePath: any;
37}) => any;
38export interface SnowpackDevServer {
39 port: number;
40 loadUrl: {
41 (reqUrl: string, opt?: {
42 isSSR?: boolean | undefined;
43 allowStale?: boolean | undefined;
44 encoding?: undefined;
45 } | undefined): Promise<LoadResult<Buffer | string>>;
46 (reqUrl: string, opt: {
47 isSSR?: boolean;
48 allowStale?: boolean;
49 encoding: BufferEncoding;
50 }): Promise<LoadResult<string>>;
51 (reqUrl: string, opt: {
52 isSSR?: boolean;
53 allowStale?: boolean;
54 encoding: null;
55 }): Promise<LoadResult<Buffer>>;
56 };
57 handleRequest: (req: http.IncomingMessage, res: http.ServerResponse, options?: {
58 handleError?: boolean;
59 }) => Promise<void>;
60 sendResponseFile: (req: http.IncomingMessage, res: http.ServerResponse, { contents, originalFileLoc, contentType }: LoadResult) => void;
61 getServerRuntime: (options?: {
62 invalidateOnChange?: boolean;
63 }) => SSRLoader;
64 sendResponseError: (req: http.IncomingMessage, res: http.ServerResponse, status: number) => void;
65 getUrlForFile: (fileLoc: string) => string | null;
66 onFileChange: (callback: OnFileChangeCallback) => void;
67 shutdown(): Promise<void>;
68}
69export declare type SnowpackBuildResultFileManifest = Record<string, {
70 source: string | null;
71 contents: string | Buffer;
72}>;
73export interface SnowpackBuildResult {
74 result: SnowpackBuildResultFileManifest;
75 onFileChange: (callback: OnFileChangeCallback) => void;
76 shutdown(): Promise<void>;
77}
78export declare type SnowpackBuiltFile = {
79 code: string | Buffer;
80 map?: string;
81};
82export declare type SnowpackBuildMap = Record<string, SnowpackBuiltFile>;
83/** Standard file interface */
84export interface SnowpackSourceFile<Type = string | Buffer> {
85 /** base extension (e.g. `.js`) */
86 baseExt: string;
87 /** file contents */
88 contents: Type;
89 /** if no location on disk, assume this exists in memory */
90 locOnDisk: string;
91 /** project "root" directory */
92 root: string;
93}
94export interface PluginLoadOptions {
95 /** The absolute file path of the source file, on disk. */
96 filePath: string;
97 /** A helper for just the file extension of the source file (ex: ".js", ".svelte") */
98 fileExt: string;
99 /** True if builder is in dev mode (`snowpack dev` or `snowpack build --watch`) */
100 isDev: boolean;
101 /** True if builder is in SSR mode */
102 isSSR: boolean;
103 /** True if HMR is enabled (add any HMR code to the output here). */
104 isHmrEnabled: boolean;
105}
106export interface PluginTransformOptions {
107 id: string;
108 fileExt: string;
109 contents: string | Buffer;
110 isDev: boolean;
111 isHmrEnabled: boolean;
112}
113export interface PluginRunOptions {
114 isDev: boolean;
115}
116/** map of extensions -> code (e.g. { ".js": "[code]", ".css": "[code]" }) */
117export declare type PluginLoadResult = SnowpackBuildMap;
118export declare type PluginTransformResult = {
119 contents: string;
120 map: string | RawSourceMap;
121};
122export interface PluginOptimizeOptions {
123 buildDirectory: string;
124}
125export interface SnowpackPlugin {
126 /** name of the plugin */
127 name: string;
128 /** Tell Snowpack how the load() function will resolve files. */
129 resolve?: {
130 /**
131 file extensions that this load function takes as input (e.g. [".jsx",
132 ".js", …])
133 */
134 input: string[];
135 /**
136 file extensions that this load function outputs (e.g. [".js", ".css"])
137 */
138 output: string[];
139 };
140 /** load a file that matches resolve.input */
141 load?(options: PluginLoadOptions): Promise<PluginLoadResult | string | null | undefined | void>;
142 /** transform a file that matches resolve.input */
143 transform?(options: PluginTransformOptions): Promise<PluginTransformResult | string | null | undefined | void>;
144 /** runs a command, unrelated to file building (e.g. TypeScript, ESLint) */
145 run?(options: PluginRunOptions): Promise<unknown>;
146 /** optimize the entire built application */
147 optimize?(options: PluginOptimizeOptions): Promise<void>;
148 /** cleanup any long-running instances/services before exiting. */
149 cleanup?(): void | Promise<void>;
150 /** Known dependencies that should be installed */
151 knownEntrypoints?: string[];
152 /** read and modify the Snowpack config object */
153 config?(snowpackConfig: SnowpackConfig): void;
154 /** Called when a watched file changes during development. */
155 onChange?({ filePath }: {
156 filePath: string;
157 }): void;
158 /** (internal interface, not set by the user) Mark a file as changed. */
159 markChanged?(file: string): void;
160}
161/** Snowpack Build Plugin type */
162export declare type SnowpackPluginFactory<PluginOptions = object> = (snowpackConfig: SnowpackConfig, pluginOptions?: PluginOptions) => SnowpackPlugin;
163export declare type MountEntry = {
164 url: string;
165 static: boolean;
166 resolve: boolean;
167};
168export interface OptimizeOptions {
169 entrypoints: 'auto' | string[] | ((options: {
170 files: string[];
171 }) => string[]);
172 preload: boolean;
173 bundle: boolean;
174 splitting: boolean;
175 treeshake: boolean;
176 manifest: boolean;
177 minify: boolean;
178 target: 'es2020' | 'es2019' | 'es2018' | 'es2017';
179}
180export interface RouteConfigObject {
181 src: string;
182 dest: string | ((req: http.IncomingMessage, res: http.ServerResponse) => void);
183 match: 'routes' | 'all';
184 _srcRegex: RegExp;
185}
186export interface PackageSourceLocal extends Omit<EsinstallOptions, 'alias' | 'dest' | 'sourcemap' | 'verbose' | 'logger' | 'cwd' | 'dest' | 'treeshake'> {
187 source: 'local';
188 external: string[];
189 knownEntrypoints: string[];
190}
191export interface PackageSourceRemote {
192 source: 'remote';
193 origin: string;
194 external: string[];
195 cache: string;
196 types: boolean;
197}
198export interface SnowpackConfig {
199 root: string;
200 extends?: string;
201 exclude: string[];
202 mount: Record<string, MountEntry>;
203 alias: Record<string, string>;
204 plugins: SnowpackPlugin[];
205 devOptions: {
206 secure: boolean;
207 hostname: string;
208 port: number;
209 open: string;
210 output: 'stream' | 'dashboard';
211 hmr?: boolean;
212 hmrDelay: number;
213 hmrPort: number | undefined;
214 hmrErrorOverlay: boolean;
215 };
216 buildOptions: {
217 out: string;
218 baseUrl: string;
219 metaUrlPath: string;
220 clean: boolean;
221 sourcemap: boolean;
222 watch: boolean;
223 htmlFragments: boolean;
224 jsxFactory: string | undefined;
225 jsxFragment: string | undefined;
226 ssr: boolean;
227 };
228 testOptions: {
229 files: string[];
230 };
231 packageOptions: PackageSourceLocal | PackageSourceRemote;
232 /** Optimize your site for production. */
233 optimize?: OptimizeOptions;
234 /** Configure routes during development. */
235 routes: RouteConfigObject[];
236 /** EXPERIMENTAL - This section is experimental and not yet finalized. May change across minor versions. */
237 experiments: {};
238 _extensionMap: Record<string, string[]>;
239}
240export declare type SnowpackUserConfig = {
241 root?: string;
242 install?: string[];
243 extends?: string;
244 exclude?: string[];
245 mount?: Record<string, string | Partial<MountEntry>>;
246 alias?: Record<string, string>;
247 plugins?: (string | [string, any])[];
248 devOptions?: Partial<SnowpackConfig['devOptions']>;
249 buildOptions?: Partial<SnowpackConfig['buildOptions']>;
250 testOptions?: Partial<SnowpackConfig['testOptions']>;
251 packageOptions?: Partial<SnowpackConfig['packageOptions']>;
252 optimize?: Partial<SnowpackConfig['optimize']>;
253 routes?: Pick<RouteConfigObject, 'src' | 'dest' | 'match'>[];
254 experiments?: {};
255};
256export interface CLIFlags {
257 help?: boolean;
258 version?: boolean;
259 reload?: boolean;
260 root?: string;
261 config?: string;
262 env?: string[];
263 open?: string[];
264 secure?: boolean;
265 verbose?: boolean;
266 quiet?: boolean;
267 [flag: string]: any;
268}
269export interface ImportMap {
270 imports: {
271 [specifier: string]: string;
272 };
273}
274export interface LockfileManifest {
275 dependencies: {
276 [packageName: string]: string;
277 };
278 lock: {
279 [specifier: string]: string;
280 };
281}
282export interface CommandOptions {
283 config: SnowpackConfig;
284 lockfile: LockfileManifest | null;
285}
286export declare type LoggerLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';
287export declare type LoggerEvent = 'debug' | 'info' | 'warn' | 'error';
288export interface LoggerOptions {
289 /** (optional) change name at beginning of line */
290 name?: string;
291 /** (optional) do some additional work after logging a message, if log level is enabled */
292 task?: Function;
293}
294/** PackageSource - a common interface for loading and interacting with dependencies. */
295export interface PackageSource {
296 /**
297 * Do any work needed before starting the dev server or build. Either will wait
298 * for this to complete before continuing. Example: For "local", this involves
299 * running esinstall (if needed) to prepare your local dependencies as ESM.
300 */
301 prepare(commandOptions: CommandOptions): Promise<ImportMap>;
302 /**
303 * Load a dependency with the given spec (ex: "/pkg/react" -> "react")
304 * If load fails or is unsuccessful, reject the promise.
305 */
306 load(spec: string, options: {
307 config: SnowpackConfig;
308 lockfile: LockfileManifest | null;
309 }): Promise<Buffer | string>;
310 /** Resolve a package import to URL (ex: "react" -> "/pkg/react") */
311 resolvePackageImport(spec: string, importMap: ImportMap, config: SnowpackConfig): string | false;
312 /** Handle 1+ missing package imports before failing, if possible. */
313 recoverMissingPackageImport(missingPackages: string[], config: SnowpackConfig): Promise<ImportMap>;
314 /** Modify the build install config for optimized build install. */
315 modifyBuildInstallOptions(options: {
316 installOptions: EsinstallOptions;
317 config: SnowpackConfig;
318 lockfile: LockfileManifest | null;
319 }): EsinstallOptions;
320 getCacheFolder(config: SnowpackConfig): string;
321 clearCache(): void | Promise<void>;
322}
323
\No newline at end of file