diff --git a/CHANGELOG.md b/CHANGELOG.md index 6119b0c..26e8798 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/src/npm.dart b/lib/src/npm.dart index bceae4b..5c5cb86 100644 --- a/lib/src/npm.dart +++ b/lib/src/npm.dart @@ -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;"); } diff --git a/pubspec.yaml b/pubspec.yaml index 144954b..3c11f95 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 diff --git a/test/npm_test.dart b/test/npm_test.dart index 89ff111..e83c810 100644 --- a/test/npm_test.dart +++ b/test/npm_test.dart @@ -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'; @@ -104,46 +105,92 @@ void main() { .validate(); }); - test("exports from jsModuleMainLibrary can be imported", () async { - await d.package(pubspec, """ - void main(List 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 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 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 {