In this in-class activity, we will set up a development environment. You should follow along with these instructions. After you follow these steps, your repository should look like my example.
- Create a new Github repository and clone it to your computer. Open it in VSCode.
- In the VSCode terminal, ensure you are in the correct directory and run
npm init
. This will initialize an NPM project and create apackage.json
file. It will walk you through a few prompts in your terminal. You can accept the defaults for all of them.
Now, let's install three.js.
- Install three.js by running
npm install three
. If you look at yourpackage.json
, you will now seethree
listed as a dependency. You will also see a new folder show up in your repository:node_modules
. This is where packages installed with NPM live! - Create an
index.html
file. Copy the following code into it::
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Cubin' along</title>
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<script src="index.js" type="module" defer></script>
</body>
</html>
- Create an
index.js
file. Copy over some code that usesthree.js
. This comes from their documentation intro.
import {
BoxGeometry,
Mesh,
MeshPhongMaterial,
PerspectiveCamera,
Scene,
WebGLRenderer,
DirectionalLight,
} from "three";
// Create our scene
const scene = new Scene();
// Create the camera so we can see our scene
const camera = new PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
// Create our renderer and add it to the DOM
const renderer = new WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Create our cube mesh from a geometry and a material and add it to the scene
const geometry = new BoxGeometry(1, 1, 1);
const material = new MeshPhongMaterial({ color: 0x00ff00 });
const cube = new Mesh(geometry, material);
scene.add(cube);
// Add a directional light so we can see shadows on the cube
const color = 0xffffff;
const intensity = 1;
const light = new DirectionalLight(color, intensity);
light.position.set(-1, 2, 4);
scene.add(light);
// Position the camera
camera.position.z = 5;
// The animation loop updates the cube's rotation
function animate() {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
// Start the animation loop
animate();
We have some starter code, and now we need to run the app. However, we can't use
live server like we've been doing so far. This is because our index.js
file is
trying to import a library using its name, three
, but the library files are
stored in /node_modules
. We need a server that knows how to connect the
imports in index.js
to the libraries in node_modules
.
- Install
web-dev-server
. We will use this instead of live server to serve our code:npm install --save-dev @web/dev-server
- Add a script to your
package.json
that we can use to run the dev server. Inside thescripts
section, copy in"start": "web-dev-server --node-resolve --open --watch"
. This means that we will be able to run the dev server usingnpm run start
- Run your app locally:
npm run start
. You should see the rotating cube show up!
Now, we're going to use Rollup to build our code. It will bundle everything together nicely.
-
Install Rollup:
npm install rollup --save-dev
. -
Install the node-resolve plugin:
npm install @rollup/plugin-node-resolve --save-dev
, which will bundle things we have put in ournode_modules
folder (in this case, three.js). -
Install the copy plugin:
npm install rollup-plugin-copy --save-dev
. This plugin will allow us to copy over certain files to our build folder. -
Create a configuration file for Rollup called
rollup.config.js
:import { nodeResolve } from "@rollup/plugin-node-resolve"; import copy from "rollup-plugin-copy"; module.exports = { input: "index.js", output: { dir: "dist", }, plugins: [ copy({ targets: [{ src: "index.html", dest: "dist" }], }), nodeResolve(), ], };
This config says to bundle the
index.js file
and output it in a folder calleddist
. It also uses the copy plugin to copy over ourindex.html
file, also putting it indist
. It also uses thenode-resolve
plugin to include any dependencies in ournode_modules
folder. -
Add a build script to your
package.json
. Inside thescripts
field, next to yourstart
script, add"build": "rollup --config"
. This will run rollup using the config file we just made. -
Now, you can run the build script you just added by running
npm run build
. Do it now. You will see that there is a new folder created calleddist/
, which contains two files:index.html
andindex.js
. However, theindex.js
has a lot more code than was in your originalindex.js
! All of the things thatindex.js
depends on, (namely, Three.js), have been bundled into one file. This folder now contains the built version of your site.
OK - we've bundled our site! However, we have not yet pushed our changes to our
main branch Github. But wait! There is a VERY IMPORTANT thing we have to do
first. We need to tell git to ignore certain files. Specifically, we do not
want to push the contents of our node_modules
folder or our dist
folder to
our main branch. We're recording what dependencies to install in our
package.json
, so there's no point in pushing another copy of that code to
Github.
-
Create a file named
.gitignore
. This is the file that tells git what things it should ignore -
Inside
.gitignore
, add two lines:node_modules/ dist/
-
Now, track your changes with git:
git add --all
. You should NOT see yournode_modules
ordist
folders in the list of changed files. If you do, you should double check that you made your.gitignore
correctly. -
Commit them:
git commit -m "deployed my site"
-
And push:
git push
Now, you should see all of your changes in your main branch!
Now, lets use Github pages to deploy our site! Previously, we were able to just make a Github pages site from the code pushed to our main branch. However, now we want to make our site from the code we bundled using Rollup! We can use a handy package to do this:
- Install the
gh-pages
package:npm install gh-pages --save-dev
- Add a
deploy
script to yourpackage.json
, under thescripts
section:"deploy": "npm run build && gh-pages -d dist"
- Run the deploy script:
npm run deploy
This will first build your site using the build
script we made above.
Remember, the build
script puts the built version of your site in a folder
called dist/
. Then, our deploy
script will use the gh-pages
library to
push everything in your dist/
folder to a special branch called gh-pages
.
Github will automatically use this branch as the pages branch and serve your
site files from it.
Your site should soon be live! You might have to wait a few minutes for Github's
deployment checks to pass. You will be able to view it at
<username.github.io>/<repositoryname>
. For me, this looks like
branchwelder.github.io/dev-toolchain-example/
.
- Rollup