Changes between Initial Version and Version 1 of Ticket #32319, comment 24


Ignore:
Timestamp:
Apr 13, 2024, 10:53:49 AM (8 months ago)
Author:
Michael

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #32319, comment 24

    initial v1  
    11Replying to [comment:16 blighj]:
    2 > I think it is possible to do this with a regex and still work with the kinds of statements found in higlight.js from #33253. You can tighten up what is matched by being more specfic on the url it matches, based on the specs of what a browser will support for a module specifier.
    3 > https://v8.dev/features/modules#specifiers
    4 > >{{{
    5 > >// Supported:
    6 > >import {shout} from './lib.mjs';
    7 > >import {shout} from '../lib.mjs';
    8 > >import {shout} from '/modules/lib.mjs';
    9 > >import {shout} from 'https://simple.example/modules/lib.mjs';
    10 > >}}}
    11 > >For now, module specifiers must be full URLs, or relative URLs starting with /, ./, or ../.
    12 >
    13 > The collectstatic command should not be changing absolute URLs, only relative ones, so we can and an extra requirement to the (?P<url>) matcher so it must start with a dot or forward slash (?P<url>**[\.\/]**.*?)
    14 > This limits what the regex matches, so that the code types in highlight.js, and some other issues I've seen in videojs and ace, are no longer matched.
    15 > But it doesn't have to worry about all possible values of the import/export part of the expression, which would be very hard to do with regex.
    16 >
    17 > Here is what I'm using in my project for the js patterns
    18 > {{{
    19 >         (
    20 >             "*.js",
    21 >             (
    22 >                 (
    23 >                     r"""(?P<matched>import(?P<import>[\s\{].*?)\s*from\s*['"](?P<url>[\.\/].*?)["']\s*;)""",
    24 >                     """import%(import)s from "%(url)s";""",
    25 >                 ),
    26 >                 (
    27 >                     r"""(?P<matched>export(?P<exports>[\s\{].*?)\s*from\s*["'](?P<url>[\.\/].*?)["']\s*;)""",
    28 >                     """export%(exports)s from "%(url)s";""",
    29 >                 ),
    30 >                 (
    31 >                     r"""(?P<matched>import\s*['"](?P<url>[\.\/].*?)["']\s*;)""",
    32 >                     """import"%(url)s";""",
    33 >                 ),
    34 >             ),
    35 >         ),
    36 > }}}
    37 >
    382> I'm enforcing the need for a semicolon for extra safety, and I've added support for
    39 > {{{
    40 > import{shout}from './lib.mjs';
    41 > }}}
    42 > which was needed for some js coming out of esbuild.
    43 >
    44 > I don't know if {{{//}}} is supported by browsers for module specifiers, if it is then you'd have to update the regex to not capture those, I haven't needed it for my own projects yet, so I'm keeping my regex simpler for now.
    453
    464Enforcing the `;` for "safety" actually breaks import assertions, e.g.:
     
    497import l from"/static/skin/skin/x-triangle.min.css"assert{type:"css"};
    508}}}
     9
     10I think the following 2 steps will make it much for robust and cover much more use cases in modern web development:
     111. Remove the ending `;` from the regex's
     122. Add the extra regex from my previous post.
Back to Top