| 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 | | > |