UNPKG

22.9 kBTypeScriptView Raw
1// Type definitions for browser-sync 2.27
2// Project: http://www.browsersync.io/
3// Definitions by: Asana <https://asana.com>,
4// Joe Skeen <https://github.com/joeskeen>
5// Thomas "Thasmo" Deinhamer <https://thasmo.com/>
6// Kiyotoshi Ichikawa <https://github.com/aznnomness>
7// Yuma Hashimoto <https://github.com/yuma84>
8// Andreas Bomholtz <https://github.com/seluxit>
9// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
10// TypeScript Version: 2.3
11
12/// <reference types="node" />
13/// <reference types="serve-static" />
14
15import * as chokidar from "chokidar";
16import * as fs from "fs";
17import * as http from "http";
18import * as mm from "micromatch";
19import { ServeStaticOptions } from "serve-static";
20
21declare namespace browserSync {
22 interface Options {
23 /**
24 * Browsersync includes a user-interface that is accessed via a separate port. The UI allows to controls
25 * all devices, push sync updates and much more.
26 *
27 * port - Default: 3001
28 * weinre.port - Default: 8080
29 * Note: Requires at least version 2.0.0.
30 */
31 ui?: UIOptions | boolean | undefined;
32 /**
33 * Browsersync can watch your files as you work. Changes you make will either be injected into the page (CSS
34 * & images) or will cause all browsers to do a full-page refresh. See anymatch for more information on glob
35 * patterns.
36 * Default: false
37 */
38 files?: string | (string | FileCallback | object)[] | undefined;
39 /**
40 * Specify which file events to respond to.
41 * Available events: `add`, `change`, `unlink`, `addDir`, `unlinkDir`
42 */
43 watchEvents?: WatchEvents | string[] | undefined;
44 /**
45 * Watch files automatically.
46 */
47 watch?: boolean | undefined;
48 /**
49 * Patterns for any watchers to ignore.
50 * Anything provided here will end up inside 'watchOptions.ignored'.
51 */
52 ignore?: string[] | undefined;
53 /**
54 * Serve an index.html file for all non-asset routes.
55 * Useful when using client-routers.
56 */
57 single?: boolean | undefined;
58 /**
59 * File watching options that get passed along to Chokidar. Check their docs for available options
60 * Default: undefined
61 * Note: Requires at least version 2.6.0.
62 */
63 watchOptions?: chokidar.WatchOptions | undefined;
64 /**
65 * Use the built-in static server for basic HTML/JS/CSS websites.
66 * Default: false
67 */
68 server?: string | boolean | string[] | ServerOptions | undefined;
69 /**
70 * Proxy an EXISTING vhost. Browsersync will wrap your vhost with a proxy URL to view your site.
71 * Passing only a URL as a string equates to passing only target property of ProxyOptions type.
72 * target - Default: undefined
73 * ws - Default: undefined
74 * middleware - Default: undefined
75 * reqHeaders - Default: undefined
76 * proxyRes - Default: undefined (http.ServerResponse if expecting single parameter)
77 * proxyReq - Default: undefined
78 */
79 proxy?: string | ProxyOptions | undefined;
80 /**
81 * Use a specific port (instead of the one auto-detected by Browsersync)
82 * Default: 3000
83 */
84 port?: number | undefined;
85 /**
86 * Functions or actual plugins used as middleware.
87 */
88 middleware?: MiddlewareHandler | PerRouteMiddleware | (MiddlewareHandler | PerRouteMiddleware)[] | undefined;
89 /**
90 * Add additional directories from which static files should be served.
91 * Should only be used in proxy or snippet mode.
92 * Default: []
93 * Note: Requires at least version 2.8.0.
94 */
95 serveStatic?: StaticOptions[] | string[] | undefined;
96 /**
97 * Options that are passed to the serve-static middleware when you use the
98 * string[] syntax: eg: `serveStatic: ['./app']`.
99 * Please see [serve-static](https://github.com/expressjs/serve-static) for details.
100 */
101 serveStaticOptions?: ServeStaticOptions | undefined;
102 /**
103 * Enable https for localhost development.
104 * Note: This may not be needed for proxy option as it will try to infer from your target url.
105 * Note: If privacy error is encountered please see HttpsOptions below, setting those will resolve.
106 * Note: Requires at least version 1.3.0.
107 */
108 https?: boolean | HttpsOptions | undefined;
109 /**
110 * Override http module to allow using 3rd party server modules (such as http2).
111 */
112 httpModule?: string | undefined;
113 /**
114 * Current working directory
115 */
116 cwd?: string;
117 /**
118 * Register callbacks via a regular option - this can be used to get access the Browsersync
119 * instance in situations where you cannot provide a callback via the normal API (for example,
120 * in a Gruntfile)
121 *
122 * This 'ready' callback can be used to access the Browsersync instance
123 */
124 callbacks?: CallbacksOptions;
125 /**
126 * Clicks, Scrolls & Form inputs on any device will be mirrored to all others.
127 * clicks - Default: true
128 * scroll - Default: true
129 * forms - Default: true
130 */
131 ghostMode?: GhostOptions | boolean | undefined;
132 /**
133 * Can be either "info", "debug", "warn", or "silent"
134 * Default: info
135 */
136 logLevel?: LogLevel | undefined;
137 /**
138 * Change the console logging prefix. Useful if you're creating your own project based on Browsersync
139 * Default: BS
140 * Note: Requires at least version 1.5.1.
141 */
142 logPrefix?: string | undefined;
143 /**
144 * Whether or not to log connections
145 * Default: false
146 */
147 logConnections?: boolean | undefined;
148 /**
149 * Whether or not to log information about changed files
150 * Default: false
151 */
152 logFileChanges?: boolean | undefined;
153 /**
154 * Log the snippet to the console when you're in snippet mode (no proxy/server)
155 * Default: true
156 * Note: Requires at least version 1.5.2.
157 */
158 logSnippet?: boolean | undefined;
159 /**
160 * You can control how the snippet is injected onto each page via a custom regex + function.
161 * You can also provide patterns for certain urls that should be ignored from the snippet injection.
162 * Note: Requires at least version 2.0.0.
163 */
164 snippetOptions?: SnippetOptions | undefined;
165 /**
166 * Add additional HTML rewriting rules.
167 * Default: false
168 * Note: Requires at least version 2.4.0.
169 */
170 rewriteRules?: boolean | RewriteRules[] | undefined;
171 /**
172 * Tunnel the Browsersync server through a random Public URL
173 * Default: null
174 */
175 tunnel?: string | boolean | undefined;
176 /**
177 * Some features of Browsersync (such as xip & tunnel) require an internet connection, but if you're
178 * working offline, you can reduce start-up time by setting this option to false
179 */
180 online?: boolean | undefined;
181 /**
182 * Default: true
183 * Decide which URL to open automatically when Browsersync starts. Defaults to "local" if none set.
184 * Can be true, local, external, ui, ui-external, tunnel or false
185 */
186 open?: OpenOptions | boolean | undefined;
187 /**
188 * The browser(s) to open
189 * Default: default
190 */
191 browser?: string | string[] | undefined;
192 /**
193 * Add HTTP access control (CORS) headers to assets served by Browsersync.
194 * Default: false
195 * Note: Requires at least version 2.16.0.
196 */
197 cors?: boolean | undefined;
198 /**
199 * Requires an internet connection - useful for services such as Typekit as it allows you to configure
200 * domains such as *.xip.io in your kit settings
201 * Default: false
202 */
203 xip?: boolean | undefined;
204 /**
205 * Reload each browser when Browsersync is restarted.
206 * Default: false
207 */
208 reloadOnRestart?: boolean | undefined;
209 /**
210 * The small pop-over notifications in the browser are not always needed/wanted.
211 * Default: true
212 */
213 notify?: boolean | undefined;
214 /**
215 * scrollProportionally: false // Sync viewports to TOP position
216 * Default: true
217 */
218 scrollProportionally?: boolean | undefined;
219 /**
220 * How often to send scroll events
221 * Default: 0
222 */
223 scrollThrottle?: number | undefined;
224 /**
225 * Decide which technique should be used to restore scroll position following a reload.
226 * Can be window.name or cookie
227 * Default: 'window.name'
228 */
229 scrollRestoreTechnique?: string | undefined;
230 /**
231 * Sync the scroll position of any element on the page. Add any amount of CSS selectors
232 * Default: []
233 * Note: Requires at least version 2.9.0.
234 */
235 scrollElements?: string[] | undefined;
236 /**
237 * Default: []
238 * Note: Requires at least version 2.9.0.
239 * Sync the scroll position of any element on the page - where any scrolled element will cause
240 * all others to match scroll position. This is helpful when a breakpoint alters which element
241 * is actually scrolling
242 */
243 scrollElementMapping?: string[] | undefined;
244 /**
245 * Time, in milliseconds, to wait before instructing the browser to reload/inject following a file
246 * change event
247 * Default: 0
248 */
249 reloadDelay?: number | undefined;
250 /**
251 * Restrict the frequency in which browser:reload events can be emitted to connected clients
252 * Default: 0
253 * Note: Requires at least version 2.6.0.
254 */
255 reloadDebounce?: number | undefined;
256 /**
257 * Emit only the first event during sequential time windows of a specified duration.
258 * Note: Requires at least version 2.13.0.
259 */
260 reloadThrottle?: number | undefined;
261 /**
262 * User provided plugins
263 * Default: []
264 * Note: Requires at least version 2.6.0.
265 */
266 plugins?: any[] | undefined;
267 /**
268 * Whether to inject changes (rather than a page refresh)
269 * Default: true
270 */
271 injectChanges?: boolean | undefined;
272 /**
273 * The initial path to load
274 */
275 startPath?: string | undefined;
276 /**
277 * Whether to minify the client script
278 * Default: true
279 */
280 minify?: boolean | undefined;
281 /**
282 * Override host detection if you know the correct IP to use
283 */
284 host?: string | undefined;
285 /**
286 * Specify a host to listen on. Use this if you want to prevent binding to all interfaces.
287 *
288 * Note: When you specify this option, it overrides the 'host' option
289 */
290 listen?: string;
291 /**
292 * Support environments where dynamic hostnames are not required (ie: electron).
293 */
294 localOnly?: boolean | undefined;
295 /**
296 * Send file-change events to the browser
297 * Default: true
298 */
299 codeSync?: boolean | undefined;
300 /**
301 * Append timestamps to injected files
302 * Default: true
303 */
304 timestamps?: boolean | undefined;
305 /**
306 * ¯\_(ツ)_/¯
307 * Best guess, when ghostMode (or SocketIO?) is setup the events
308 * listed here will be emitted and able to hook into.
309 */
310 clientEvents?: string[] | undefined;
311 /**
312 * Alter the script path for complete control over where the Browsersync Javascript is served
313 * from. Whatever you return from this function will be used as the script path.
314 * Note: Requires at least version 1.5.0.
315 */
316 scriptPath?: ((path: string) => string) | undefined;
317 /**
318 * Configure the Socket.IO path and namespace & domain to avoid collisions.
319 * path - Default: "/browser-sync/socket.io"
320 * clientPath - Default: "/browser-sync"
321 * namespace - Default: "/browser-sync"
322 * domain - Default: undefined
323 * port - Default: undefined
324 * clients.heartbeatTimeout - Default: 5000
325 * Note: Requires at least version 1.6.2.
326 */
327 socket?: SocketOptions | undefined;
328 /**
329 * Configure the script domain
330 */
331 script?: ScriptOptions | undefined;
332 /**
333 * ¯\_()_/¯
334 */
335 tagNames?: TagNamesOptions | undefined;
336 /**
337 * ¯\_()_/¯
338 */
339 injectFileTypes?: string[] | undefined;
340 /**
341 * ¯\_()_/¯
342 */
343 excludeFileTypes?: string[] | undefined;
344 }
345
346 type WatchEvents = "add" | "change" | "unlink" | "addDir" | "unlinkDir";
347
348 type LogLevel = "info" | "debug" | "warn" | "silent";
349
350 type OpenOptions = "local" | "external" | "ui" | "ui-external" | "tunnel";
351
352 interface Hash<T> {
353 [path: string]: T;
354 }
355
356 interface UIOptions {
357 /** set the default port */
358 port?: number | undefined;
359 /** set the default weinre port */
360 weinre?: {
361 port?: number | undefined;
362 } | undefined;
363 }
364
365 interface FileCallback {
366 match?: string | string[] | undefined;
367 fn: (event: string, file: string) => any;
368 options?: chokidar.WatchOptions | undefined;
369 }
370
371 interface ServerOptions {
372 /** set base directory */
373 baseDir?: string | string[] | undefined;
374 /** enable directory listing */
375 directory?: boolean | undefined;
376 /** set index filename */
377 index?: string | undefined;
378 /**
379 * key-value object hash, where the key is the url to match,
380 * and the value is the folder to serve (relative to your working directory)
381 */
382 routes?: Hash<string> | undefined;
383 /** configure custom middleware */
384 middleware?: (MiddlewareHandler | PerRouteMiddleware)[] | undefined;
385 serveStaticOptions?: ServeStaticOptions | undefined;
386 }
387
388 interface ProxyOptions {
389 target?: string | undefined;
390 middleware?: MiddlewareHandler | undefined;
391 ws?: boolean | undefined;
392 reqHeaders?: ((config: object) => Hash<object>) | undefined;
393 proxyRes?: ProxyResponseMiddleware | ProxyResponseMiddleware[] | undefined;
394 proxyReq?: ((res: http.IncomingMessage) => void)[] | ((res: http.IncomingMessage) => void) | undefined;
395 error?: ((err: NodeJS.ErrnoException, req: http.IncomingMessage, res: http.ServerResponse) => void) | undefined;
396 }
397
398 interface ProxyResponseMiddleware {
399 (proxyRes: http.ServerResponse | http.IncomingMessage, res: http.ServerResponse, req: http.IncomingMessage): void;
400 }
401
402 interface HttpsOptions {
403 key?: string | undefined;
404 cert?: string | undefined;
405 }
406
407 interface StaticOptions {
408 route: string | string[],
409 dir: string | string[]
410 }
411
412 interface MiddlewareHandler {
413 (req: http.IncomingMessage, res: http.ServerResponse, next: () => void): any;
414 }
415
416 interface PerRouteMiddleware {
417 id?: string | undefined;
418 route: string;
419 handle: MiddlewareHandler;
420 }
421
422 interface CallbacksOptions {
423 ready: (err: Error, bs: BrowserSyncInstance) => void;
424 }
425
426 interface GhostOptions {
427 clicks?: boolean | undefined;
428 scroll?: boolean | undefined;
429 forms?: FormsOptions | boolean | undefined;
430 }
431
432 interface FormsOptions {
433 inputs: boolean,
434 submit: boolean,
435 toggles: boolean
436 }
437
438 interface SnippetOptions {
439 async?: boolean | undefined;
440 whitelist?: string[] | undefined,
441 blacklist?: string[] | undefined,
442 rule?: {
443 match?: RegExp | undefined;
444 fn?: ((snippet: string, match: string) => any) | undefined
445 } | undefined;
446 }
447
448 interface SocketOptions {
449 path?: string | undefined;
450 clientPath?: string | undefined;
451 namespace?: string | undefined;
452 domain?: string | undefined;
453 port?: number | undefined;
454 clients?: { heartbeatTimeout?: number | undefined; } | undefined;
455 }
456
457 interface ScriptOptions {
458 domain?: string | undefined;
459 }
460
461 interface TagNamesOptions {
462 less?: string | undefined;
463 scss?: string | undefined;
464 css?: string | undefined;
465 jpg?: string | undefined;
466 jpeg?: string | undefined;
467 png?: string | undefined;
468 svg?: string | undefined;
469 gif?: string | undefined;
470 js?: string | undefined;
471 }
472
473 interface RewriteRules {
474 match: RegExp;
475 replace?: string | undefined;
476 fn?: ((req: http.IncomingMessage, res: http.ServerResponse, match: string) => string) | undefined;
477 }
478
479 interface StreamOptions {
480 once?: boolean | undefined;
481 match?: mm.Pattern | mm.Pattern[] | undefined;
482 }
483
484 interface BrowserSyncStatic extends BrowserSyncInstance {
485 /**
486 * Start the Browsersync service. This will launch a server, proxy or start the snippet mode
487 * depending on your use-case.
488 */
489 (config?: Options, callback?: (err: Error, bs: BrowserSyncInstance) => any): BrowserSyncInstance;
490 /**
491 *
492 */
493 instances: Array<BrowserSyncInstance>;
494 /**
495 * Create a Browsersync instance
496 * @param name an identifier that can used for retrieval later
497 */
498 create(name?: string, emitter?: NodeJS.EventEmitter): BrowserSyncInstance;
499 /**
500 * Get a single instance by name. This is useful if you have your build scripts in separate files
501 * @param name the identifier used for retrieval
502 */
503 get(name: string): BrowserSyncInstance;
504 /**
505 * Check if an instance has been created.
506 * @param name the name of the instance
507 */
508 has(name: string): boolean;
509 /**
510 * Reset the state of the module.
511 * (should only be needed for test environments)
512 */
513 reset(): void;
514 }
515
516 interface BrowserSyncInstance {
517 /** the name of this instance of browser-sync */
518 name: string;
519 /**
520 * Start the Browsersync service. This will launch a server, proxy or start the snippet mode
521 * depending on your use-case.
522 */
523 init(config?: Options, callback?: (err: Error, bs: BrowserSyncInstance) => any): BrowserSyncInstance;
524 /**
525 * This method will close any running server, stop file watching & exit the current process.
526 */
527 exit(): void;
528 /**
529 * Helper method for browser notifications
530 * @param message Can be a simple message such as 'Connected' or HTML
531 * @param timeout How long the message will remain in the browser. @since 1.3.0
532 */
533 notify(message: string, timeout?: number): void;
534 /**
535 * Method to pause file change events
536 */
537 pause(): void;
538 /**
539 * Method to resume paused watchers
540 */
541 resume(): void;
542 /**
543 * Reload the browser
544 * The reload method will inform all browsers about changed files and will either cause the browser
545 * to refresh, or inject the files where possible.
546 */
547 reload(): void;
548 /**
549 * Reload a single file
550 * The reload method will inform all browsers about changed files and will either cause the browser
551 * to refresh, or inject the files where possible.
552 */
553 reload(file: string): void;
554 /**
555 * Reload multiple files
556 * The reload method will inform all browsers about changed files and will either cause the browser
557 * to refresh, or inject the files where possible.
558 */
559 reload(files: string[]): void;
560 /**
561 * The reload method will inform all browsers about changed files and will either cause the browser
562 * to refresh, or inject the files where possible.
563 */
564 reload(options: { stream: boolean }): NodeJS.ReadWriteStream;
565 /**
566 * The stream method returns a transform stream and can act once or on many files.
567 * @param opts Configuration for the stream method
568 */
569 stream(opts?: StreamOptions): NodeJS.ReadWriteStream;
570 /**
571 * Instance Cleanup.
572 */
573 cleanup(fn?: (error: NodeJS.ErrnoException, bs: BrowserSyncInstance) => void): void;
574 /**
575 * Register a plugin.
576 * Must implement at least a 'plugin' property that returns
577 * callable function.
578 *
579 * @method use
580 * @param {object} module The object to be `required`.
581 * @param {object} options The
582 * @param {any} cb A callback function that will return any errors.
583 */
584 use(module: { "plugin:name"?: string | undefined, plugin: (opts: object, bs: BrowserSyncInstance) => any }, options?: object, cb?: any): void;
585 /**
586 * Callback helper to examine what options have been set.
587 * @param {string} name The key to search options map for.
588 */
589 getOption(name: string): any;
590 /**
591 * Stand alone file-watcher. Use this along with Browsersync to create your own, minimal build system
592 */
593 watch(patterns: string, opts?: chokidar.WatchOptions, fn?: (event: string, file: fs.Stats) => any)
594 : NodeJS.EventEmitter;
595 /**
596 * The internal Event Emitter used by the running Browsersync instance (if there is one). You can use
597 * this to emit your own events, such as changed files, logging etc.
598 */
599 emitter: NodeJS.EventEmitter;
600 /**
601 * A simple true/false flag that you can use to determine if there's a currently-running Browsersync instance.
602 */
603 active: boolean;
604 /**
605 * A simple true/false flag to determine if the current instance is paused
606 */
607 paused: boolean;
608 }
609}
610
611declare const browserSync: browserSync.BrowserSyncStatic;
612export = browserSync;
613
\No newline at end of file