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

使用了 global 变量、http 模块,不兼容浏览器端使用 #2

Closed
xiaoyann opened this issue Feb 5, 2018 · 6 comments
Closed

Comments

@xiaoyann
Copy link

xiaoyann commented Feb 5, 2018

项目中使用了 Node 中才有的 API,除非浏览器端 mock 这些 API 才能正常使用

@xiaoyann xiaoyann changed the title 使用了 global 变量,不支持浏览器端使用 使用了 global 变量、http 模块,不兼容浏览器端使用 Feb 5, 2018
@ysmood
Copy link
Owner

ysmood commented Feb 5, 2018

middleware.ts 这个看名字就知道是给 express.js 用的中间件了,不会引入到浏览器的。nisper 已经用到线上网页项目一年多了,不可能有这么严重的浏览器有问题的。

https://github.com/ysmood/nisper/blob/master/lib/index.ts#L10 关于这个,你需要学习下 typescript,这个只是 type 引用,并没有实际引入代码 😂

@xiaoyann
Copy link
Author

xiaoyann commented Feb 5, 2018

nisper@0.8.2 包里的代码,这在浏览器里不报错?:

var middleware_1 = require("./middleware");
var http_1 = require("http");
exports.ServerRequest = http_1.ServerRequest;
exports.ServerResponse = http_1.ServerResponse;

......

middleware: middleware_1["default"](opts),

middleware:
引入:https://github.com/ysmood/nisper/blob/master/lib/index.ts#L9
调用:https://github.com/ysmood/nisper/blob/master/lib/index.ts#L238

@ysmood
Copy link
Owner

ysmood commented Feb 5, 2018

嗯,确实有问题。

看了下代码,这个项目发布的时候会用 webpack 编译,webpack 会自动引入 http 库,这个逻辑源自当年的 browserify ,所以不会报错,只是引入了一大块无用代码。但是如果你不用 webpack 确实可能会出错。

ysmood added a commit that referenced this issue Feb 5, 2018
@ysmood
Copy link
Owner

ysmood commented Feb 5, 2018

修正了,这两行当时加入仅仅是为了 typescript 的 type 提示,删掉没任何影响,发布了 v0.8.3

@ysmood
Copy link
Owner

ysmood commented Feb 5, 2018

非常感谢你的提示。

实际上这个是 typescript 没能实现的 feature:
microsoft/TypeScript#2812

我以为纯 type import 不会实际编译出 require,但实际上不是如此,巧合的是 webpack 自动引入才一直没事

@xiaoyann
Copy link
Author

xiaoyann commented Feb 5, 2018

真快,赞

@xiaoyann xiaoyann closed this as completed Feb 5, 2018
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

No branches or pull requests

2 participants