Cocos Creator 3.x之自定义npm库(ts版)
原创
©著作权归作者所有:来自51CTO博客作者Aonaufly的原创作品,请联系作者获取转载授权,否则将追究法律责任
一,创建npm库
1,代码
a, toolkit.js
export class Toolkit{
public printSomething(): void{
console.log(`this is ts npm lib`);
}
}
b, index.js
import { Toolkit } from "./toolkit";
export {Toolkit};
2, 编辑tsconfig.jsom
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
3, 命令:tsc
4, 命令:npm publish, 提交库
二,nodejs平台测试
1, 命令:npm i npmts-demo-alex
2, 测试代码index.js
// "use strict"
// exports.__esModule = true;
let npm_1 = require("npmts-demo-alex");
let alex = new npm_1.Toolkit();
alex.printSomething();
3, 结果
三,cocos creator 3.x平台测试
```json
{
"compilerOptions": {
"moduleResolution": "node",
"esModuleInterop": true,
...
},
...
}
```
1,命令:npm i npmts-demo-alex
2,代码
3, 结果