2
0

Import-via-modules.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <base href="../../" />
  6. <script src="list.js"></script>
  7. <script src="page.js"></script>
  8. <link type="text/css" rel="stylesheet" href="page.css" />
  9. </head>
  10. <body>
  11. <h1>[name]</h1><br />
  12. <p>
  13. While importing three.js via script tags is a great way to get up and running fast, it has a few drawbacks for longer lived projects, for example:
  14. <ul>
  15. <li>You have to manually fetch and include a copy of the library as part of your project's source code</li>
  16. <li>Updating the library's version is a manual process</li>
  17. <li>When checking in a new version of the library your version control diffs are cluttered by the many lines of the build file</li>
  18. </ul>
  19. </p>
  20. <p>Using a dependency manager like npm avoids these caveats by allowing you to simply download and import your desired version of the library onto your machine.</p>
  21. <h2>Installation via npm</h2>
  22. <p>Three.js is published as an npm module, see: <a href="https://www.npmjs.com/package/three" target="_blank">npm</a>. This means all you need to do to include three.js into your project is run "npm install three"</p>
  23. <h2>Importing the module</h2>
  24. <p>Assuming that you're bundling your files with a tool such as <a href="https://webpack.github.io/" target="_blank">Webpack</a> or <a href="https://github.com/substack/node-browserify" target="_blank">Browserify</a>, which allow you to "require('modules') in the browser by bundling up all of your dependencies."</p>
  25. <p>
  26. You should now be able to import the module into your source files and continue to use it as per normal.
  27. </p>
  28. <code>
  29. var THREE = require('three');
  30. var scene = new THREE.Scene();
  31. ...
  32. </code>
  33. <p>
  34. You're also able to leverage <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import" target="_blank">ES6 import syntax</a>:
  35. </p>
  36. <code>
  37. import * as THREE from 'three';
  38. const scene = new THREE.Scene();
  39. ...
  40. </code>
  41. <p>
  42. or if you wish to import only select parts of three.js library, for example Scene:
  43. </p>
  44. <code>
  45. import { Scene } from 'three';
  46. const scene = new Scene();
  47. ...
  48. </code>
  49. <h2>Caveats</h2>
  50. <p>
  51. Currently it's not possible to import the files within the "examples/js" directory in this way.
  52. This is due to some of the files relying on global namespace pollution of THREE. For more information see <a href="https://github.com/mrdoob/three.js/issues/9562" target="_blank">Transform `examples/js` to support modules #9562</a>.
  53. </p>
  54. </body>
  55. </html>