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 fallback: string;
210 open: string;
211 output: 'stream' | 'dashboard';
212 hmr?: boolean;
213 hmrDelay: number;
214 hmrPort: number | undefined;
215 hmrErrorOverlay: boolean;
216 };
217 buildOptions: {
218 out: string;
219 baseUrl: string;
220 metaUrlPath: string;
221 clean: boolean;
222 sourcemap: boolean;
223 watch: boolean;
224 htmlFragments: boolean;
225 jsxFactory: string | undefined;
226 jsxFragment: string | undefined;
227 ssr: boolean;
228 };
229 testOptions: {
230 files: string[];
231 };
232 packageOptions: PackageSourceLocal | PackageSourceRemote;
233 /** Optimize your site for production. */
234 optimize?: OptimizeOptions;
235 /** Configure routes during development. */
236 routes: RouteConfigObject[];
237 /** EXPERIMENTAL - This section is experimental and not yet finalized. May change across minor versions. */
238 experiments: {};
239 _extensionMap: Record<string, string[]>;
240}
241export declare type SnowpackUserConfig = {
242 root?: string;
243 install?: string[];
244 extends?: string;
245 exclude?: string[];
246 mount?: Record<string, string | Partial<MountEntry>>;
247 alias?: Record<string, string>;
248 plugins?: (string | [string, any])[];
249 devOptions?: Partial<SnowpackConfig['devOptions']>;
250 buildOptions?: Partial<SnowpackConfig['buildOptions']>;
251 testOptions?: Partial<SnowpackConfig['testOptions']>;
252 packageOptions?: Partial<SnowpackConfig['packageOptions']>;
253 optimize?: Partial<SnowpackConfig['optimize']>;
254 routes?: Pick<RouteConfigObject, 'src' | 'dest' | 'match'>[];
255 experiments?: {};
256};
257export interface CLIFlags {
258 help?: boolean;
259 version?: boolean;
260 reload?: boolean;
261 root?: string;
262 config?: string;
263 env?: string[];
264 open?: string[];
265 secure?: boolean;
266 verbose?: boolean;
267 quiet?: boolean;
268 [flag: string]: any;
269}
270export interface ImportMap {
271 imports: {
272 [specifier: string]: string;
273 };
274}
275export interface LockfileManifest {
276 dependencies: {
277 [packageName: string]: string;
278 };
279 lock: {
280 [specifier: string]: string;
281 };
282}
283export interface CommandOptions {
284 config: SnowpackConfig;
285 lockfile: LockfileManifest | null;
286}
287export declare type LoggerLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';
288export declare type LoggerEvent = 'debug' | 'info' | 'warn' | 'error';
289export interface LoggerOptions {
290 /** (optional) change name at beginning of line */
291 name?: string;
292 /** (optional) do some additional work after logging a message, if log level is enabled */
293 task?: Function;
294}
295/** PackageSource - a common interface for loading and interacting with dependencies. */
296export interface PackageSource {
297 /**
298 * Do any work needed before starting the dev server or build. Either will wait
299 * for this to complete before continuing. Example: For "local", this involves
300 * running esinstall (if needed) to prepare your local dependencies as ESM.
301 */
302 prepare(commandOptions: CommandOptions): Promise<ImportMap>;
303 /**
304 * Load a dependency with the given spec (ex: "/pkg/react" -> "react")
305 * If load fails or is unsuccessful, reject the promise.
306 */
307 load(spec: string, options: {
308 config: SnowpackConfig;
309 lockfile: LockfileManifest | null;
310 }): Promise<Buffer | string>;
311 /** Resolve a package import to URL (ex: "react" -> "/pkg/react") */
312 resolvePackageImport(spec: string, importMap: ImportMap, config: SnowpackConfig): string | false;
313 /** Handle 1+ missing package imports before failing, if possible. */
314 recoverMissingPackageImport(missingPackages: string[], config: SnowpackConfig): Promise<ImportMap>;
315 /** Modify the build install config for optimized build install. */
316 modifyBuildInstallOptions(options: {
317 installOptions: EsinstallOptions;
318 config: SnowpackConfig;
319 lockfile: LockfileManifest | null;
320 }): EsinstallOptions;
321 getCacheFolder(config: SnowpackConfig): string;
322 clearCache(): void | Promise<void>;
323}
324
\No newline at end of file