44 * @providesModule figwheel-bridge
55 */
66
7- var CLOSURE_UNCOMPILED_DEFINES = null ;
87var debugEnabled = false ;
98
109var config = {
@@ -33,34 +32,34 @@ function formatCompileError(msg) {
3332 var errorStr = "Figwheel Compile Exception: "
3433 var data = msg [ 'exception-data' ] ;
3534 if ( data [ 'message' ] ) {
36- errorStr += data [ 'message' ] + " " ;
35+ errorStr += data [ 'message' ] + " " ;
3736 }
3837 if ( data [ 'file' ] ) {
39- errorStr += "in file " + data [ 'file' ] + " " ;
38+ errorStr += "in file " + data [ 'file' ] + " " ;
4039 }
4140 if ( data [ 'line' ] ) {
42- errorStr += "at line " + data [ 'line' ] ;
41+ errorStr += "at line " + data [ 'line' ] ;
4342 }
4443 if ( data [ 'column' ] ) {
45- errorStr += ", column " + data [ 'column' ] ;
44+ errorStr += ", column " + data [ 'column' ] ;
4645 }
4746 return errorStr ;
4847}
4948
50- /* This is simply demonstrating that we can receive and react to
49+ /* This is simply demonstrating that we can receive and react to
5150 * arbitrary messages from Figwheel this will enable creating a nicer
5251 * feedback system in the Figwheel top level React component.
5352 */
5453function figwheelMessageHandler ( msg ) {
5554 if ( msg [ "msg-name" ] == "compile-failed" ) {
56- console . warn ( formatCompileError ( msg ) ) ;
55+ console . warn ( formatCompileError ( msg ) ) ;
5756 }
5857}
5958
6059function listenToFigwheelMessages ( ) {
6160 if ( figwheel . client . add_json_message_watch ) {
62- figwheel . client . add_json_message_watch ( "ReactNativeMessageIntercept" ,
63- figwheelMessageHandler ) ;
61+ figwheel . client . add_json_message_watch ( "ReactNativeMessageIntercept" ,
62+ figwheelMessageHandler ) ;
6463 }
6564}
6665
@@ -86,7 +85,7 @@ var figwheelApp = function (platform, devHost) {
8685 if ( typeof goog === "undefined" ) {
8786 loadApp ( platform , devHost , function ( appRoot ) {
8887 app . setState ( { root : appRoot , loaded : true } ) ;
89- listenToFigwheelMessages ( ) ;
88+ listenToFigwheelMessages ( ) ;
9089 } ) ;
9190 }
9291 }
@@ -103,34 +102,34 @@ var isChrome = function () {
103102 return typeof importScripts === "function"
104103} ;
105104
106- function asyncImportScripts ( url , success , error ) {
105+ function asyncImportScripts ( url , transform , success , error ) {
107106 logDebug ( '(asyncImportScripts) Importing: ' + url ) ;
108107 asyncImportChain =
109- asyncImportChain
110- . then ( function ( v ) { return fetch ( url ) ; } )
111- . then ( function ( response ) {
112- if ( response . ok )
113- return response . text ( ) ;
114- throw new Error ( "Failed to Fetch: " + url + " - Perhaps your project was cleaned and you haven't recompiled?" )
115- } )
116- . then ( function ( responseText ) {
117- evaluate ( responseText ) ;
118- fireEvalListenters ( url ) ;
119- success ( ) ;
120- return true ;
121- } )
122- . catch ( function ( e ) {
123- console . error ( e ) ;
124- error ( ) ;
125- return true ;
126- } ) ;
108+ asyncImportChain
109+ . then ( function ( v ) { return fetch ( url ) ; } )
110+ . then ( function ( response ) {
111+ if ( response . ok )
112+ return response . text ( ) ;
113+ throw new Error ( "Failed to Fetch: " + url + " - Perhaps your project was cleaned and you haven't recompiled?" )
114+ } )
115+ . then ( function ( responseText ) {
116+ evaluate ( transform ( responseText ) ) ;
117+ fireEvalListenters ( url ) ;
118+ success ( ) ;
119+ return true ;
120+ } )
121+ . catch ( function ( e ) {
122+ console . error ( e ) ;
123+ error ( ) ;
124+ return true ;
125+ } ) ;
127126}
128127
129128function syncImportScripts ( url , success , error ) {
130129 try {
131130 importScripts ( url ) ;
132131 logDebug ( 'Evaluated: ' + url ) ;
133- fireEvalListenters ( url ) ;
132+ fireEvalListenters ( url ) ;
134133 success ( ) ;
135134 } catch ( e ) {
136135 console . error ( e ) ;
@@ -141,13 +140,14 @@ function syncImportScripts(url, success, error) {
141140// Loads js file sync if possible or async.
142141function importJs ( src , success , error ) {
143142 var noop = function ( ) { } ;
144- success = ( typeof success == 'function' ) ? success : noop ;
145- error = ( typeof error == 'function' ) ? error : noop ;
143+ var identity = function ( arg ) { return arg } ;
144+ var successCb = ( typeof success == 'function' ) ? success : noop ;
145+ var errorCb = ( typeof error == 'function' ) ? error : noop ;
146146 logDebug ( '(importJs) Importing: ' + src ) ;
147147 if ( isChrome ( ) ) {
148- syncImportScripts ( src , success , error ) ;
148+ syncImportScripts ( src , successCb , errorCb ) ;
149149 } else {
150- asyncImportScripts ( src , success , error ) ;
150+ asyncImportScripts ( src , identity , successCb , errorCb ) ;
151151 }
152152}
153153
@@ -175,16 +175,29 @@ function isUnDefined(x) {
175175function assertRootElExists ( platform ) {
176176 var basicMessage = "ClojureScript project didn't compile, or didn't load correctly." ;
177177 if ( isUnDefined ( env ) ) {
178- throw new Error ( "Critical Error: env namespace not defined - " + basicMessage ) ;
178+ throw new Error ( "Critical Error: env namespace not defined - " + basicMessage ) ;
179179 } else if ( isUnDefined ( env [ platform ] ) ) {
180- throw new Error ( "Critical Error: env." + platform + " namespace not defined - " + basicMessage ) ;
180+ throw new Error ( "Critical Error: env." + platform + " namespace not defined - " + basicMessage ) ;
181181 } else if ( isUnDefined ( env [ platform ] . main ) ) {
182- throw new Error ( "Critical Error: env." + platform + ".main namespace not defined - " + basicMessage ) ;
182+ throw new Error ( "Critical Error: env." + platform + ".main namespace not defined - " + basicMessage ) ;
183183 } else if ( isUnDefined ( env [ platform ] . main . root_el ) ) {
184- throw new Error ( "Critical Error: env." +
185- platform + ".main namespace doesn't define a root-el which should hold the root react node of your app." ) ;
184+ throw new Error ( "Critical Error: env." +
185+ platform + ".main namespace doesn't define a root-el which should hold the root react node of your app." ) ;
186186 }
187- }
187+ }
188+
189+ function importIndexJs ( fileBasePath ) {
190+ var src = fileBasePath + '/index.js' ;
191+ var transformFn = function ( code ) {
192+ var defines = code . match ( new RegExp ( "goog.global.CLOSURE_UNCOMPILED_DEFINES.*?;" ) ) ;
193+ var deps = code . match ( / g o o g .r e q u i r e \( .* ?\) ; / g) ;
194+ var transformedCode = defines . concat ( deps ) . join ( '' ) ;
195+ logDebug ( 'transformed index.js: ' , transformedCode ) ;
196+ return transformedCode ;
197+ } ;
198+ logDebug ( '(importIndexJs) Importing: ' + src ) ;
199+ asyncImportScripts ( src , transformFn , function ( ) { } , function ( ) { } ) ;
200+ }
188201
189202function loadApp ( platform , devHost , onLoadCb ) {
190203 var fileBasePath = serverBaseUrl ( ( isChrome ( ) ? "localhost" : devHost ) ) + "/" + config . basePath + platform ;
@@ -193,26 +206,24 @@ function loadApp(platform, devHost, onLoadCb) {
193206 var mainJs = `/env/${ platform } /main.js` ;
194207 evalListeners . waitForFinalEval = function ( url ) {
195208 if ( url . indexOf ( mainJs ) > - 1 ) {
196- assertRootElExists ( platform ) ;
209+ assertRootElExists ( platform ) ;
197210 onLoadCb ( env [ platform ] . main . root_el ) ;
198211 console . info ( 'Done loading Clojure app' ) ;
199- delete evalListeners . waitForFinalEval ;
212+ delete evalListeners . waitForFinalEval ;
200213 }
201214 } ;
202215
203216 if ( typeof goog === "undefined" ) {
204217 console . info ( 'Loading Closure base.' ) ;
205218 interceptRequire ( ) ;
206- // need to know base path here
219+
220+ // need to know base path here
207221 importJs ( fileBasePath + '/goog/base.js' , function ( ) {
208222 shimBaseGoog ( fileBasePath ) ;
209- importJs ( fileBasePath + '/cljs_deps.js' ) ;
210- importJs ( fileBasePath + '/goog/deps.js' , function ( ) {
211- // This is needed because of RN packager
212- // seriously React packager? why.
213- var googreq = goog . require ;
214-
215- googreq ( `env.${ platform } .main` ) ;
223+ importJs ( fileBasePath + '/cljs_deps.js' , function ( ) {
224+ importJs ( fileBasePath + '/goog/deps.js' , function ( ) {
225+ importIndexJs ( fileBasePath ) ;
226+ } ) ;
216227 } ) ;
217228 } ) ;
218229 }
@@ -230,8 +241,8 @@ function withModules(moduleById) {
230241
231242function figwheelImportScript ( uri , callback ) {
232243 importJs ( uri . toString ( ) ,
233- function ( ) { callback ( true ) ; } ,
234- function ( ) { callback ( false ) ; } )
244+ function ( ) { callback ( true ) ; } ,
245+ function ( ) { callback ( false ) ; } )
235246}
236247
237248// Goog fixes
0 commit comments