Is there any way I can include an external JS library in my pebble code? Conventionally on a webpage I would do this in my head tags:
<script type='text/javascript' src='https://cdn.firebase.com/js/client/1.0.11/firebase.js'></script>
But in pebble, I am unable to do that since I am only using JS. So how can I include an external library for a JavaScript file.
I am not sure if there have been changes since the above answer, but it looks like there is in fact a way to include additional resources while keeping things tidy. On the pebbleJS page, there is the following section with an some information on the subject.
GLOBAL NAMESPACE - require(path)
Loads another JavaScript file allowing you to write a multi-file project. Package loading loosely follows the CommonJS format. path is the path to the dependency.
You can then use the following code to "require" a JS library in your pebble project. This should be usable on both Cloud Pebble as well as native development.
You can then use this as shown below:
Update: After some digging into this for my own, I have successfully gotten CloudPebble to work with this functionality. It is a bit convoluted, but follows the ECMAScript 6 standards. Below I am posting a simple example of getting things set up. Additionally, I would suggest looking at this code from PebbleJS for a better reference of a complex setup.
myApp.js
myExtraFile.js
I think there's some confusion over Pebble.js vs PebbleKit JS (v3.8.1). Pebble.js is a fledgling SDK where eventually the programmer will be able to write pure JavaScript. It's still cooking so there's some functionality missing like the ability to draw a line or obtain the screen dimensions. The repo is a mix of C and JS sources where you can add C code to augment missing functionality but otherwise all your code lives in
src/js/app.js
orsrc/js/app/
. Anyway, Pebble.js does supportrequire
.I don't use CloudPebble but I got the impression that it either supports Pebble.js (and hence
require
) or is planning to. I think all this SDK boilerplate code would be hidden.PebbleKit JS does not support
require
out of the box AFAIK. I've made a demo that portsrequire
support from Pebble.js to PKJS. The summary of changes is:src/js/pebble-js-app.js
tosrc/js/app/index.js
.src/js/app/index.js
.index.js
will be loaded when the ready event is emitted.src/js/loader.js
from Pebble.js.src/js/main.js
that callsrequire('src/js/app')
on theready
event.wscript
with the following deltas.src/js/app/
andrequire('./name')
will work.I've tried to cover this all in the demo's readme.
BTW, here's the official breakdown of all the different SDKs but it's a little confusing.
At present, you cannot include external JS files.
If you're using CloudPebble, then the only way to do this is to copy and paste the content of the JS library files into your JS file.
If you're doing native development, you can modify the wscript file to combine multiple JS files into one at build time.