Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected token export #126

Open
dcmorse opened this issue Jan 24, 2019 · 4 comments
Open

Unexpected token export #126

dcmorse opened this issue Jan 24, 2019 · 4 comments

Comments

@dcmorse
Copy link

dcmorse commented Jan 24, 2019

irb(main):001:0> context = MiniRacer::Context.new
=> #<MiniRacer::Context:0x0055fb4a1deef8 @functions={}, @timeout=nil, @max_memory=nil, @current_exception=nil, @isolate=false, @disposed=false, @callback_mutex=#<Thread::Mutex:0x0055fb4a1dede0>, @callback_running=false, @thread_raise_called=false, @eval_thread=nil>
irb(main):002:0> context.eval 'export const adder = (a,b)=>a+b;'
MiniRacer::ParseError: Uncaught SyntaxError: Unexpected token export at undefined:1:0

I'm confused. Do I have to set flags or something to have mini_racer understand export?

@SamSaffron
Copy link
Collaborator

hmmm what is export going to even do? we would need to implement some sort of shim to implement it on behalf of v8.

@dcmorse
Copy link
Author

dcmorse commented Jan 26, 2019

I was imagining something like this

c = MiniRacer::Context.new
c.eval('export const foo = ()=>{return "foo";};', filename: 'foo.js')
c.eval('import * as Foo from "foo.js"; Foo.foo();', filename: 'bar.js')

I'm trying to get my standalone js string processing library to be callable from within the server using miniracer in rails, and also to go through webpacker to be callable from js on the client. Webpacker seems to insist I use import/export to communicate among files. Miniracer seems to dislike import/export. I need them to both run from the same source tree, but I can't figure out how to make them speak the same dialect of js.

@bogdan
Copy link

bogdan commented Apr 29, 2021

I think if it would just do nothing, that would be better than an exception. This makes it easy to eval files written in ESM syntax.

Is there any way to prevent export usage from throwing an exception?

@tisba
Copy link
Collaborator

tisba commented Jul 12, 2021

I think if it would just do nothing, that would be better than an exception. This makes it easy to eval files written in ESM syntax.

Is there any way to prevent export usage from throwing an exception?

I think that would be a bad idea. Besides the fact that I don't know the first thing how to extend V8 with keywords/syntax, I think silently ignoring misuse is not good.

There are plenty of tools available, to compile ESM-based scripts into other forms like CommonJS. For example we're using esbuild but webpack should also do the job. CommonJS can be easily implemented with a wrapper in mini_racer directly (you only need to wire up a require and expose a modules.exports object.

Even better and more straight forward, is to bundle the scripts to be evaluated so you don't need any CommonJS, ESM etc at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants