UNPKG

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