@@ -130,13 +130,14 @@ Future<CompilerResult> _compile(List<String> args,
130130 // the correct location and keeps the real file location hidden from the
131131 // front end.
132132 var multiRootScheme = argResults['multi-root-scheme' ] as String ;
133+ var multiRootPaths = (argResults['multi-root' ] as Iterable <String >)
134+ .map (Uri .base .resolve)
135+ .toList ();
136+ var multiRootOutputPath =
137+ _longestPrefixingPath (path.absolute (output), multiRootPaths);
133138
134139 var fileSystem = MultiRootFileSystem (
135- multiRootScheme,
136- (argResults['multi-root' ] as Iterable <String >)
137- .map (Uri .base .resolve)
138- .toList (),
139- fe.StandardFileSystem .instance);
140+ multiRootScheme, multiRootPaths, fe.StandardFileSystem .instance);
140141
141142 Uri toCustomUri (Uri uri) {
142143 if (uri.scheme == '' ) {
@@ -354,7 +355,8 @@ Future<CompilerResult> _compile(List<String> args,
354355 jsUrl: path.toUri (output).toString (),
355356 mapUrl: path.toUri (output + '.map' ).toString (),
356357 bazelMapping: options.bazelMapping,
357- customScheme: multiRootScheme);
358+ customScheme: multiRootScheme,
359+ multiRootOutputPath: multiRootOutputPath);
358360
359361 outFiles.add (file.writeAsString (jsCode.code));
360362 if (jsCode.sourceMap != null ) {
@@ -390,7 +392,8 @@ JSCode jsProgramToCode(JS.Program moduleTree, ModuleFormat format,
390392 String jsUrl,
391393 String mapUrl,
392394 Map <String , String > bazelMapping,
393- String customScheme}) {
395+ String customScheme,
396+ String multiRootOutputPath}) {
394397 var opts = JS .JavaScriptPrintingOptions (
395398 allowKeywordsInProperties: true , allowSingleLineIfStatements: true );
396399 JS .SimpleJavaScriptPrintingContext printer;
@@ -409,7 +412,8 @@ JSCode jsProgramToCode(JS.Program moduleTree, ModuleFormat format,
409412 Map builtMap;
410413 if (buildSourceMap && sourceMap != null ) {
411414 builtMap = placeSourceMap (
412- sourceMap.build (jsUrl), mapUrl, bazelMapping, customScheme);
415+ sourceMap.build (jsUrl), mapUrl, bazelMapping, customScheme,
416+ multiRootOutputPath: multiRootOutputPath);
413417 var jsDir = path.dirname (path.fromUri (jsUrl));
414418 var relative = path.relative (path.fromUri (mapUrl), from: jsDir);
415419 var relativeMapUrl = path.toUri (relative).toString ();
@@ -505,3 +509,14 @@ String _findPackagesFilePath() {
505509 dir = parent;
506510 }
507511}
512+
513+ /// Inputs must be absolute paths. Returns null if no prefixing path is found.
514+ String _longestPrefixingPath (String basePath, List <Uri > prefixingPaths) {
515+ return prefixingPaths.fold (null , (String previousValue, Uri element) {
516+ if (basePath.startsWith (element.path) &&
517+ (previousValue == null || previousValue.length < element.path.length)) {
518+ return element.path;
519+ }
520+ return previousValue;
521+ });
522+ }
0 commit comments