@@ -4,44 +4,91 @@ var _ = require("../lodash.custom");
44var fs = require ( "fs" ) ;
55var config = require ( "./config" ) ;
66
7+ function getPath ( options , relative , port ) {
8+ if ( options . get ( "mode" ) === "snippet" ) {
9+ return options . get ( "scheme" ) + "://HOST:" + port + relative ;
10+ } else {
11+ return "//HOST:" + port + relative ;
12+ }
13+ }
14+
715var connectUtils = {
816 /**
917 * @param {Immutable.Map } options
1018 * @returns {String }
1119 */
1220 scriptTags : function ( options ) {
1321
14- function getPath ( relative , port ) {
15- if ( options . get ( "mode" ) === "snippet" ) {
16- return options . get ( "scheme" ) + "://HOST:" + port + relative ;
17- } else {
18- return "//HOST:" + port + relative ;
22+ var scriptPath = this . clientScript ( options ) ;
23+ var async = options . getIn ( [ "snippetOptions" , "async" ] ) ;
24+ var scriptDomain = options . getIn ( [ "script" , "domain" ] ) ;
25+
26+ /**
27+ * Generate the [src] attribute based on user options
28+ */
29+ var scriptSrc = ( function ( ) {
30+
31+ /**
32+ * First, was 'scriptPath' set? if so the user wanted full control over the
33+ * script tag output
34+ *
35+ */
36+ if ( _ . isFunction ( options . get ( "scriptPath" ) ) ) {
37+ return options . get ( "scriptPath" ) . apply ( null , getScriptArgs ( options , scriptPath ) ) ;
1938 }
20- }
2139
22- var template = fs . readFileSync ( config . templates . scriptTag , "utf-8" ) ;
23- var scriptPath = this . clientScript ( options ) ;
24- var async = options . getIn ( [ "snippetOptions" , "async" ] ) ;
25- var script ;
26- var override = false ;
27-
28- if ( _ . isFunction ( options . get ( "scriptPath" ) ) ) {
29- var args = getScriptArgs ( options , scriptPath ) ;
30- script = options . get ( "scriptPath" ) . apply ( null , args ) ;
31- override = true ;
32- } else {
33- script = getPath ( scriptPath , options . get ( "port" ) ) ;
34- }
40+ /**
41+ * Next, if 'script.domain' was given, allow that + the path to the JS file
42+ * eg:
43+ * script.domain=localhost:3000
44+ * -> localhost:3000/browser-sync/browser-sync-client.js
45+ */
46+ if ( scriptDomain ) {
47+ if ( _ . isFunction ( scriptDomain ) ) {
48+ return scriptDomain . call ( null , options ) + scriptPath ;
49+ }
50+ if ( scriptDomain . match ( / \{ p o r t \} / ) ) {
51+ return scriptDomain . replace ( "{port}" , options . get ( "port" ) ) + scriptPath ;
52+ }
53+ return scriptDomain + scriptPath ;
54+ }
3555
36- if ( ! override && ( options . get ( "server" ) || options . get ( "proxy" ) ) ) {
37- script = scriptPath ;
38- }
56+ /**
57+ * Now if server or proxy, use dynamic script
58+ * eg:
59+ * browser-sync start --server
60+ * ->
61+ * "HOST:3000/browser-sync/browser-sync-client.js".replace("HOST", location.hostname)
62+ */
63+ if ( options . get ( "server" ) || options . get ( "proxy" ) ) {
64+ return scriptPath ;
65+ }
3966
40- template = template
41- . replace ( "%script%" , script )
42- . replace ( "%async%" , async ? "async" : "" ) ;
67+ /**
68+ * Final use case is snippet mode
69+ * -> "http://HOST:3000/browser-sync/browser-sync-client.js".replace("HOST", location.hostname)
70+ * -> "//HOST:3000/browser-sync/browser-sync-client.js".replace("HOST", location.hostname)"
71+ */
72+ return getPath ( options , scriptPath , options . get ( "port" ) ) ;
73+ } ) ( ) ;
4374
44- return template ;
75+ /**
76+ * Decide which template shall be used to generate the script tags
77+ */
78+ var template = ( function ( ) {
79+ if ( scriptDomain ) {
80+ return config . templates . scriptTagSimple ;
81+ }
82+ return config . templates . scriptTag ;
83+ } ) ( ) ;
84+
85+ /**
86+ * Finally read the template file from disk and replace
87+ * the dynamic values.
88+ */
89+ return fs . readFileSync ( template , "utf8" )
90+ . replace ( "%script%" , scriptSrc )
91+ . replace ( "%async%" , async ? "async" : "" ) ;
4592 } ,
4693 /**
4794 * @param {Map } options
@@ -116,17 +163,32 @@ var connectUtils = {
116163 port = options . getIn ( [ "socket" , "port" ] ) ;
117164 }
118165
119- if ( socketOpts . domain ) {
120- string = withDomain ;
121- if ( typeof socketOpts . domain === "function" ) {
122- socketOpts . domain = socketOpts . domain . call ( null , options ) ;
166+ /**
167+ * Ensure socket.domain is always a string (for noop replacements later)
168+ */
169+ socketOpts . domain = ( function ( ) {
170+ if ( socketOpts . domain ) {
171+ string = withDomain ;
172+ /**
173+ * User provided a function
174+ */
175+ if ( _ . isFunction ( socketOpts . domain ) ) {
176+ return socketOpts . domain . call ( null , options ) ;
177+ }
178+ /**
179+ * User provided a string
180+ */
181+ if ( _ . isString ( socketOpts . domain ) ) {
182+ return socketOpts . domain ;
183+ }
123184 }
124- }
185+ return "" ;
186+ } ) ( ) ;
125187
126188 return string
127189 . replace ( "{protocol}" , protocol )
128190 . replace ( "{port}" , port )
129- . replace ( "{domain}" , socketOpts . domain )
191+ . replace ( "{domain}" , socketOpts . domain . replace ( "{port}" , port ) )
130192 . replace ( "{ns}" , namespace ) ;
131193 } ,
132194 /**
0 commit comments