Expose loadId during deferred loading that will allow for advanced bundling optimizations.

Change-Id: Ic4e5edd8dc4761e49b4eb97892c75b869adfc2a7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/196980
Reviewed-by: Joshua Litt <joshualitt@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Commit-Queue: Joshua Litt <joshualitt@google.com>
diff --git a/pkg/compiler/lib/src/js_emitter/headers.dart b/pkg/compiler/lib/src/js_emitter/headers.dart
index 08fd664a..9953606 100644
--- a/pkg/compiler/lib/src/js_emitter/headers.dart
+++ b/pkg/compiler/lib/src/js_emitter/headers.dart
@@ -27,11 +27,12 @@
 //    directly. Instead, a closure that will invoke [main], and its arguments
 //    [args] is passed to [dartMainRunner].
 //
-// dartDeferredLibraryLoader(uri, successCallback, errorCallback):
+// dartDeferredLibraryLoader(uri, successCallback, errorCallback, loadId):
 //    if this function is defined, it will be called when a deferred library
 //    is loaded. It should load and eval the javascript of `uri`, and call
 //    successCallback. If it fails to do so, it should call errorCallback with
-//    an error.
+//    an error. The loadId argument is the deferred import that resulted in 
+//    this uri being loaded.
 //
 // dartCallInstrumentation(id, qualifiedName):
 //    if this function is defined, it will be called at each entry of a
diff --git a/sdk/lib/_internal/js_runtime/lib/js_helper.dart b/sdk/lib/_internal/js_runtime/lib/js_helper.dart
index cfaaeee..3f2302d 100644
--- a/sdk/lib/_internal/js_runtime/lib/js_helper.dart
+++ b/sdk/lib/_internal/js_runtime/lib/js_helper.dart
@@ -2736,7 +2736,7 @@
       waitingForLoad[i] = false;
       return new Future.value();
     }
-    return _loadHunk(uris[i]).then((Null _) {
+    return _loadHunk(uris[i], loadId).then((Null _) {
       waitingForLoad[i] = false;
       initializeSomeLoadedHunks();
     });
@@ -2833,7 +2833,7 @@
   throw new UnsupportedError('Cannot extract URI from "$stack"');
 }
 
-Future<Null> _loadHunk(String hunkName) {
+Future<Null> _loadHunk(String hunkName, String loadId) {
   var future = _loadingLibraries[hunkName];
   _eventLog.add(' - _loadHunk: $hunkName');
   if (future != null) {
@@ -2873,8 +2873,10 @@
 
   if (JS('bool', 'typeof # === "function"', deferredLibraryLoader)) {
     try {
-      JS('void', '#(#, #, #)', deferredLibraryLoader, uri, jsSuccess,
-          jsFailure);
+      // Share the loadId that hunk belongs to, this will allow for any
+      // additional loadId based bundling optimizations.
+      JS('void', '#(#, #, #, #)', deferredLibraryLoader, uri, jsSuccess,
+          jsFailure, loadId);
     } catch (error, stackTrace) {
       failure(error, "invoking dartDeferredLibraryLoader hook", stackTrace);
     }