@@ -1835,17 +1835,25 @@ _flutter.loader.load();
18351835 'hashAndRenameWebOutput renames the binary with its content hash and pairs the source map' ,
18361836 () => testbed.run (() {
18371837 const jsContent = 'console.log("hello");\n //# sourceMappingURL=main.dart.js.map\n ' ;
1838+ const mapContent = '{"version":3,"sources":[]}' ;
18381839 final File jsFile = environment.buildDir.childFile ('main.dart.js' )
18391840 ..createSync (recursive: true )
18401841 ..writeAsStringSync (jsContent);
18411842 final File mapFile = environment.buildDir.childFile ('main.dart.js.map' )
18421843 ..createSync (recursive: true )
1843- ..writeAsStringSync ('{"version":3,"sources":[]}' );
1844+ ..writeAsStringSync (mapContent );
18441845
1845- // The hash is computed from the compiler's output, before the
1846- // sourceMappingURL comment is rewritten to the hashed map name.
1846+ final String expectedMapHash = crypto.sha256
1847+ .convert (utf8.encode (mapContent))
1848+ .toString ()
1849+ .substring (0 , 8 );
1850+ final expectedMapBasename = 'main.dart.$expectedMapHash .js.map' ;
1851+ final rewrittenJsContent =
1852+ 'console.log("hello");\n //# sourceMappingURL=$expectedMapBasename \n ' ;
1853+ // The binary hash is computed after rewriting the sourceMappingURL
1854+ // comment so the filename hash matches the final on-disk file bytes.
18471855 final String expectedHash = crypto.sha256
1848- .convert (utf8.encode (jsContent ))
1856+ .convert (utf8.encode (rewrittenJsContent ))
18491857 .toString ()
18501858 .substring (0 , 8 );
18511859
@@ -1855,33 +1863,38 @@ _flutter.loader.load();
18551863 final File renamedFile = environment.buildDir.childFile (newBasename);
18561864 expect (renamedFile, exists);
18571865
1858- // The map shares the binary's hash so that '<binary>.map' resolves , and
1859- // the binary's sourceMappingURL comment points at the renamed map.
1866+ // The map is hashed with its own content hash , and the binary's
1867+ // sourceMappingURL comment points at the renamed map.
18601868 expect (mapFile, isNot (exists));
1861- expect (environment.buildDir.childFile ('$ newBasename .map' ), exists);
1862- expect (renamedFile.readAsStringSync (), contains ( 'sourceMappingURL=$ newBasename .map' ) );
1869+ expect (environment.buildDir.childFile (expectedMapBasename ), exists);
1870+ expect (renamedFile.readAsStringSync (), rewrittenJsContent );
18631871 }),
18641872 );
18651873
18661874 test (
18671875 'Dart2JSTarget buildFiles includes the renamed source map' ,
18681876 () => testbed.run (() {
1869- // Binary and map contents differ, as in a real build; the map name must
1870- // still be discoverable from the binary name.
1877+ // Binary and map contents differ, as in a real build; the map name has
1878+ // its own content hash and must still be discoverable by buildFiles.
1879+ const mapContent = '{"version":3,"sources":["main.dart"]}' ;
18711880 final File jsFile = environment.buildDir.childFile ('main.dart.js' )
18721881 ..createSync (recursive: true )
18731882 ..writeAsStringSync ('console.log("hello");\n //# sourceMappingURL=main.dart.js.map\n ' );
18741883 final File mapFile = environment.buildDir.childFile ('main.dart.js.map' )
18751884 ..createSync (recursive: true )
1876- ..writeAsStringSync ('{"version":3,"sources":["main.dart"]}' );
1885+ ..writeAsStringSync (mapContent);
1886+ final String expectedMapHash = crypto.sha256
1887+ .convert (utf8.encode (mapContent))
1888+ .toString ()
1889+ .substring (0 , 8 );
18771890 final String newBasename = hashAndRenameWebOutput (file: jsFile, sourceMapFile: mapFile);
18781891
18791892 final target = Dart2JSTarget (const JsCompilerConfig (webContentHash: true ));
18801893 final List <String > files = target
18811894 .buildFiles (environment)
18821895 .map ((File f) => f.basename)
18831896 .toList ();
1884- expect (files, containsAll (< String > [newBasename, '$ newBasename .map' ]));
1897+ expect (files, containsAll (< String > [newBasename, 'main.dart.$ expectedMapHash .js .map' ]));
18851898 }),
18861899 );
18871900
@@ -1969,16 +1982,18 @@ _flutter.loader.load();
19691982 // Produce the mjs and its map with the real rename logic instead of
19701983 // hand-picked names, so the discovery logic is tested against what the
19711984 // build actually writes.
1985+ const mjsMapContent = '{"version":3,"sources":["main.dart"]}' ;
19721986 final File mjsFile = environment.buildDir.childFile ('main.dart.mjs' )
19731987 ..createSync (recursive: true )
19741988 ..writeAsStringSync ('export function main() {}\n //# sourceMappingURL=main.dart.mjs.map\n ' );
19751989 final File mjsMapFile = environment.buildDir.childFile ('main.dart.mjs.map' )
19761990 ..createSync (recursive: true )
1977- ..writeAsStringSync ('{"version":3,"sources":["main.dart"]}' );
1978- final String newMjsBasename = hashAndRenameWebOutput (
1979- file: mjsFile,
1980- sourceMapFile: mjsMapFile,
1981- );
1991+ ..writeAsStringSync (mjsMapContent);
1992+ final String expectedMjsMapHash = crypto.sha256
1993+ .convert (utf8.encode (mjsMapContent))
1994+ .toString ()
1995+ .substring (0 , 8 );
1996+ hashAndRenameWebOutput (file: mjsFile, sourceMapFile: mjsMapFile);
19821997
19831998 final target = Dart2WasmTarget (
19841999 const WasmCompilerConfig (webContentHash: true ),
@@ -1990,7 +2005,7 @@ _flutter.loader.load();
19902005 .map ((File f) => f.basename)
19912006 .toList ();
19922007 expect (files, contains (mapFile.basename));
1993- expect (files, contains ('$ newMjsBasename .map' ));
2008+ expect (files, contains ('main.dart.$ expectedMjsMapHash .mjs .map' ));
19942009 }),
19952010 );
19962011
@@ -2043,6 +2058,7 @@ _flutter.loader.load();
20432058 final File stalePartMap = environment.buildDir.childFile ('main.dart.js_1.part.js.map' )
20442059 ..createSync (recursive: true );
20452060 const jsContent = 'console.log("hello");\n //# sourceMappingURL=main.dart.js.map\n ' ;
2061+ const mapContent = '{"version":3,"sources":["main.dart"]}' ;
20462062 final common = < String > [
20472063 ..._kDart2jsLinuxArgs,
20482064 '-Ddart.vm.product=true' ,
@@ -2074,17 +2090,22 @@ _flutter.loader.load();
20742090 ],
20752091 onRun: (_) {
20762092 environment.buildDir.childFile ('main.dart.js' ).writeAsStringSync (jsContent);
2077- environment.buildDir
2078- .childFile ('main.dart.js.map' )
2079- .writeAsStringSync ('{"version":3,"sources":["main.dart"]}' );
2093+ environment.buildDir.childFile ('main.dart.js.map' ).writeAsStringSync (mapContent);
20802094 },
20812095 ),
20822096 );
20832097
20842098 await Dart2JSTarget (const JsCompilerConfig (webContentHash: true )).build (environment);
20852099
2100+ final String expectedMapHash = crypto.sha256
2101+ .convert (utf8.encode (mapContent))
2102+ .toString ()
2103+ .substring (0 , 8 );
2104+ final expectedMapBasename = 'main.dart.$expectedMapHash .js.map' ;
2105+ final rewrittenJsContent =
2106+ 'console.log("hello");\n //# sourceMappingURL=$expectedMapBasename \n ' ;
20862107 final String expectedHash = crypto.sha256
2087- .convert (utf8.encode (jsContent ))
2108+ .convert (utf8.encode (rewrittenJsContent ))
20882109 .toString ()
20892110 .substring (0 , 8 );
20902111 final expectedBasename = 'main.dart.$expectedHash .js' ;
@@ -2093,7 +2114,7 @@ _flutter.loader.load();
20932114 expect (stalePartJs, isNot (exists));
20942115 expect (stalePartMap, isNot (exists));
20952116 expect (environment.buildDir.childFile (expectedBasename), exists);
2096- expect (environment.buildDir.childFile ('$ expectedBasename .map' ), exists);
2117+ expect (environment.buildDir.childFile (expectedMapBasename ), exists);
20972118
20982119 final Depfile depfile = environment.depFileService.parse (
20992120 environment.buildDir.childFile ('dart2js.d' ),
@@ -2361,34 +2382,89 @@ const mapName = "main.dart.js.map";
23612382console.log(mapName);
23622383//# sourceMappingURL=main.dart.js.map
23632384''' ;
2385+ const mapContent = '{"version":3}' ;
23642386 final File jsFile = environment.buildDir.childFile ('main.dart.js' )
23652387 ..createSync (recursive: true )
23662388 ..writeAsStringSync (jsContent);
23672389 final File mapFile = environment.buildDir.childFile ('main.dart.js.map' )
23682390 ..createSync (recursive: true )
2369- ..writeAsStringSync ('{"version":3}' );
2391+ ..writeAsStringSync (mapContent );
23702392
2393+ final String expectedMapHash = crypto.sha256
2394+ .convert (utf8.encode (mapContent))
2395+ .toString ()
2396+ .substring (0 , 8 );
23712397 final String newBasename = hashAndRenameWebOutput (file: jsFile, sourceMapFile: mapFile);
23722398 final File renamedJs = environment.buildDir.childFile (newBasename);
23732399 expect (renamedJs.readAsStringSync (), contains ('const mapName = "main.dart.js.map";' ));
2374- expect (renamedJs.readAsStringSync (), contains ('//# sourceMappingURL=$newBasename .map' ));
2400+ expect (
2401+ renamedJs.readAsStringSync (),
2402+ contains ('//# sourceMappingURL=main.dart.$expectedMapHash .js.map' ),
2403+ );
23752404 }),
23762405 );
23772406
23782407 test (
23792408 'hashAndRenameWebOutput supports non-dart compound extensions like worker.js.map' ,
23802409 () => testbed.run (() {
2410+ const mapContent = '{"version":3}' ;
23812411 final File workerJs = environment.buildDir.childFile ('worker.js' )
23822412 ..createSync (recursive: true )
23832413 ..writeAsStringSync ('console.log("worker");\n //# sourceMappingURL=worker.js.map\n ' );
23842414 final File workerMap = environment.buildDir.childFile ('worker.js.map' )
23852415 ..createSync (recursive: true )
2386- ..writeAsStringSync ('{"version":3}' );
2416+ ..writeAsStringSync (mapContent );
23872417
2418+ final String expectedMapHash = crypto.sha256
2419+ .convert (utf8.encode (mapContent))
2420+ .toString ()
2421+ .substring (0 , 8 );
23882422 final String newBasename = hashAndRenameWebOutput (file: workerJs, sourceMapFile: workerMap);
23892423 expect (newBasename, matches (r'^worker\.[a-f0-9]{8}\.js$' ));
23902424 expect (workerMap.existsSync (), isFalse);
2391- expect (environment.buildDir.childFile ('$newBasename .map' ).existsSync (), isTrue);
2425+ expect (environment.buildDir.childFile ('worker.$expectedMapHash .js.map' ).existsSync (), isTrue);
2426+ }),
2427+ );
2428+
2429+ test (
2430+ 'hashAndRenameWebOutput with sourceMapFile produces matching hash and urlHashed: true in updatePrecacheManifest' ,
2431+ () => testbed.run (() {
2432+ final File jsFile = environment.outputDir.childFile ('main.dart.js' )
2433+ ..createSync (recursive: true )
2434+ ..writeAsStringSync ('console.log("hello");\n //# sourceMappingURL=main.dart.js.map\n ' );
2435+ final File mapFile = environment.outputDir.childFile ('main.dart.js.map' )
2436+ ..createSync (recursive: true )
2437+ ..writeAsStringSync ('{"version":3,"sources":["main.dart"]}' );
2438+ final File mjsFile = environment.outputDir.childFile ('main.dart.mjs' )
2439+ ..createSync (recursive: true )
2440+ ..writeAsStringSync ('export function main() {}\n //# sourceMappingURL=main.dart.mjs.map\n ' );
2441+ final File mjsMapFile = environment.outputDir.childFile ('main.dart.mjs.map' )
2442+ ..createSync (recursive: true )
2443+ ..writeAsStringSync ('{"version":3,"sources":["main.dart"]}' );
2444+
2445+ final String newJsBasename = hashAndRenameWebOutput (file: jsFile, sourceMapFile: mapFile);
2446+ final String newMjsBasename = hashAndRenameWebOutput (
2447+ file: mjsFile,
2448+ sourceMapFile: mjsMapFile,
2449+ );
2450+
2451+ final File manifestFile = updatePrecacheManifest (
2452+ environment.outputDir,
2453+ enabled: true ,
2454+ useLocalCanvasKit: false ,
2455+ )! ;
2456+ final decoded = jsonDecode (manifestFile.readAsStringSync ()) as Map <String , Object ?>;
2457+ final List <Map <String , Object ?>> entries = (decoded['entries' ]! as List <Object ?>)
2458+ .cast <Map <String , Object ?>>();
2459+
2460+ expect (entries, hasLength (2 ));
2461+ for (final entry in entries) {
2462+ final url = entry['url' ]! as String ;
2463+ final hash = entry['hash' ]! as String ;
2464+ expect (url, anyOf (newJsBasename, newMjsBasename));
2465+ expect (url, contains ('.$hash .' ));
2466+ expect (entry['urlHashed' ], isTrue);
2467+ }
23922468 }),
23932469 );
23942470
0 commit comments