将Nodejs模块加载到Web Worker中 [英] Load Nodejs Module into A Web Worker

查看:2808
本文介绍了将Nodejs模块加载到Web Worker中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算在Node.js应用程序中使用web worker来执行一些并发任务。然而,由于'webworker-threads'模块遵循HTML5 web worker的实现,需要Node.js模块如 require(fs)内部web worker不工作。 importScripts()可以加载js文件,但我想在web工作,所以我可以 require npm-安装模块。是否有解决方法?

I'm intending to use web worker inside my Node.js application for some concurrent tasks. However since the 'webworker-threads' module follows the implementation of HTML5 web worker, requiring Nodejs modules like require("fs") inside web worker does not work. importScripts() can load js files but I would like a functionality inside the web worker so that I can require npm-installed modules. Is there a workaround for that?

推荐答案

webworker-threads的作者这里。感谢您使用该模块!

author of webworker-threads here. Thank you for using the module!

有一个默认的 native_fs _ 对象与 readFileSync

There is a default native_fs_ object with the readFileSync you can use to read files.

除此之外,我主要依靠onejs来编译 package.json 转换为 importScripts 的单个JS文件,就像在部署到客户端Web worker环境时一样。 (还有onejs的许多替代方法 - browserify等)

Beyond that, I've mostly relied on onejs to compile all required modules in package.json into a single JS file for importScripts to use, just like one would do when deploying to a client-side web worker environment. (There are also many alternatives to onejs -- browserify, etc.)

希望这有助于!

这篇关于将Nodejs模块加载到Web Worker中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆