From b3635e37651ae5169c250322af249c6a41213e9c Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Mon, 13 Nov 2023 09:35:02 -0500 Subject: [PATCH] [AssetMapper] Fixing js sourceMappingURL extraction when sourceMappingURL used in code --- .../Resolver/JsDelivrEsmResolver.php | 5 +++- .../Resolver/JsDelivrEsmResolverTest.php | 25 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php b/src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php index dae1a3d1a8656..bbc9199cc7c08 100644 --- a/src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php +++ b/src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php @@ -248,7 +248,10 @@ private function makeImportsBare(string $content, array &$dependencies): string }, $content); // source maps are not also downloaded - so remove the sourceMappingURL - $content = preg_replace('{//# sourceMappingURL=.*$}m', '', $content); + // remove the final one only (in case sourceMappingURL is used in the code) + if (false !== $lastPos = strrpos($content, '//# sourceMappingURL=')) { + $content = substr($content, 0, $lastPos).preg_replace('{//# sourceMappingURL=.*$}m', '', substr($content, $lastPos)); + } return preg_replace('{/\*# sourceMappingURL=[^ ]*+ \*/}', '', $content); } diff --git a/src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php b/src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php index 1c2ac78d0d2f4..121e80a3a0b3a 100644 --- a/src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php +++ b/src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php @@ -430,7 +430,30 @@ public static function provideDownloadPackagesTests() ], ]; - yield 'css file removes importmap' => [ + yield 'js sourcemap is correctly removed when sourceMapping appears in the JS' => [ + [ + 'es-module-shims' => self::createRemoteEntry('es-module-shims', version: '1.8.2'), + ], + [ + [ + 'url' => '/es-module-shims@1.8.2/+esm', + 'body' => <<<'EOF' +const je="\n//# sourceURL=",Ue="\n//# sourceMappingURL=",Me=/^(text|application)\/(x-)?javascript(;|$)/,_e=/^(application)\/wasm(;|$)/,Ie=/^(text|application)\/json(;|$)/,Re=/^(text|application)\/css(;|$)/,Te=/url\(\s*(?:(["'])((?:\\.|[^\n\\"'])+)\1|((?:\\.|[^\s,"'()\\])+))\s*\)/g;export{t as default}; +//# sourceMappingURL=/sm/ef3916de598f421a779ba0e69af94655b2043095cde2410cc01893452d893338.map +EOF + ], + ], + [ + 'es-module-shims' => [ + 'content' => <<<'EOF' +const je="\n//# sourceURL=",Ue="\n//# sourceMappingURL=",Me=/^(text|application)\/(x-)?javascript(;|$)/,_e=/^(application)\/wasm(;|$)/,Ie=/^(text|application)\/json(;|$)/,Re=/^(text|application)\/css(;|$)/,Te=/url\(\s*(?:(["'])((?:\\.|[^\n\\"'])+)\1|((?:\\.|[^\s,"'()\\])+))\s*\)/g;export{t as default}; +EOF, + 'dependencies' => [], + ], + ], + ]; + + yield 'css file removes sourcemap' => [ ['lodash' => self::createRemoteEntry('bootstrap/dist/bootstrap.css', version: '5.0.6', type: ImportMapType::CSS)], [ [