#32319: Add support to HashedFilesMixin for ES modules
-------------------------------------+------------------------------------
Reporter: gilmrjc | Owner: gilmrjc
Type: New feature | Status: new
Component: contrib.staticfiles | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+------------------------------------
Comment (by blighj):
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. By being more
specfic on the url it matches, based on the specs of what a browser will
support for a module specifier.
https://v8.dev/features/modules#specifiers
>{{{
>// Supported:
>import {shout} from './lib.mjs';
>import {shout} from '../lib.mjs';
>import {shout} from '/modules/lib.mjs';
>import {shout} from 'https://simple.example/modules/lib.mjs';
>}}}
>For now, module specifiers must be full URLs, or relative URLs starting
with /, ./, or ../.
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>**[\.\/]**.*?)
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.
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.
Here is what I'm using in my project for the js patterns
{{{
(
"*.js",
(
(
r"""(?P<matched>import(?P<import>[\s\{].*?)\s*from\s*['"](?P<url>[\.\/].*?)["']\s*;)""",
"""import%(import)s from "%(url)s";""",
),
(
r"""(?P<matched>export(?P<exports>[\s\{].*?)\s*from\s*["'](?P<url>[\.\/].*?)["']\s*;)""",
"""export%(exports)s from "%(url)s";""",
),
(
r"""(?P<matched>import\s*['"](?P<url>[\.\/].*?)["']\s*;)""",
"""import"%(url)s";""",
),
),
),
}}}
I'm enforcing the need for a semicolon for extra safety, and I've added
support for
{{{
import{shout}from './lib.mjs';
}}}
which was needed for some js coming out of esbuild.
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/32319#comment:16>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070183e01cb29e-8966e075-f48a-401f-adee-772479f6c945-000000%40eu-central-1.amazonses.com.