Skip to content

DDC interop: allow me to control which APIs are exported from a module #27692

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

Open
nex3 opened this issue Oct 30, 2016 · 4 comments
Open

DDC interop: allow me to control which APIs are exported from a module #27692

nex3 opened this issue Oct 30, 2016 · 4 comments
Labels
area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop. customer-dart-sass type-enhancement A request for a change that isn't a bug web-js-interop Issues that impact all js interop

Comments

@nex3
Copy link
Member

nex3 commented Oct 30, 2016

When I'm using DDC to compile a module for use as a JavaScript module, I want to be able to control exactly what API is exposed, just as I would be able to if I were writing JavaScript natively. I don't want to expose all Dart types and fields that are transitively used by my module.

I propose that a flag, --entrypoint-only be added to DDC. This flag causes the generated module to export only those names that are exported by the Dart library(s) passed directly to the dev compiler, without any extra prefixes. It could also cause the compiler not to emit a summary file, since this JS module won't be able to be imported by other DDC-generated modules.

For example, consider the following two files:

// test1.dart
export 'test2.dart' show bar;
final foo = "foo";

// test2.dart
final bar = "bar";
final baz = "baz";

Currently, dartdevc --modules es6 -o test1.dart.js test1.dart emits

export const test1 = Object.create(null);
export const test2 = Object.create(null);
import { core, dart, dartx } from 'dart_sdk';
test1.foo = "foo";
test2.bar = "bar";
test1.bar = test2.bar;
test2.baz = "baz";

I'd like dartdevc --modules es6 -o test1.dart.js --entrypoint-only test1.dart to emit something like:

const test1 = Object.create(null);
export default test1;
const test2 = Object.create(null);
import { core, dart, dartx } from 'dart_sdk';
test1.foo = "foo";
test2.bar = "bar";
test1.bar = test2.bar;
test2.baz = "baz";
@nex3 nex3 added web-js-interop Issues that impact all js interop type-enhancement A request for a change that isn't a bug web-dev-compiler labels Oct 30, 2016
@nex3 nex3 changed the title DDC interop: allow me to control which names are exported from a module DDC interop: allow me to control which APIs are exported from a module Oct 30, 2016
@nex3
Copy link
Member Author

nex3 commented Oct 30, 2016

Eventually, this could also be used to seed tree-shaking for the module in question.

@jmesserly
Copy link

this is a great idea. We absolutely need a way to expose APIs directly on the module (instead of prefixed via library). I think in ES6 it will end up as the "default export" (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export)

@nex3
Copy link
Member Author

nex3 commented Feb 3, 2017

Currently I'm working around this by writing something like the following:

// # bin/sass.dart
export 'package:sass/src/executable.dart'
    if (node) 'package:sass/src/node.dart';

// #  lib/src/executable.dart
void main(List<String> args) {
  // ...
}

// # lib/src/node.dart
import 'package:js/js.dart';

import 'executable.dart' as executable;

@JS()
class _Exports {
  external set run_(function);
  // ...
}

@JS()
external _Exports get exports;

void main() {
  exports.run_ = allowInterop(executable.main);
  // ...
}

// # sass.js
var index = require('./sass.dart.js');

index.run_(process.argv.slice(2));

However, this seems to make the entire app run about 10% slower across the board than just directly compiling and running lib/src/executable.dart. That's a pretty serious perf hit for us.

@jmesserly
Copy link

However, this seems to make the entire app run about 10% slower across the board than just directly compiling and running lib/src/executable.dart. That's a pretty serious perf hit for us.

that's with dart2js?

@vsmenon vsmenon added the area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop. label Jul 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop. customer-dart-sass type-enhancement A request for a change that isn't a bug web-js-interop Issues that impact all js interop
Projects
None yet
Development

No branches or pull requests

4 participants