Hey folks! This is a fork of https://github.com/whydoidoit/babel-playcanvas-template made by the amazing whydoidoit. Unfortunately it seems that over the last 2 years some things broke, and since it seems he is not maintaining the repository anymore I decided to spend a few minutes to make the minimal necessary changes.
Note that the tools used (e.g. Babel, Webpack etc.) are also out of date, but they work and I don't have enough time at the moment to update them. Feel free to send a pull request if you do!
Apart from this note and the chapters "Deprecated Methods", "Setting up Redirector (Chrome only)" and a small change in "HTTPS serving", this document is completely written by whydoidoit.
Also shoutout to thisredone from whom I stole the Redirector instructions!
This is a template project for using ES6 via Babel and WebPack to build PlayCanvas projects.
PlayCanvas is a fantastic open source WebGL Engine and online Editor (which you can get access to for free or pay for an organisational license).
PlayCanvas have developed a shared model that means you can edit your 3D scenes as a collaborative experience with team mates around the office, or around the world - it's great. They have applied the same to code editing, which is fine for some use cases but imposes certain limitations:
- No offline access to source code
- You are stuck with their web editor - which is "ok" but no WebStorm, Sublime or VSCode
- No source control
- Someone else can change your file when you aren't looking and you'll never know who!
- No ES6 features, just pure Javascript
- No NPM ecosystem, meaning you are scrabbling for browserified versions of libraries or more often doing something again or just not bothering
All of this means that it is hard to choose PlayCanvas for serious development projects without going "Engine Only" and that loses you many of the advantages of having a fantastic online editor and layout tool. So now why choose PlayCanvas when Three.js would give you just as much if not more?
The answer has to be to produce code in a proper offline build environment with all the advantages of Babel, WebPack, NPM et al and still be able to use the output in the PlayCanvas online Editor. As no one had done this, and I needed it for a number of projects I took on the task myself. This has lead to a number of NPM repos and a WebPackPlugin that automate most of the process.
If you are asking why you should use ES6 and Babel then I'd say it's for one simple reason: a programming language should try to get the hell out of your way and let you express what you want.
When we code Javascript for WebGL we are coding for the browser and nearly everything that touches the outside world will be async. Expressing async in traditional Javascript is messy as hell. Try writing a for-next loop that loads a list of things from the web in sequence using Promises or callbacks and it will become immediately obvious. With Babel and ES6 it's just a loop. Everything else is a christmas tree. Yes it's possible, but it's easy to have a hard to spot bug, so you do LESS of it than you would otherwise and refactoring is a scary prospect. That's not right. That's damaging your creativity to my mind.
function requestFromUrl(url, info) { return new Promise(/* code */)}
async function getData() {
let urls = ["https://blah1.org", "https://blah1.org/blah", "https://blah2.org/blah/blah"];
let data = "";
for(let i = 0; i < urls.length; i++) {
data = await requestFromUrl(urls[i], data);
}
return data;
}
I know this is a contrived example, but this "kind of thing" happens all of the time in my developments, and they are better for me being able to implement them easily. Write that as just promises or callbacks and it will be illegible to most developers without a lot of study.
ES6/ES7 etc exist to create a better programming language for the web, I say let's use it.
That's what Babel is for, if the browser doesn't support something, it provides that support for you. Plus it compiles your ES6 to ES5.
In addition this template project lets you specify which versions of browsers you are supporting and Babel only does what it has to. You could even simply build different versions for different browsers (or ages of browser etc) if you prefer!
If you need some standard function, algorithm or procedure there's a good chance that there is tested code out there to install. with one line of shell script.
WebPack is going to make building all of this and serving it to your browser an automated process.
The shortest way to get started is really simple.
You must have a version of Node and NPM installed.
You can get that from here.
EITHER
Download this repo, change to the directory and type:
npm install
OR
Make a directory and change to it
npm install babel-playcanvas-template
Now in [YOUR_DIRECTORY]/node_modules/babel-playcanvas-template
are all of the files you need. Either
develop in there or copy that whole directory structure somewhere you want to develop.
The entry point - which is where you will import your own code - is in src/main.js
In the template this imports a bunch of PlayCanvas extensions and then a single
example.js
script that uses a couple of ES6 features for a demo.
Create a file in src
or a sub directory and script what you like. Just make sure that it is imported
by main.js
(note that paths are relative to src
and must start with a ./
).
When you start developing things that import each other, you just need
to make sure that something in main.js
imports something that imports the code you
add!
If you find that something didn't show up, that's probably why.
Firstly we need to make a configuration file - there's an example called config.example.json
.
The config file is in the root of the project (the parent of src
) and needs
to be called config.json
. This will eventually also control the automatic upload
of your code to PlayCanvas, but to start with, just copy the example to config.json
.
You can build your code using either webpack
or an automated process with npm
.
So typing npm run build
in the root folder of the project (the parent of src
)
the template will build a production version of your code into the build
folder.
Either build your code with NPM
npm run build
Or build your production code with webpack
webpack --config webpack.production.config.js
The output file will be called main.build.js
. To use that in PlayCanvas just drag and drop
it onto the PlayCanvas editor for your project.
Now open your developer tools in the browser with the PlayCanvas Editor open and in the Javascript console type
config.accessToken
Copy the result of this and paste it into the config.json
file as your bearer token.
Then in the javascript console type
config.project.id
And put that in the project id part of config.json
Finally if you haven't already done it, drag main.build.js
and drop it in the PlayCanvas assets window.
When it's imported click on it and in the properties window on the right, take it's ID and put that in
config.json
as your assetId.
Now every time you run npm run build
it will upload the result to PlayCanvas for you.
There's a better way to do ongoing development though, you only really need to upload your build when the attributes of something change, you add a new script or you want to publish your build.
This template project has a solution for that too. You will be able to see all of your
source code in your developer tools when you use any means of making a development
build.
Previously two deprecated methods of locally serving a development build were described here: Loading Screen Method and PlayCanvas Script Method. As far as I can see, neither work anymore. (Possibly due to pc.Asset.prototype.getFileUrl
not being used anymore for loading scripts, but that is pure conjecture on my part.)
The following instructions are taken from this fork by thisredone. I've slightly modified them - the biggest change is that they should only apply to http URLs.
-
Download the Redirector chrome extension.
-
Setup two redirects:
- One for script files
Example URL: http://launch.playcanvas.com/api/assets/files/main.build.js?id=11217398&branchId=00465776-6b83-4f4c-af75-01c351769fa8
Include pattern: http://launch.playcanvas.com/api/assets/files/(.+.js)?(.*)
Redirect to: http://localhost:8081/$1?$2
Pattern type: Regular Expression
In advanced options:
Apply to: Scripts and XMLHttpRequests (Ajax) - One for HMR updates
Example URL: http://launch.playcanvas.com/191247d10e518c420782.hot-update.json
Include pattern: http://launch.playcanvas.com/(.*).hot-update.(js|json)$
Redirect to: http://localhost:8081/$1.hot-update.$2
Pattern type: Regular Expression
In advanced options:
Apply to: Scripts and XMLHttpRequests (Ajax)
- One for script files
After starting the server in the next step, you can open http://launch.playcanvas.com/%7BprojectId%7D?debug=true (so the same URL as you'd regularely do, but with http instead of https) and all script files will be used from your local server instead of the files uploaded on the PlayCanvas servers.
Note that while Redirector is active, all requests matching the above patterns are redirected to your local server, even if you are not running your local server and even for projects hosted at http://launch.playcanvas.com that you don't even want to run locally. Be sure to turn Redirector off or use https URLs in those cases.
Now you can type npm start
in the project root. This will, build and upload your code, then start a local server
to serve any changes you make. When you change your code, your launch window will automatically
update.
If you need to upload again, just stop the server with CTRL+C and type npm start
again. Then refresh
your launch and Editor windows.
Type webpack --config webpack.development.config.js
to build and (if configured) upload a development
version of your code which will have source mapping to make it possible to see your own code
when you debug.
Just type npm run build
any time you want a production build.
Production builds are minified and don't have source maps embedded (they are a separate file).
You can just use NPM like normal. Basically find the module you need and type
npm install --save <module-name>
You can then import it into the file you need it in by adding an import
statement at the
top of your file.
import blah from 'blah-module';
...
blah(something);
You may also use require
syntax if the whole file is written that way.
config.json
also contains a "browsers"
entry - this is a query in the
browserslist format that tells Babel
what it needs to augment in the target output. By default it's set to > 1%
which means that the output code will work on 99%
of browsers in the field.
If you set it to last 2 Chrome versions
then a lot more of ES6 is implemented
already and there will be less work done, so a smaller output file
(and possibly some code could be faster).
Using this method you could actually create multiple builds and choose between them.
Hopefully this will get you started using ES6 and modules with PlayCanvas. Feel free to ask for @whydoidoit on the PlayCanvas forum if you want to discuss.
Enjoy!
- Ends -
You can configure webpack to HTTPS serve instead of HTTP.
Use npm run https
to start your local development build. Then:
-
Either: in a separate window navigate to https://localhost:8081/main.build.js and if you are warned it isn't safe, just proceed anyway. This will mean that you always see that the launch page is untrusted and may cause other issues, it's normally fine for me.
-
Or: get your browser to trust
node_modules/webpack-dev-server/ssl/server.pem
. This can be easier said than done. You can also replaceserver.pem
with your own trustedlocalhost
certificate. Just you'll have to pack it as a.pem
file. (On Apple by default it will be a.p12
, Google for how to change it).
Don't forget to change your launch URL to HTTPS and add/change the Redirector rules to support HTTPS!!
Personally I've used Certificate Tools to make certs that work. Make sure you sent the Subject Alternative Name(s) DNS
to localhost
as well as Common Names
. It also provides you with a thing
to run to pack .p12 into a .pem after you've generated your certificate. It only took me about 5 tries
to work out what I had to do with it!