Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripting: Modules #1452

Merged
merged 7 commits into from Jan 27, 2021
Merged

Scripting: Modules #1452

merged 7 commits into from Jan 27, 2021

Conversation

shrinktofit
Copy link
Contributor

No description provided.

@shrinktofit shrinktofit marked this pull request as ready for review January 15, 2021 11:39
@shrinktofit shrinktofit marked this pull request as draft January 15, 2021 11:39
@shrinktofit shrinktofit marked this pull request as ready for review January 19, 2021 06:56
- [全局对象 globalThis](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/globalThis)
- [可选链操作符 `?.`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining)
- [空值合并操作符 `??`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator)
- [逻辑空赋值操作符 `??=`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_nullish_assignment)、[逻辑与赋值操作符 `&&=`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND_assignment)、[逻辑或赋值操作符 `||=`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR_assignment)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议这里列一个标题

  • 逻辑赋值操作符
    • 逻辑空...
    • 逻辑与...
    • 逻辑或...


项目资源目录下,以 `.ts` 作为后缀的文件。例如 `assets/scripts/foo.ts`、`assets/scripts/bar.mjs`,都视为 ESM 格式。

对于任何其它模块,Cocos Creator 选择与 Node.js 类似的规则来[鉴别格式](https://nodejs.org/api/packages.html#packages_determining_module_system)。具体地,以下文件将被视为 ESM 格式:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文字和链接之间需要有空格

选择与 Node.js 类似的规则来 鉴别格式


### 后缀与目录导入

Cocos Creator 对模块说明符中模块的后缀要求更偏向于 Web:必须指定后缀并且不支持 Node.js 式的目录导入。然而,基于历史原因和现行的一些限制,TypeScript 模块不允许给出后缀并支持 Node.js 式的目录导入。具体来说:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为啥用 “:”


在 ESM 模块中,通过标准的导入导出语句与目标模块进行交互,例如:

```js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“```ts” 吧

// [Module] { default: <module.exports> }
```

<!-- TODO:guess named exports -->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是漏了?通常内容间一个换行就够

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里专门注释的

@@ -1,6 +1,8 @@

Copy link
Contributor

@xunyi0 xunyi0 Jan 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

开头不需要留一个空行

@@ -1,6 +1,8 @@

# 外部代码支持

⚠️ 注意,从 3.0 开始,推荐使用模块代替插件脚本的使用!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> **注意**:从 Cocos Creator 3.0 开始,推荐使用模块代替插件脚本!

@@ -1,6 +1,8 @@

# 外部代码支持

⚠️ 注意,从 3.0 开始,推荐使用模块代替插件脚本的使用!

## 插件脚本

当脚本资源在导入时设置了 **导入为插件**,此脚本资源便称为 **插件脚本**。插件脚本通常用于引入第三方库。目前仅支持 JavaScript 插件脚本。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3.0 并没有 “导入为插件” 这个功能吧?

Cocos Creator 支持 TypeScript 4.1.0。在此基础上,做了以下限制:

- `tsconfig.json` 不会被读取。每个项目都隐含着如下选项:
```json5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码跟正文保留一个空行

"forceConsistentCasingInFileNames": true,
},
}
```

- 隐含着 `isolatedModules` 选项,这意味着:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是对代码的补充吧?那不需要另起一行了,前面保留两个空格的缩进就好了:

其中的 isolatedModules 表示:


CommonJS 模块的 [**ECMAScript 模块命名空间**](https://tc39.es/ecma262/#sec-module-namespace-objects) 表示,是含有一个 `default` 导出的命名空间,其中的 `default` 导出就指向 CommonJS 模块的 `module.exports` 的值。

该 [**模块命名空间外来对象**](https://tc39.es/ecma262/#module-namespace-exotic-object) 可以通过 `import * as m from 'cjs'` 来观测:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[**模块命名空间外来对象**](https://tc39.es/ecma262/#module-namespace-exotic-object) 可以通过 `import * as m from 'cjs'` 来观测:
[模块命名空间外来对象](https://tc39.es/ecma262/#module-namespace-exotic-object) 可以通过 `import * as m from 'cjs'` 来观测(观察?)


<!-- TODO:guess named exports -->


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一个空行就够了,留这么多空行显示的也只是一个空行


### 解析算法规范

`CREATOR_ESM_RESOLVE(specifier, parentURL)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这部分都是英文的吗

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meicuo

> npm install -g nrm
```

> `-g` 代表全局:直接安装到当前计算机中。因此,只要执行一次后续就无需再次执行。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> `-g` 代表全局:直接安装到当前计算机中。因此,只要执行一次后续就无需再次执行
> `-g` 表示全局,会将 NPM 包直接安装到当前计算机中,只要执行一次后续就不需要再次执行


## 安装 NPM 包

在项目根目录下,执行
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
在项目根目录下,执行
在项目根目录中执行以下命令:

@xunyi0
Copy link
Contributor

xunyi0 commented Jan 21, 2021

另外,这几篇文档都没有加到 SUMMARY.md 中

@shrinktofit
Copy link
Contributor Author

另外,这几篇文档都没有加到 SUMMARY.md 中

index.md加了就行了吧

@@ -0,0 +1,36 @@
# 示例:导入游戏联机对战引擎 MGOBE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要用我们官方提供的 Service 做为示例。如果用这个做为示例的话,那大家自己接入就好了,就不会用 Cocos Service 了。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已移除这个示例


```
📂 assets
📂 Libs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

大小写不一致啊,为什么不统一小写开头

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已移除这个示例

```ini
# .gitignore 文件

node_modules/ # 忽略 node_modules 目录
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们新建项目默认的 .gitignore 里面已经有忽略 node_modules 了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已移除这段

@@ -0,0 +1,126 @@
# 示例:通过 NPM 使用 protobuf.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全文档所有 NPM 都应该用小写,类似 vivo 而不是 Vivo
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更新

@xunyi0 xunyi0 changed the base branch from v3.0 to v3.0-release January 27, 2021 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants