Skip to content

Js interop and local vars #25033

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

Closed
Aetet opened this issue Nov 24, 2015 · 6 comments
Closed

Js interop and local vars #25033

Aetet opened this issue Nov 24, 2015 · 6 comments
Labels
closed-duplicate Closed in favor of an existing report web-js-interop Issues that impact all js interop

Comments

@Aetet
Copy link

Aetet commented Nov 24, 2015

Currently js interop works fine with global vars, but what about local vars through closure? If it will support it, then it will has clear exports and require. Because in all module systems that's just

function(require, exports, module){
// Here will be transpiled dart code
}

If in js interop we can access to local vars like: require, exports, module then we can make all require and exports clear to user like:

@JS('require')
external Request get requireCommon()

@JS()
class Request {
// definition for node request module
}

@JS('module.exports')
external void set exportsCommon(Exports exports)

@JS()
class Exports {
   external get String a;
}

Request request = requireCommon('request');
new Exports(a: 'Hello')

Or we can add new annotation for require:

@JSRequire('request')
class Request {}

There's some problem with new annotation is what will be cwd ? Current directory with dart file or with compiled dart file.

So we can use js modules like request in dart and exporting code from dart without syntax change.

@iposva-google iposva-google added the web-js-interop Issues that impact all js interop label Nov 24, 2015
@kevmoo
Copy link
Member

kevmoo commented Nov 24, 2015

@Aetet would you share the API you're trying to work with?

@Aetet
Copy link
Author

Aetet commented Nov 24, 2015

I build my code with webpack, which can understand CommonJS module.
In my js code I can do something like this:

import from '../dart/build/web/main.dart.js'

That's good. But I want export some vars from dart code. For example variable `a = 'Hello',
So in js code it will look like this:

import {a} from  '../dart/build/web/main.dart.js'

So webpack wrap compiled code of main.dart.js like this:

(function(modules) {
// Operation over modules
})([
// a lot of js modules
// And here will be compiled dart module
function(module, exports, __webpack_require__) {
eval("(function(){compiled dart code})()");
])

So if dart code can operate with module, exports, __webpack_require__ like it operates with global vars in js-interop layer, we can use all benefits from modern module systems like es6 without any pain. So the question is how achieve access to module, exports, __webpack_require__ from dart code without making them global?

@kevmoo
Copy link
Member

kevmoo commented Nov 24, 2015

@Aetet I think I understand. Our focus w/ this first set of work is to enable consumption of JS APIs from within a Dart app.

We want to be able to enable the creation of JS libraries from Dart, but that work will happen later.

I think your request is along those lines...

@jacob314 jacob314 added this to the Later milestone Jul 8, 2016
@Aetet
Copy link
Author

Aetet commented Oct 13, 2016

@jacob314 Is any update on this?

@kevmoo
Copy link
Member

kevmoo commented Nov 8, 2016

@jacob314 😃

@dgrove dgrove removed this from the Later milestone Mar 20, 2018
@jmesserly
Copy link

merging into #25059

@jmesserly jmesserly added the closed-duplicate Closed in favor of an existing report label Jun 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-duplicate Closed in favor of an existing report web-js-interop Issues that impact all js interop
Projects
None yet
Development

No branches or pull requests

6 participants