02. 初始化Three.js 项目
02 初始化Three.js 项目
本文以
第
yarn global add create-react-app
第
我用的是
create-react-app 4.0.2 版本,对应的是React 17.0.1
yarn create react-app test-threejs --template typescript
第
-
修改
TS 编译目标ES 版本为es2017 "target": "es2017"
-
添加一些本人
TS 配置偏好"noUnusedLocals": true, "noUnusedParameters": true, "sourceMap": true, "removeComments": false
第
由
yarn add --dev react-app-rewired react-app-rewire-alias
第
-
在项目根目录,新建文件
tsconfig.paths.json ,内容暂时设置为:{ "compilerOptions": { "baseUrl": ".", "paths": { "@/src/*": ["./src/*"], "@/components/*": ["./src/components/*"] } } }
我们暂时先添加
2 个路径映射src 和components ,具体路径还会根据将来实际开发过程中所需要创建不同的目录结构进行修改。补充:根据
typescript 官方更新说明文档,baseUrl 这一项是可以省略的,但是上面代码中还是遵循了之前的配置方式,继续添加上了baseUrl 。 -
在项目根目录,新建文件
config-overrides.js ,内容为:const { alias, configPaths } = require('react-app-rewire-alias') module.exports = function override(config) { alias(configPaths('./tsconfig.paths.json'))(config) return config }
注意是
.js 文件 而不是.ts 文件,鼠标放到require 上后也许会显示提示文字:文件是CommonJS 模块;它可能会转换为ES6 模块。请忽略这个提示,这并不是什么错误信息。
require 是Nodejs 导入模块的方式,TypeScript 导入模块使用的是import 。 -
在项目根目录,新建文件
global.d.ts ,内容为:注意:本步骤的目的是为了让
TS 忽略对react-app-rewire-alias 和其他一些非常规格式文件的导入检查。本步骤是可选的,不是必须的,你可以跳过本步骤。
declare module '*.png'; declare module '*.gif'; declare module '*.jpg'; declare module '*.jpeg'; declare module '*.svg'; declare module '*.css'; declare module '*.less'; declare module '*.scss'; declare module '*.sass'; declare module '*.styl'; declare module 'react-app-rewire-alias';
-
修改
tsconfig.json 文件,添加以下一行内容:{ "extends": "./tsconfig.paths.json", "compilerOptions": { ... } }
请注意
extends 是和compilerOptions 平级的。至此,我们的
tsconfig.json 最终内容如下:{ "extends": "./tsconfig.paths.json", "compilerOptions": { "target": "es2017", "lib": [ "dom", "dom.iterable", "esnext" ], "allowJs": true, "skipLibCheck": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "strict": true, "forceConsistentCasingInFileNames": true, "noFallthroughCasesInSwitch": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "noEmit": true, "jsx": "react-jsx", "noUnusedLocals": true, "noUnusedParameters": true, "sourceMap": true, "removeComments": false }, "include": [ "src" ] }
第
将命令中
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
},
第
yarn add node-sass --dev
假设你使用的是较早版本的
create-react-app ,那么当时还不支持最新版node-sass 5.0 ,所以你只能安装:yarn add node-sass@4.14.1 --dev
第
//npm install --save three
yarn add three
以下更新于
2021.04.11
关于
写这篇文章的时候是
所以,我们现在还需要额外安装对应的
//npm install @types/three
yarn add @types/three
本系列教程前面相当一部分示例是基于
所谓不能正常运行,多数都是因为某些类在新版本中引入路径发生了变化,可根据
VSCode 中的提示进行修改。
我们会在第
以上更新于
2021.04.11
第
{
"name": "test-threejs",
"homepage": ".",
...
}
假设不添加
我猜测
homepage 的默认值是 “/”
如果你的项目将来并不是发布在网站根目录,那么设置
举例:
假设将来项目网址为:https://threejs.puxiao.com,那么你完全可以跳过本步骤,不作任何修改。
但若将来项目网址为:https://threejs.puxiao.com/demo/,那么此时所有文件资源存放在
第
以下为我个人的习惯,你可以根据自己喜好对应选择是否清理。
-
清除掉
index.tsx 中<React.StrictMode> 、reportWebVitals()目前
React 最新版中对应的tsconfig.json 默认就使用的是严格模式,因此<React.StrictMode> 是可以删除掉的 -
删除掉
src 目录下setupTests.ts 、reportWebVitals.ts、App.test.tsx 文件这些都是用来做
React 自动单元调试的,暂且用不到 -
删除掉
public 目录下logo192.png 、logo512.png、manifest.json、robots.txt -
清除掉
public 目录下index.html 中无用的代码,只保留最基础的标签。<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8" /> <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="theme-color" content="#000000" /> <meta name="description" content="Web site created using create-react-app" /> <title>Hello Threejs</title> </head> <body> <noscript>You need to enable JavaScript to run this app.</noscript> <div id="root"></div> </body> </html>
经过以上
下一章,终于要开始编写