Skip to content

Instantly share code, notes, and snippets.

@Boscop
Forked from mattdesl/modular-three.md
Created September 22, 2016 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Boscop/dcecf5b25e5a2851200c9151319b5435 to your computer and use it in GitHub Desktop.
Save Boscop/dcecf5b25e5a2851200c9151319b5435 to your computer and use it in GitHub Desktop.
quick/easy ThreeJS hacking with npm

This isn't bullet-proof but here's how I've doing things lately:

modular ThreeJS quickie

  • npm install three --save
  • Include this in your browserify/webpack root script, typically your index.js:
global.THREE = require('three')
  • Add the "THREE" global to your linter (e.g. semistandard/eslintrc)
  • For the rest of your script(s), code per usual by just assuming THREE exists in window/global scope.
global.THREE = require('three');
require('./other.js');
console.log(new THREE.Vector());

custom builds

If I need to customize ThreeJS (which is very often in production) then I will simply replace require('three') with require('./lib/vendor/three.js').

examples / one-off extension

As bad as it sounds, I might just copy the ThreeJS file and replace THREE.Foo = function() with function Foo (), then add module.exports = Foo at the bottom. This maybe takes 1-2 minutes and tends to be less painful than dealing with build tool junk.

lots of extensions

Sometimes you need a whole bunch of extensions/examples. Instead of converting them all I pretty much give up and just go the standard script-tag way.

To KISS I just end up removing the require('three') from my index, copying node_modules/three/build/three.js to a vendor folder, npm uninstall three --save to remove from package.json, and adding some script tags in HTML:

<body>
  ...
  <script src="js/vendor/three.js.js"></script>
  <script src="js/vendor/some-three-extension.js"></script>
  <script src="js/bundle.js"></script>
  ...
</body>

My project's codebase doesn't change since it already assumes THREE is in global scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment