Coder Social home page Coder Social logo

opal-node-compiler's Introduction

Opal Compiler for Node.js

Build npm version

Transpile Ruby code to JavaScript in JavaScript!

Usage

Given a Ruby file named hello.rb:

puts "Hello world"

The following code will transpile hello.rb to JavaScript:

const Builder = require('opal-compiler').Builder
// Opal object will be available on the global scope

const builder = Builder.create()
const result = builder.build('hello.rb').toString()
console.log(result)
//(function(Opal) {
//  var self = Opal.top, $scope = Opal, nil = Opal.nil, $breaker = Opal.breaker, $slice = Opal.slice;

//  Opal.add_stubs(['$puts']);
//  return self.$puts("Hello world")
//})(Opal);

opal-node-compiler's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar ggrossetie avatar kuboon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

kuboon

opal-node-compiler's Issues

Add support for generating source map

The Ruby version of Opal supports source maps:

require 'opal'

builder = Opal::Builder.new
builder.append_paths 'lib'
builder.build 'path/to/entrypoint'

code = builder.to_s
source_map = builder.source_map

opal-node-compiler does not expose source map and accessing it directly results in error:

const { Builder: OpalBuilder } = require('opal-compiler')

const builder = OpalBuilder.create()
builder.appendPaths('lib')

const code = builder.build(modulePath).toString()
const sourceMap = builder.$source_map()
SourceMap: uninitialized constant Opal::SourceMap
    at Function.$$const_missing (node_modules/opal-runtime/src/opal.js:3052:52)
    at const_missing (node_modules/opal-runtime/src/opal.js:224:32)
    at Opal.const_get_qualified (node_modules/opal-runtime/src/opal.js:271:38)
    at constructor.$$source_map (node_modules/opal-compiler/src/opal-builder.js:1988:20)

Issues when trying to run in a NextJs app

I'm trying to setup opal-compiler in a NextJs + React App.

As soon as I put const Builder = require('opal-compiler').Builder in the js where I want to use the buildString() function, I get this error stacktrace:

process.stderr is undefined
$$6@webpack-internal:///./node_modules/opal-runtime/src/nodejs.js:676:5 
$$write@webpack-internal:///./node_modules/opal-runtime/src/opal.js:18268:12 
$$puts@webpack-internal:///./node_modules/opal-runtime/src/opal.js:18324:14 
Opal.send2@webpack-internal:///./node_modules/opal-runtime/src/opal.js:1729:17 
Opal.send@webpack-internal:///./node_modules/opal-runtime/src/opal.js:1719:17 
$$warn@webpack-internal:///./node_modules/opal-runtime/src/opal.js:4799:16 
warn@webpack-internal:///./node_modules/opal-runtime/src/opal.js:23182:10 
handle_unsupported_feature@webpack-internal:///./node_modules/opal-runtime/src/opal.js:23169:11 
$$freeze@webpack-internal:///./node_modules/opal-runtime/src/opal.js:23434:33 
Opal.modules["opal/path_reader"]/</<@webpack-internal:///./node_modules/opal-compiler/src/opal-builder.js:971:198 Opal.modules["opal/path_reader"]/<@webpack-internal:///./node_modules/opal-compiler/src/opal-builder.js:1049:7 Opal.modules["opal/path_reader"]@webpack-internal:///./node_modules/opal-compiler/src/opal-builder.js:1050:5 Opal.load@webpack-internal:///./node_modules/opal-runtime/src/opal.js:2399:13 
Opal.require@webpack-internal:///./node_modules/opal-runtime/src/opal.js:2427:17 
Opal.modules["opal/builder"]@webpack-internal:///./node_modules/opal-compiler/src/opal-builder.js:1890:8 Opal.load@webpack-internal:///./node_modules/opal-runtime/src/opal.js:2399:13 
Opal.require@webpack-internal:///./node_modules/opal-runtime/src/opal.js:2427:17 
Opal.modules["opal-builder"]@webpack-internal:///./node_modules/opal-compiler/src/opal-builder.js:34439:8 Opal.load@webpack-internal:///./node_modules/opal-runtime/src/opal.js:2399:13 
Opal.require@webpack-internal:///./node_modules/opal-runtime/src/opal.js:2427:17 
@webpack-internal:///./node_modules/opal-compiler/src/index.js:6:6 
./node_modules/opal-compiler/src/index.js@http://localhost:3000/_next/static/chunks/4.js:743:1 __webpack_require__@http://localhost:3000/_next/static/chunks/webpack.js?ts=1611842475447:873:31 fn@http://localhost:3000/_next/static/chunks/webpack.js?ts=1611842475447:151:20 
@webpack-internal:///./Components/codenewton/common/IDE/Editors/common/Ruby/Opal.js:27:34 
@webpack-internal:///./Components/codenewton/common/IDE/Editors/common/Ruby/Opal.js:161:30 
./Components/codenewton/common/IDE/Editors/common/Ruby/Opal.js@http://localhost:3000/_next/static/chunks/4.js:119:1 __webpack_require__@http://localhost:3000/_next/static/chunks/webpack.js?ts=1611842475447:873:31 fn@http://localhost:3000/_next/static/chunks/webpack.js?ts=1611842475447:151:20 
@webpack-internal:///./Components/codenewton/common/IDE/Editors/common/index.jsx:26:88 
@webpack-internal:///./Components/codenewton/common/IDE/Editors/common/index.jsx:323:30 
./Components/codenewton/common/IDE/Editors/common/index.jsx@http://localhost:3000/_next/static/chunks/4.js:131:1 __webpack_require__@http://localhost:3000/_next/static/chunks/webpack.js?ts=1611842475447:873:31 fn@http://localhost:3000/_next/static/chunks/webpack.js?ts=1611842475447:151:20

Now, I get the error is inside opal-runtime src/nodejs.js, and says process.stderr not being defined. The component where I'm using it being used server-side, i.e. it's SSR if the issue is process.stderr not being available on the client side. Is there something I can look at to fix it for NextJs.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.