LayaBox小游戏开发

Laya 引用 第三方JS库

2017-11-21  本文已影响1044人  合肥黑

一般情况下,使用script标签引入js文件,然后下载相应的d.ts或者自己手写一个就可以了(关于d.ts可以参考TypeScript Handbook读书笔记第四章 d.ts声明)。

一、DefinitelyTyped

这个项目里有很多别人弄好的d.ts,可以下载使用。
参考 [翻译] DefinitelyTyped 项目定义 .d.ts文件

二、typings

typings是一个包管理器用来获取声明文件。我们将会使用它来获取声明文件。
typings install --global --save dt~knockout
--global标记会告诉Typings从DefinitelyTyped去获取声明文件,这是由社区维护的.d.ts文件仓库。
这个命令会在当前目录下创建一个typings.json文件和一个typings文件夹。

具体可以参考typescript 如何使用js 库

使用npm全局安装typings :npm install -g typings
搜索资源,支持模糊搜索:typings search base64
安装ts库:typings install base-64 --save
安装js库,否则无法运行:npm install base-64 --save 注意,这两个文件名字要一样

三、问题

1.lodash
按照上面两个方式都试了,发现typings那个方式的d.ts文件不行。区别可以参考egret是不是不支持import?

我看4.1.0版本的第三方库的配置里追加了typings的字段,于是用typings下了个lodash的d.ts。d.ts的内容大概就是一个大module。

declare module 'lodash' {
    var _: _.LoDashStatic;
    namespace _ {

        xxxx
    }
    export = _;
}

正常来说在TS里用import * as _ from 'lodash',来引入并且编译什么的都没问题。但是并不能运行。。编译后的JS把import转化成var _ = require("lodash");报require is not defined。

于是我翻找了一个并非用typings下载但能用的d.ts,大概结构如下:

declare var _: _.LoDashStatic;
declare module _ {

    xxx
}
declare module "lodash" {
    export = _;
}

这个不用import就可以直接用使用。。。

在typings生成的d.ts中也可以看到来源并不是definitelyTyped

// Generated by typings
// Source: https://raw.githubusercontent.com/types/
npm-lodash/6557c3cd628ef6b39abfdc826b27ea1f06d8af89/index.d.ts

2.qs
参考qs JSON区别
功能虽然都是序列化。假设我要提交的数据如下

var a = {name:'hehe',age:10};
//qs.stringify序列化结果如下
name=hehe&age=10
//而JSON.stringify序列化结果如下:
"{"a":"hehe","age":10}"

vux中使用post提交表单数据:

this.$http.post(this.$sign.config.url.loginUrl,this.$qs.stringify({
    "phone":this.phoneNumber,
    "vCode":this.loginCode,
    "smsCode":this.phoneCode    
    })
)
.then(response=>{
    console.log(response.data);
    if(response.data.httpCode == 200){
        
    }else{
        
    }
})

在firebug中可以看到传递的参数:
phone=15210275239&vCode=8vsd&smsCode=1534

但是按照lodash的处理方式,发现不能用。报错:Uncaught ReferenceError: require is not defined

3.介绍一下Browserify
参考
github browserify
通过 Browserify 在浏览器中使用 NodeJS 模块
browserify学习总结
前端模块及依赖管理的新选择:Browserify

NodeJS 把 JavaScript 的使用从浏览器端扩展到了服务器端,使得前端开发人员可以用熟悉的语言编写服务器端代码。这一变化使得 NodeJS 很快就流行起来。在 NodeJS 社区中有非常多的高质量模块可以直接使用。根据最新的统计结果,NodeJS 的 npm 中的模块数量已经超过了 Java 的 Maven Central 和 Ruby 的 RubyGems,成为模块数量最多的社区。不过这些 NodeJS 模块并不能直接在浏览器端应用中使用,原因在于引用这些模块时需要使用 NodeJS 中的 require 方法,而该方法在浏览器端并不存在。Browserify 作为 NodeJS 模块与浏览器端应用之间的桥梁,让应用可以直接使用 NodeJS 中的模块,并可以把应用所依赖的模块打包成单个 JavaScript 文件。通过 Browserify 还可以在应用开发中使用与 NodeJS 相同的方式来进行模块化和管理模块依赖。

以下是官网教程:
Here is a tutorial on how to use Browserify on the command line to bundle up a simple file called main.js along with all of its dependencies:

main.js
var unique = require('uniq');
var data = [1, 2, 2, 3, 4, 5, 5, 5, 6];
console.log(unique(data));

Install the uniq module with npm:
npm install uniq
Now recursively bundle up all the required modules starting at main.js into a single file called bundle.js with the browserify command:
browserify main.js -o bundle.js
Browserify parses the AST for require() calls to traverse the entire dependency graph of your project.Drop a single <script> tag into your html and you're done!
<script src="bundle.js"></script>

但是我在测试的时候失败了

<script>
document.write("Hello World!");
var unique = require('uniq');
var data = [1, 2222, 2222, 3, 4, 555, 5, 5, 6];
console.log(unique(data));
</script>

报错是Uncaught ReferenceError: require is not defined

参考browserify#external-requires
You can just as easily create a bundle that will export a require() function so you can require() modules from another script tag. Here we'll create a bundle.js with the through and duplexer modules.
$ browserify -r through -r duplexer -r ./my-file.js:my-module > bundle.js
Then in your page you can do:

<script src="bundle.js"></script>
<script>
  var through = require('through');
  var duplexer = require('duplexer');
  var myModule = require('my-module');
  /* ... */
</script>

可是这个-r参数实在有点奇怪,好几次搞出Error:Cannot find module ...
最终browserify -r uniq main.js > bundle.js搞定,控制台终于打印出我们想要的结果[1, 2222, 3, 4, 5, 555, 6]

4.回到第2步,用Browserify把qs处理一下:
browserify -r qs qs.js > bundle.js
然后在index.html中

<script>
document.write("Hello World!");
var a = {name:'hehe',age:10};
var qs = require('qs');
console.log("qs",qs.stringify(a));
</script>

控制台看到qs name=hehe&age=10,OK!
不过放到LAYA项目中,require编译会报错。这里随便写个d.ts骗一骗吧,比如我写了个browserify.d.ts:
declare function require(args:string);

上一篇 下一篇

猜你喜欢

热点阅读