UNPKG

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