8000 [AssetMapper] Ignore comment lines in JavaScriptImportPathCompiler by smnandre · Pull Request #53893 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[AssetMapper] Ignore comment lines in JavaScriptImportPathCompiler #53893

New issue

Have a question abo 8000 ut this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,31 @@
*/
final class JavaScriptImportPathCompiler implements AssetCompilerInterface
{
// https://regex101.com/r/qFoeoR/1
private const IMPORT_PATTERN = '/(?:\'(?:[^\'\\\\]|\\\\.)*\'|"(?:[^"\\\\]|\\\\.)*")|(?:import\s*(?:(?:\*\s*as\s+\w+|[\w\s{},*]+)\s*from\s*)?|\bimport\()\s*[\'"`](\.\/[^\'"`]+|(\.\.\/)*[^\'"`]+)[\'"`]\s*[;\)]?/m';
/**
* @see https://regex101.com/r/1iBAIb/1
*/
private const IMPORT_PATTERN = '/
^
(?:\/\/.*) # Lines that start with comments
|
(?:
\'(?:[^\'\\\\]|\\\\.)*\' # Strings enclosed in single quotes
|
"(?:[^"\\\\]|\\\\.)*" # Strings enclosed in double quotes
)
|
(?: # Import statements (script captured)
import\s*
(?:
(?:\*\s*as\s+\w+|\s+[\w\s{},*]+)
\s*from\s*
)?
|
\bimport\(
)
\s*[\'"`](\.\/[^\'"`]+|(\.\.\/)*[^\'"`]+)[\'"`]\s*[;\)]
?
/mx';

public function __construct(
private readonly ImportMapConfigReader $importMapConfigReader,
Expand All @@ -42,7 +65,7 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
return preg_replace_callback(self::IMPORT_PATTERN, function ($matches) use ($asset, $assetMapper, $content) {
$fullImportString = $matches[0][0];

// Ignore enquoted strings (e.g. console.log("import 'foo';")
// Ignore matches that did not capture import statements
if (!isset($matches[1][0])) {
return $fullImportString;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ public static function provideCompileTests(): iterable
'expectedJavaScriptImports' => ['/assets/other.js' => ['lazy' => false, 'asset' => 'other.js', 'add' => true]],
];

yield 'commented_import_on_one_line_then_module_name_on_next_is_not_ok' => [
'input' => "// import \n './other.js';",
'expectedJavaScriptImports' => [],
];

yield 'commented_import_on_one_line_then_import_on_next_is_ok' => [
'input' => "// import\nimport { Foo } from './other.js';",
'expectedJavaScriptImports' => ['/assets/other.js' => ['lazy' => false, 'asset' => 'other.js', 'add' => true]],
];

yield 'importing_a_css_file_is_included' => [
'input' => "import './styles.css';",
'expectedJavaScriptImports' => ['/assets/styles.css' => ['lazy' => false, 'asset' => 'styles.css', 'add' => true]],
Expand Down
0