Skip to content

Commit dac3e20

Browse files
authored
Make :preloads and :closure-defines work (#185)
* using reagent 0.8.0-alpha1 * require react-native in namespace declaration * bump versions * make preloads and closure defines work * make preloads and closure defines work * revert changes in core.cljs * do not preload cljs-devtools it causes a warning on startup * add empty line * update README * code cleanup * bump om-next version
1 parent 845f4cd commit dac3e20

File tree

7 files changed

+76
-66
lines changed

7 files changed

+76
-66
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ For more ClojureScript React Native resources visit [cljsrn.org](http://cljsrn.o
1919
Contributions are very welcome.
2020

2121
## Status
22-
- Uses [React Native] v0.53.0
22+
- Uses [React Native] v0.55.4
2323
- Reusable codebase between iOS and Android
2424
- Figwheel used for REPL and live coding
2525
- Works in iOS (real device and simulator)

re-natal.coffee

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ ipAddressRx = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/i
3434
debugHostRx = /host]\s+\?:\s+@".*";/g
3535
namespaceRx = /\(ns\s+([A-Za-z0-9.-]+)/g
3636
jsRequireRx = /js\/require "(.+)"/g
37-
rnVersion = '0.53.0'
38-
rnWinVersion = '0.53.0-rc.1'
37+
rnVersion = '0.55.4'
38+
rnWinVersion = '0.55.0-rc.0'
3939
rnPackagerPort = 8081
4040
process.title = 're-natal'
4141
buildProfiles =
@@ -64,17 +64,17 @@ interfaceConf =
6464
sources:
6565
common: ["events.cljs", "subs.cljs", "db.cljs"]
6666
other: [["reagent_dom.cljs","reagent/dom.cljs"], ["reagent_dom_server.cljs","reagent/dom/server.cljs"]]
67-
deps: ['[reagent "0.7.0" :exclusions [cljsjs/react cljsjs/react-dom cljsjs/react-dom-server cljsjs/create-react-class]]'
68-
'[re-frame "0.10.4"]']
69-
shims: ["cljsjs.react", "cljsjs.react.dom", "cljsjs.react.dom.server", "cljsjs.create-react-class"]
67+
deps: ['[reagent "0.8.1" :exclusions [cljsjs/react cljsjs/react-dom cljsjs/react-dom-server cljsjs/create-react-class]]'
68+
'[re-frame "0.10.5"]']
69+
shims: []
7070
sampleCommandNs: '(in-ns \'$PROJECT_NAME_HYPHENATED$.ios.core)'
7171
sampleCommand: '(dispatch [:set-greeting "Hello Native World!"])'
7272
'om-next':
7373
cljsDir: "cljs-om-next"
7474
sources:
7575
common: ["state.cljs"]
7676
other: [["support.cljs","re_natal/support.cljs"]]
77-
deps: ['[org.omcljs/om "1.0.0-beta1" :exclusions [cljsjs/react cljsjs/react-dom]]']
77+
deps: ['[org.omcljs/om "1.0.0-beta3" :exclusions [cljsjs/react cljsjs/react-dom]]']
7878
shims: ["cljsjs.react", "cljsjs.react.dom"]
7979
sampleCommandNs: '(in-ns \'$PROJECT_NAME_HYPHENATED$.state)'
8080
sampleCommand: '(swap! app-state assoc :app/msg "Hello Native World!")'
@@ -131,10 +131,6 @@ ensureExecutableAvailable = (executable) ->
131131
exec "type #{executable}"
132132

133133
isYarnAvailable = () ->
134-
try
135-
ensureExecutableAvailable('yarn')
136-
true
137-
catch e
138134
false
139135

140136
isSomeDepsMissing = () ->

resources/advanced.profile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
:static-fns true
77
:optimize-constants true
88
:optimizations :advanced
9+
:target :nodejs
910
:closure-defines {"goog.DEBUG" false}}}

resources/dev.profile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{:id "$PLATFORM$"
22
:source-paths ["src" "env/dev"]
33
:figwheel true
4-
:compiler {:output-to "target/$PLATFORM$/not-used.js"
4+
:compiler {:output-to "target/$PLATFORM$/index.js"
55
:main "env.$PLATFORM$.main"
66
:output-dir "target/$PLATFORM$"
7-
:optimizations :none}}
7+
:optimizations :none
8+
:target :nodejs}}

resources/figwheel-bridge.js

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* @providesModule figwheel-bridge
55
*/
66

7-
var CLOSURE_UNCOMPILED_DEFINES = null;
87
var debugEnabled = false;
98

109
var 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
*/
5453
function figwheelMessageHandler(msg) {
5554
if(msg["msg-name"] == "compile-failed") {
56-
console.warn(formatCompileError(msg));
55+
console.warn(formatCompileError(msg));
5756
}
5857
}
5958

6059
function 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

129128
function 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.
142141
function 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) {
175175
function 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(/goog.require\(.*?\);/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

189202
function 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

231242
function 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

resources/prod.profile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
:static-fns true
77
:optimize-constants true
88
:optimizations :simple
9+
:target :nodejs
910
:closure-defines {"goog.DEBUG" false}}}

resources/project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
:license {:name "Eclipse Public License"
55
:url "http://www.eclipse.org/legal/epl-v10.html"}
66
:dependencies [[org.clojure/clojure "1.9.0"]
7-
[org.clojure/clojurescript "1.9.946"]
7+
[org.clojure/clojurescript "1.10.238"]
88
$INTERFACE_DEPS$]
99
:plugins [[lein-cljsbuild "1.1.4"]
1010
[lein-figwheel "0.5.14"]]

0 commit comments

Comments
 (0)