Add js2coffee.js to my ruby gem. Thanks. #396

Open
zw963 opened this Issue Sep 8, 2015 · 4 comments

Projects

None yet

2 participants

@zw963
zw963 commented Sep 8, 2015

Hi, I hope add js2coffee.js to my gem. https://github.com/zw963/coffee_without_nodejs
Of cause I will rename this gem to a new name later. (could I use js2coffee for gem name?)
the only goal is to convert between coffee and js simple enough, no more options, no node dependency.

following is my example code:

require 'execjs'

source = File.read(File.expand_path('../js2coffee.js', __FILE__))
compiler = ExecJS.compile("var window = {}; #{source}")

wrapper = <<-WRAPPER
(function(script, options) {
    try {
        var result;
        result = window.js2coffee.build(script, options);
        return [result.code, result.map, result.warnings];
    } catch (err) {
        if (err instanceof SyntaxError && err.location) {
            throw new SyntaxError([options.filename, err.location.first_line + 1, err.location.first_column + 1].join(":") + ": " + err.message)
        } else {
            throw err;
        }
    }
})
WRAPPER

result = compiler.call(wrapper, 'var x = function(x) { x * x };')
p result
@rstacruz
Member
rstacruz commented Sep 9, 2015

go for it, but i do suggest that:

  • errors remain as errors (ie, a Js2coffee::SyntaxError Ruby exception) in the spirit of making it work like other compilers in the ruby language.
  • return result as a ruby Struct, so you can do result.code.
  • keep version numbers consistent with js2coffee's version numbers.
@zw963
zw963 commented Sep 9, 2015

Thank very much.

errors remain as errors (ie, a Js2coffee::SyntaxError Ruby exception)

I will try figure out this later, do my best to achieve this.

return result as a ruby Struct, so you can do result.code.

good advise.

keep version numbers consistent with js2coffee's version numbers.

my gem is still be basically, and function simple, maybe bugged. (only used by myself before now)
so maybe I should keep very low version number. I will add js2coffee compiler and Coffee compiler
version in README. perhaps, some month later, I will keep this synchronous.

@zw963
zw963 commented Sep 13, 2015

Hi, is there any option, can output js.map v3 format?

e.g.

{
  "version": 3,
  "file": "notifications.js",
  "sourceRoot": "",
  "sources": [
    "../coffee/notifications.coffee"
  ],
  "names": [],
  "mappings": "AAAA;AAAA,EAAA,GAAG,CAAC,iCAAJ,GAAwC,SAAA,GAAA;WACtC,CAAA,CAAE,OAAF,CAAU,CA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AAO,IAFP;AAAA,QAGA,KAAA,EAAO,GAHP;AAAA,QAIA,KAAA,EAAO,IAJP;AAAA,QAKA,SAAA,EAAW,WALX;AAAA,QAMA,QAAA,EAAU,IANV;AAAA,QAOA,OAAA,EAAS;UACP;AAAA,YACE,IAAA,EAAM,MADR;AAAA,YAEE,KAAA,EAAO,SAAA,GAAA;qBACL,QAAQ,CAAC,MAAT,CAAgB,IAAhB,EADK;YAAA,CAFT;WADO,EAMP;AAAA,YACE,IAAA,EAAM,IADR;AAAA,YAEE,KAAA,EAAO,SAAA,GAAA;qBACL,CAAA,CAAE,IAAF,CAAO,CAAC,MAAR,CAAe,OAAf,EADK;YAAA,CAFT;WANO;SAPT;OADF,EADF;KANwB;EAAA,CAP1B,CAAA;;AAAA,EAmCA,GAAG,CAAC,mBAAJ,GAA0B,SAAA,GAAA;WACxB,CAAA,CAAE,YAAF,CAAe,CAAC,SAAhB,CACE;AAAA,MAAA,MAAA,EAAQ,CAAC,CAAC,MAAF,CAAS,wBAAT,EAAmC,MAAnC,CAAA,IAA8C,CAAtD;AAAA,MACA,QAAA,EAAU,SAAC,KAAD,EAAQ,EAAR,GAAA;AACR,YAAA,MAAA;AAAA,QAAA,MAAA,GAAS,CAAA,CAAE,YAAF,CAAe,CAAC,SAAhB,CAA0B,QAA1B,EAAoC,QAApC,CAAT,CAAA;eACA,CAAC,CAAC,MAAF,CAAS,wBAAT,EAAmC,MAAnC,EAFQ;MAAA,CADV;AAAA,MAIA,WAAA,EAAa,IAJb;KADF,EADwB;EAAA,CAnC1B,CAAA;AAAA"
}
@rstacruz
Member

Sorry, haven't explored sourcemap output formats, but feel free to send a PR if it's something that needs to be modified in the package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment