未验证 提交 bdbdd5e1 编写于 作者: M Mr.doob 提交者: GitHub

Merge pull request #19622 from donmccurdy/docs-installation-es-modules

Docs: Update 'Installation' section for ES Module migration.
......@@ -6,7 +6,7 @@ var list = {
"Getting Started": {
"Creating a scene": "manual/en/introduction/Creating-a-scene",
"Import via modules": "manual/en/introduction/Import-via-modules",
"Installation": "manual/en/introduction/Installation",
"Browser support": "manual/en/introduction/Browser-support",
"WebGL compatibility check": "manual/en/introduction/WebGL-compatibility-check",
"How to run things locally": "manual/en/introduction/How-to-run-things-locally",
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<base href="../../../" />
<script src="list.js"></script>
<script src="page.js"></script>
<link type="text/css" rel="stylesheet" href="page.css" />
</head>
<body>
<h1>[name]</h1>
<p>
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:
<ul>
<li>You have to manually fetch and include a copy of the library as part of your project's source code</li>
<li>Updating the library's version is a manual process</li>
<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>
</ul>
</p>
<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>
<h2>Installation via npm</h2>
<p>Three.js is published as an npm module, see: [link:https://www.npmjs.com/package/three npm]. This means all you need to do to include three.js into your project is run "npm install three"</p>
<h2>Importing the module</h2>
<p>Assuming that you're bundling your files with a tool such as [link:https://webpack.github.io/ Webpack] or [link:https://github.com/substack/node-browserify Browserify], which allow you to "require('modules')" in the browser by bundling up all of your dependencies.</p>
<p>
You should now be able to import the module into your source files and continue to use it as per normal.
</p>
<code>
var THREE = require('three');
var scene = new THREE.Scene();
...
</code>
<p>
You're also able to leverage [link:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import ES6 import syntax]:
</p>
<code>
import * as THREE from 'three';
const scene = new THREE.Scene();
...
</code>
<p>
or if you wish to import only select parts of three.js library, for example Scene:
</p>
<code>
import { Scene } from 'three';
const scene = new Scene();
...
</code>
<h2>Importable Examples</h2>
<p>
The core of three.js is focused on the most important components of a 3D engine. Many other components like loaders or controls are part of the
examples directory. three.js ensures that these files are kept in sync with the core but users have to import them separately if they are required
for a project. You can find them in the [link:https://github.com/mrdoob/three.js/tree/master/examples/jsm examples/jsm] directory. If you install three.js
via npm, import example files like so:
</p>
<code>
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
</code>
<p>
Note: When using code from the examples directory, it's important that all files match the version of
your three.js main file. For example, it's not acceptable to use *GLTFLoader* and *OrbitControls* from R96 together
with three.js R103.
</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<base href="../../../" />
<script src="list.js"></script>
<script src="page.js"></script>
<link type="text/css" rel="stylesheet" href="page.css" />
</head>
<body>
<h1>[name]</h1>
<p>
You can install three.js with [link:https://www.npmjs.com/ npm] and modern build tools, or get started quickly with just static hosting or a CDN. For most users, installing from npm is the best choice.
</p>
<p>
Whichever you choose, be consistent and import all files from the same version of the library. Mixing files from different sources may cause duplicate code to be included, or even break the application in unexpected ways.
</p>
<p>
All methods of installing three.js depend on ES modules (see [link:https://eloquentjavascript.net/10_modules.html#h_hF2FmOVxw7 Eloquent JavaScript: ECMAScript Modules], which allow you to include only the parts of the library needed in the final project.
</p>
<h2>Install from npm</h2>
<p>
To install the [link:https://www.npmjs.com/package/three three] npm module, open a terminal window in your project folder and run:
</p>
<code>
npm install --save three
</code>
<p>
The package will be downloaded and installed. Then you're ready to import it in your code:
</p>
<code>
///////////////////////////////////////////////////////
// Option 1: Import the entire three.js core library.
import * as THREE from 'three';
const scene = new THREE.Scene();
///////////////////////////////////////////////////////
// Option 2: Import just the parts you need.
import { Scene } from 'three';
const scene = new Scene();
</code>
<p>
When installing from npm, you'll almost always use some sort of [link:https://eloquentjavascript.net/10_modules.html#h_zWTXAU93DC bundling tool] to combine all of the packages your project requires into a single JavaScript file. While any modern JavaScript bundler can be used with three.js, the most popular choice is [link:https://webpack.js.org/ webpack].
</p>
<p>
Not all features are accessed directly through the <em>three</em> module (also called a "bare import"). Other popular parts of the library — such as controls, loaders, and post-processing effects — must be imported from the [link:https://github.com/mrdoob/three.js/tree/dev/examples/jsm examples/jsm] subfolder. To learn more, see <em>Examples</em> below.
</p>
<p>
Learn more about npm modules from [link:https://eloquentjavascript.net/20_node.html#h_J6hW/SmL/a Eloquent JavaScript: Installing with npm].
</p>
<h2>Install from CDN or static hosting</h2>
<p>
The three.js library can be used without any build system, either by uploading files to your own web server or by using an existing CDN. Because the library relies on ES modules, any script that references it must use <em>type="module"</em> as shown below:
</p>
<code>
&lt;script type="module">
// Find the latest version by visiting https://unpkg.com/three. The URL will
// redirect to the newest stable release.
import * as THREE from 'https://unpkg.com/three@&lt;VERSION>/build/three.module.js';
const scene = new THREE.Scene();
&lt;/script>
</code>
<p>
Not all features are accessed through the <em>build/three.module.js</em> module. Other popular parts of the library — such as controls, loaders, and post-processing effects — must be imported from the [link:https://github.com/mrdoob/three.js/tree/dev/examples/jsm examples/jsm] subfolder. To learn more, see <em>Examples</em> below.
</p>
<h2>Examples</h2>
<p>
The core of three.js is focused on the most important components of a 3D engine. Many other useful components — such as controls, loaders, and post-processing effects — are part of the [link:https://github.com/mrdoob/three.js/tree/dev/examples/jsm examples/jsm] directory. They are referred to as "examples," because while you can use them off the shelf, they're also meant to be remixed and customized. These components are always kept in sync with the core library, whereas similar third-party packages on npm are maintained by different people and may not be up to date.
</p>
<p>
Examples do not need to be <em>installed</em> separately, but do need to be <em>imported</em> separately. If three.js was installed with npm, you can load the [page:OrbitControls] component with:
</p>
<code>
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
const controls = new OrbitControls();
</code>
<p>
If three.js was installed from a CDN, use the same CDN to install other components:
</p>
<code>
&lt;script type="module">
// Find the latest version by visiting https://unpkg.com/three. The URL will
// redirect to the newest stable release.
import { OrbitControls } from 'https://unpkg.com/three@&lt;VERSION>/examples/jsm/controls/OrbitControls.js';
const controls = new OrbitControls();
&lt;/script>
</code>
<p>
It's important that all files use the same version. Do not import different examples from different versions, or use examples from a different version than the three.js library itself.
</p>
<h2>Compatibility</h2>
<h3>CommonJS imports</h3>
<p>
While most modern JavaScript bundlers now support ES modules by default, some older build tools might not. In those cases you can likely configure the bundler to understand ES modules: [link:http://browserify.org/ Browserify] just needs the [link:https://github.com/babel/babelify babelify] plugin, for example.
</p>
<h3>Node.js</h3>
<p>
Using three.js in [link:https://eloquentjavascript.net/20_node.html Node.js] can be difficult, for two reasons:
</p>
<p>
First, because three.js is built for the web, it depends on browser and DOM APIs that don't always exist Node.js. Some of these issues can be resolved by using shims like [link:https://github.com/stackgl/headless-gl headless-gl], or by replacing components like [page:TextureLoader] with custom alternatives. Other DOM APIs may be deeply intertwined with the code that uses them, and will be harder to work around. We welcome simple and maintainable pull requests to improve Node.js support, but recommend opening an issue to discuss your improvements first.
</p>
<p>
Second, Node.js support for ES modules is ... complicated. As of Node.js v12, the core library can be imported as a CommonJS module, with <em>require('three')</em>. However, most example components in <em>examples/jsm</em> cannot. Future versions of Node.js may resolve this, but in the meantime you may need to use workarounds like [link:https://github.com/standard-things/esm esm] to enable your Node.js application to recognize ES modules.
</p>
</body>
</html>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<base href="../../../" />
<script src="list.js"></script>
<script src="page.js"></script>
<link type="text/css" rel="stylesheet" href="page.css" />
</head>
<body>
<h1>通过模块来引入([name])</h1>
<p>
虽然通过script标签来引入three.js是一个能够快速起步、快速运行的方式,但这种方式对于一些具有较长生命周期的项目来说是有一些缺点。比如:
<ul>
<li>你必须手动获得并在你的项目源代码中包含这个库的一个拷贝</li>
<li>更新库的版本是一个手动操作的过程</li>
<li>在检查新版本的库时,你的版本差异对比将会被许多行的构建文件给弄乱。</li>
</ul>
</p>
<p>使用像npm这样的依赖包管理器,你只需在你的机器上下载并导入你所需要的版本的库就很好地避免这些需要注意的问题。</p>
<h2>通过npm来安装</h2>
<p>Three.js目前已经作为一个npm模块来进行了发布,详情请参阅:[link:https://www.npmjs.com/package/three npm]。这意味着你只需运行"npm install three"就可以使你的项目包含three.js库。</p>
<h2>导入这个模块</h2>
<p>假设你正在使用[link:https://webpack.github.io/ Webpack]或者[link:https://github.com/substack/node-browserify Browserify]等允许你“通过打包所有依赖,来在浏览器中使用require('modules')”的打包工具对你的文件进行打包。</p>
<p>你现在可以在你的源代码中引入模块,并继续像往常一样使用这个库。
</p>
<code>
var THREE = require('three');
var scene = new THREE.Scene();
...
</code>
<p>
你也可以使用[link:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/import ES6 import](在ES6标准中新增的import语句)
</p>
<code>
import * as THREE from 'three';
const scene = new THREE.Scene();
...
</code>
<p>
或者,如果你希望只导入three.js库中的特定部分,例如Scene:
</p>
<code>
import { Scene } from 'three';
const scene = new Scene();
...
</code>
<h2>可引入的示例</h2>
<p>
The core of three.js is focused on the most important components of a 3D engine. Many other components like loaders or controls are part of the
examples directory. three.js ensures that these files are kept in sync with the core but users have to import them separately if they are required
for a project. You can find them in the [link:https://github.com/mrdoob/three.js/tree/master/examples/jsm examples/jsm] directory. If you install three.js
via npm, import example files like so:
</p>
<code>
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
</code>
<p>
请注意:当你在使用来自示例(examples)文件夹中的代码时,其中的所有文件和你的three.js主文件版本相匹配是很重要的。
比如说,three.js的R103版本不能够接受和来自R96版本的*GLTFLoader*和*OrbitControls*一起使用。
</p>
</body>
</html>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<base href="../../../" />
<script src="list.js"></script>
<script src="page.js"></script>
<link type="text/css" rel="stylesheet" href="page.css" />
</head>
<body>
<h1>通过模块来引入([name])</h1>
<p>
You can install three.js with [link:https://www.npmjs.com/ npm] and modern build tools, or get started quickly with just static hosting or a CDN. For most users, installing from npm is the best choice.
</p>
<p>
Whichever you choose, be consistent and import all files from the same version of the library. Mixing files from different sources may cause duplicate code to be included, or even break the application in unexpected ways.
</p>
<p>
All methods of installing three.js depend on ES modules (see [link:https://eloquentjavascript.net/10_modules.html#h_hF2FmOVxw7 Eloquent JavaScript: ECMAScript Modules], which allow you to include only the parts of the library needed in the final project.
</p>
<h2>Install from npm</h2>
<p>
To install the [link:https://www.npmjs.com/package/three three] npm module, open a terminal window in your project folder and run:
</p>
<code>
npm install --save three
</code>
<p>
The package will be downloaded and installed. Then you're ready to import it in your code:
</p>
<code>
///////////////////////////////////////////////////////
// Option 1: Import the entire three.js core library.
import * as THREE from 'three';
const scene = new THREE.Scene();
///////////////////////////////////////////////////////
// Option 2: Import just the parts you need.
import { Scene } from 'three';
const scene = new Scene();
</code>
<p>
When installing from npm, you'll almost always use some sort of [link:https://eloquentjavascript.net/10_modules.html#h_zWTXAU93DC bundling tool] to combine all of the packages your project requires into a single JavaScript file. While any modern JavaScript bundler can be used with three.js, the most popular choice is [link:https://webpack.js.org/ webpack].
</p>
<p>
Not all features are accessed directly through the <em>three</em> module (also called a "bare import"). Other popular parts of the library — such as controls, loaders, and post-processing effects — must be imported from the [link:https://github.com/mrdoob/three.js/tree/dev/examples/jsm examples/jsm] subfolder. To learn more, see <em>Examples</em> below.
</p>
<p>
Learn more about npm modules from [link:https://eloquentjavascript.net/20_node.html#h_J6hW/SmL/a Eloquent JavaScript: Installing with npm].
</p>
<h2>Install from CDN or static hosting</h2>
<p>
The three.js library can be used without any build system, either by uploading files to your own web server or by using an existing CDN. Because the library relies on ES modules, any script that references it must use <em>type="module"</em> as shown below:
</p>
<code>
&lt;script type="module">
// Find the latest version by visiting https://unpkg.com/three. The URL will
// redirect to the newest stable release.
import * as THREE from 'https://unpkg.com/three@&lt;VERSION>/build/three.module.js';
const scene = new THREE.Scene();
&lt;/script>
</code>
<p>
Not all features are accessed through the <em>build/three.module.js</em> module. Other popular parts of the library — such as controls, loaders, and post-processing effects — must be imported from the [link:https://github.com/mrdoob/three.js/tree/dev/examples/jsm examples/jsm] subfolder. To learn more, see <em>Examples</em> below.
</p>
<h2>Examples</h2>
<p>
The core of three.js is focused on the most important components of a 3D engine. Many other useful components — such as controls, loaders, and post-processing effects — are part of the [link:https://github.com/mrdoob/three.js/tree/dev/examples/jsm examples/jsm] directory. They are referred to as "examples," because while you can use them off the shelf, they're also meant to be remixed and customized. These components are always kept in sync with the core library, whereas similar third-party packages on npm are maintained by different people and may not be up to date.
</p>
<p>
Examples do not need to be <em>installed</em> separately, but do need to be <em>imported</em> separately. If three.js was installed with npm, you can load the [page:OrbitControls] component with:
</p>
<code>
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
const controls = new OrbitControls();
</code>
<p>
If three.js was installed from a CDN, use the same CDN to install other components:
</p>
<code>
&lt;script type="module">
// Find the latest version by visiting https://unpkg.com/three. The URL will
// redirect to the newest stable release.
import { OrbitControls } from 'https://unpkg.com/three@&lt;VERSION>/examples/jsm/controls/OrbitControls.js';
const controls = new OrbitControls();
&lt;/script>
</code>
<p>
It's important that all files use the same version. Do not import different examples from different versions, or use examples from a different version than the three.js library itself.
</p>
<h2>Compatibility</h2>
<h3>CommonJS imports</h3>
<p>
While most modern JavaScript bundlers now support ES modules by default, some older build tools might not. In those cases you can likely configure the bundler to understand ES modules: [link:http://browserify.org/ Browserify] just needs the [link:https://github.com/babel/babelify babelify] plugin, for example.
</p>
<h3>Node.js</h3>
<p>
Using three.js in [link:https://eloquentjavascript.net/20_node.html Node.js] can be difficult, for two reasons:
</p>
<p>
First, because three.js is built for the web, it depends on browser and DOM APIs that don't always exist Node.js. Some of these issues can be resolved by using shims like [link:https://github.com/stackgl/headless-gl headless-gl], or by replacing components like [page:TextureLoader] with custom alternatives. Other DOM APIs may be deeply intertwined with the code that uses them, and will be harder to work around. We welcome simple and maintainable pull requests to improve Node.js support, but recommend opening an issue to discuss your improvements first.
</p>
<p>
Second, Node.js support for ES modules is ... complicated. As of Node.js v12, the core library can be imported as a CommonJS module, with <em>require('three')</em>. However, most example components in <em>examples/jsm</em> cannot. Future versions of Node.js may resolve this, but in the meantime you may need to use workarounds like [link:https://github.com/standard-things/esm esm] to enable your Node.js application to recognize ES modules.
</p>
</body>
</html>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册