10000 [AssetMapper] Fixing js sourceMappingURL extraction when sourceMappin… · symfony/symfony@b3635e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit b3635e3

Browse files
weaverryannicolas-grekas
authored andcommitted
[AssetMapper] Fixing js sourceMappingURL extraction when sourceMappingURL used in code
1 parent 6c91073 commit b3635e3

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ private function makeImportsBare(string $content, array &$dependencies): string
248248
}, $content);
249249

250250
// source maps are not also downloaded - so remove the sourceMappingURL
251-
$content = preg_replace('{//# sourceMappingURL=.*$}m', '', $content);
251+
// remove the final one only (in case sourceMappingURL is used in the code)
252+
if (false !== $lastPos = strrpos($content, '//# sourceMappingURL=')) {
253+
$content = substr($content, 0, $lastPos).preg_replace('{//# sourceMappingURL=.*$}m', '', substr($content, $lastPos));
254+
}
252255

253256
return preg_replace('{/\*# sourceMappingURL=[^ ]*+ \*/}', '', $content);
254257
}

src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,30 @@ public static function provideDownloadPackagesTests()
430430
],
431431
];
432432

433-
yield 'css file removes importmap' => [
433+
yield 'js sourcemap is correctly removed when sourceMapping appears in the JS' => [
434+
[
435+
'es-module-shims' => self::createRemoteEntry('es-module-shims', version: '1.8.2'),
436+
],
437+
[
438+
[
439+
'url' => '/es-module-shims@1.8.2/+esm',
440+
'body' => <<<'EOF'
441+
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};
442+
//# sourceMappingURL=/sm/ef3916de598f421a779ba0e69af94655b2043095cde2410cc01893452d893338.map
443+
EOF
444+
],
445+
],
446+
[
447+
'es-module-shims' => [
448+
'content' => <<<'EOF'
449+
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};
450+
EOF,
451+
'dependencies' => [],
452+
],
453+
],
454+
];
455+
456+
yield 'css file removes sourcemap' => [
434457
['lodash' => self::createRemoteEntry('bootstrap/dist/bootstrap.css', version: '5.0.6', type: ImportMapType::CSS)],
435458
[
436459
[

0 commit comments

Comments
 (0)
0