简体   繁体   English

如何使用ruby gem commonjs加载纯JS文件?

[英]How to load pure JS file using ruby gem commonjs?

Simple question: how to load pure .js file from ruby? 一个简单的问题:如何从ruby加载纯.js文件?

My current strategy is to load a .js file from within ruby is to use the commonjs gem . 我当前的策略是从ruby内加载.js文件是使用commonjs gem I recognize that the instructions are to load the js directory and then require the file . 我知道指令是要加载js目录 ,然后需要file Unfortunately, I keep getting a TypeError when I try to load the directory for trying to convert nil to string. 不幸的是,当我尝试加载目录以将nil转换为字符串时,我一直收到TypeError。

Here's my file structure: 这是我的文件结构:

.
├── Gemfile
├── LICENSE.md
├── README.md
├── Rakefile
├── lib
│   ├── example
│   │   ├── js
│   │   │   ├── data.json
│   │   │   ├── expressions.js
│   │   │   └── javascript_i_want_to_run.js
│   │   └── version.rb
│   └── example.rb
├── example.gemspec
├── script
└── test

Following the commonjs instructions , here is the failure I get when invoking from pry (notice I'm loading the directory not the file ). 按照commonjs的说明进行操作 ,这是从pry调用时遇到的失败(注意,我加载的是目录而不是file )。

[1] pry(main)> require 'commonjs'
=> true
[2] pry(main)> env = CommonJS::Environment.new(path: 'lib/example/js')
TypeError: no implicit conversion of nil into String
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/commonjs-0.2.7/lib/commonjs/environment.rb:9:in `initialize'

I'm stumped. 我很沮丧 Any ideas what could be happening? 有什么想法会发生什么吗?

There is a mistake in readme. 自述文件中有一个错误。 The first parameter in initialize method of CommonJS::Environment class is runtime . CommonJS::Environment类的initialize方法中的第一个参数是runtime It should be: 它应该是:

require 'commonjs'
require 'v8'

runtime = V8::Context.new
env = CommonJS::Environment.new(runtime, path: 'lib/example/js')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM