jbr / sibilant

Just another compile-to-js LISP-like language
https://sibilant.org
MIT License
386 stars 47 forks source link

Require external libraries #71

Closed pfmescher closed 10 years ago

pfmescher commented 10 years ago

I'm currently trying to make a small api using sibilant and express and when I try to run the app using sibilant directly, it is not resolving the express import correctly.

Console output:

pablo@linux-oto5:~/notes> sibilant app.sl > app.js
pablo@linux-oto5:~/notes> node app.js
Listening on 3000
  C-c C-c
pablo@linux-oto5:~/notes>sibilant -x app.sl

module.js:333
    throw err;
          ^
Error: Cannot find module 'express'
    at Function.Module._resolveFilename (module.js:331:15)
    at Function.Module._load (module.js:273:25)
    at Module.require (module.js:357:17)
    at require (module.js:373:17)
    at sibilant:1:15
    at Object.exports.runInContext (vm.js:60:17)
    at runInSandbox (/home/pablo/.nvm/v0.11.13/lib/node_modules/sibilant/lib/cli.js:35:13)
    at /home/pablo/.nvm/v0.11.13/lib/node_modules/sibilant/lib/cli.js:136:18
    at /home/pablo/.nvm/v0.11.13/lib/node_modules/sibilant/lib/cli.js:140:9
    at /home/pablo/.nvm/v0.11.13/lib/node_modules/sibilant/lib/cli.js:142:5

app.sl:

(var express (require "express"))
(var expressJwt (require "express-jwt"))
;(var config (require 'config.sl))
(var config {secret "this is a secret" port 3000})
;(var mongoReaction (require 'mongo-reaction))
(var app (express))
(app.get "/login" (# (req res) (res.send 'Hola)))
(app.use "/" (expressJwt {secret (get config 'secret)}))

(var server
     (app.listen (get config 'port)
         (#> (console.log
           "Listening on %d"
           (get (server.address) 'port)))))
jbr commented 10 years ago

Thanks for reporting this. Can you confirm that this is fixed in v0.1.7 or HEAD?

pfmescher commented 10 years ago

Fix confirmed. Works great now :)

However, I'm now trying to import an external file with a sibilant object into the main app and it's throwing again. I don't know if it is a bug, but the documentation doesn't explain how to add external .sibilant files so I'm not sure how to properly do this..

config.sl

{ secret "this is my secret" port 3000 }

console

pablo@linux-oto5:~/notes> sibilant -x app.sl

/home/pablo/notes/config.sl:1
nction (exports, require, module, __filename, __dirname) { { secret "this is m
                                                                    ^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected string
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/pablo/notes/app.sl:3:14)
    at Module._compile (module.js:456:26)
    at runInSandbox (/usr/local/lib/node_modules/sibilant/lib/cli.js:32:23)
    at /usr/local/lib/node_modules/sibilant/lib/cli.js:133:18

I'm trying to import the file as you would an external JSON file. Changing the filename to .json and fixing the syntax works though, so it's nothing I can't live without.. just thought it would be nice

jbr commented 10 years ago

1) Sibilant only registers the .sibilant extension. require-ing a .sl file won't be recognized as sibilant. I'm open to registering other extensions, but at the moment it's just .sibilant. 2) If you put json in a .sibilant file and require it, sibilant will do exactly what node would do, which is to export an empty object because there are no module.exports. If you want to export the contents of your file, you'll need to either (set module 'exports { secret "this is my secret" port 3000 }) or require it as json.

Potentially, I could add an extension for "sibilant-flavored json" but I'm not entirely convinced it's worth it. It would have to be a distinct extension from a sibilant/node module, though. Maybe something like .sjson. So the short answer is "the feature you were expecting doesn't exist yet, and wouldn't work quite like that if it did"

pfmescher commented 10 years ago

Thanks for the clarification. As I said earlier, the documentation is not quite open about this.. We can keep the bug closed then :)