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

Use package: imports to load jsModuleMainLibrary #84

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
* `pkg-chocolatey` now creates a file named `LICENSE.txt` rather than `LICENSE`,
at the request of the Chocolatey reviewers.

* Allow the `pkg.jsModuleMainLibrary` path to be absolute rather than requiring
it to be relative to the package root.

* Load `pkg.jsModuleMainLibrary` using a `package:` URL. While this shouldn't
affect behavior at all, it will cause dart2js to generate less code if the
executables *also* properly import libraries in the `lib` directory using
`package:` URLs.

# 1.4.0

* Add `pkg.environmentConstants` to make it possible to define custom
Expand Down
9 changes: 6 additions & 3 deletions lib/src/npm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,12 @@ String get _wrapperLibrary {
var import = jsonEncode(p.toUri(p.join('..', path)).toString());
wrapper.writeln("import $import as ${_executableIdentifiers[name]};");
});
if (jsModuleMainLibrary.value != null) {
var target =
jsonEncode(p.toUri(p.join('..', jsModuleMainLibrary.value)).toString());

var mainLibrary = jsModuleMainLibrary.value;
if (mainLibrary != null) {
var target = jsonEncode(p.isWithin("lib", mainLibrary)
? "package:$dartName/${p.toUri(p.relative(mainLibrary, from: "lib"))}"
: p.toUri(p.relative(mainLibrary, from: "build")).toString());
wrapper.writeln("import $target as module_main;");
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: cli_pkg
version: 1.4.1-dev
version: 1.4.1
description: Grinder tasks for releasing Dart CLI packages.
homepage: https://github.com/google/dart_cli_pkg

Expand Down
117 changes: 82 additions & 35 deletions test/npm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import 'dart:convert';

import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import 'package:test_process/test_process.dart';

Expand Down Expand Up @@ -104,46 +105,92 @@ void main() {
.validate();
});

test("exports from jsModuleMainLibrary can be imported", () async {
await d.package(pubspec, """
void main(List<String> args) {
pkg.jsModuleMainLibrary.value = "lib/src/exports.dart";
group("exports from jsModuleMainLibrary can be imported", () {
setUp(() async {
await d.package(pubspec, "", [
_packageJson,
d.dir("lib/src", [
d.file("exports.dart", """
import 'package:js/js.dart';

@JS()
class Exports {
external set hello(String value);
}

pkg.addNpmTasks();
grind(args);
}
""", [
_packageJson,
d.dir("lib/src", [
d.file("exports.dart", """
import 'package:js/js.dart';

@JS()
class Exports {
external set hello(String value);
}

@JS()
external Exports get exports;

void main() {
exports.hello = "Hi, there!";
}
""")
])
]).create();
@JS()
external Exports get exports;

await (await grind(["pkg-js-dev"])).shouldExit();
void main() {
exports.hello = "Hi, there!";
}
""")
])
]).create();
});

test("with a relative path", () async {
await d.dir(p.basename(appDir), [
d.dir("tool", [
d.file("grind.dart", """
import 'package:cli_pkg/cli_pkg.dart' as pkg;
import 'package:grinder/grinder.dart';

void main(List<String> args) {
pkg.jsModuleMainLibrary.value = "lib/src/exports.dart";

pkg.addNpmTasks();
grind(args);
}
""")
])
]).create();

await d.file("test.js", """
var my_app = require("./my_app/build/my_app.dart.js");
await (await grind(["pkg-js-dev"])).shouldExit();

console.log(my_app.hello);
""").create();
await d.file("test.js", """
var my_app = require("./my_app/build/my_app.dart.js");

var process = await TestProcess.start("node$dotExe", [d.path("test.js")]);
expect(process.stdout, emitsInOrder(["Hi, there!", emitsDone]));
await process.shouldExit(0);
console.log(my_app.hello);
""").create();

var process =
await TestProcess.start("node$dotExe", [d.path("test.js")]);
expect(process.stdout, emitsInOrder(["Hi, there!", emitsDone]));
await process.shouldExit(0);
});

test("with an absolute path", () async {
await d.dir(p.basename(appDir), [
d.dir("tool", [
d.file("grind.dart", """
import 'package:cli_pkg/cli_pkg.dart' as pkg;
import 'package:grinder/grinder.dart';
import 'package:path/path.dart' as p;

void main(List<String> args) {
pkg.jsModuleMainLibrary.value = p.absolute("lib/src/exports.dart");

pkg.addNpmTasks();
grind(args);
}
""")
])
]).create();

await (await grind(["pkg-js-dev"])).shouldExit();

await d.file("test.js", """
var my_app = require("./my_app/build/my_app.dart.js");

console.log(my_app.hello);
""").create();

var process =
await TestProcess.start("node$dotExe", [d.path("test.js")]);
expect(process.stdout, emitsInOrder(["Hi, there!", emitsDone]));
await process.shouldExit(0);
});
});

test("takes its name from the package.json name field", () async {
Expand Down