Skip to content

[AssetMapper] Wrong javascript import detection #53608

@mtarld

Description

@mtarld

Symfony version(s) affected

6.3

Description

Asset mapper unexpectedly detects some JavaScript imports. Indeed, let's say we have the following vanilla.js JS file:

console.warn("[DEPRECATED] Default export is deprecated. Instead use import { createStore } from 'zustand/vanilla'.");

Then it'll end in an infinite loop as the file is requiring itself.

How to reproduce

Start a fresh project, require zustand@4.5.0, then run bin/console asset-map:compile, you should end in an infinite loop.

Possible Solution

Update the Symfony\Component\AssetMapper\Compiler\JavaScriptImportPathCompiler::IMPORT_PATTERN so that it detects imports better.

As a quickfix, I updated it like the following (but I'm sure there is a better way):

- /(?:import\s*(?:(?:\*\s*as\s+\w+|[\w\s{},*]+)\s*from\s*)?|\bimport\()\s*[\'"`](\.\/[^\'"`]+|(\.\.\/)*[^\'"`]+)[\'"`]\s*[;\)]?/m
+ /(?:[^ ]import\s*(?:(?:\*\s*as\s+\w+|[\w\s{},*]+)\s*from\s*)?|\bimport\()\s*[\'"`](\.\/[^\'"`]+|(\.\.\/)*[^\'"`]+)[\'"`]\s*[;\)]?/m

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions