#32319: Add support to HashedFilesMixin for ES modules -------------------------------------+------------------------------------- Reporter: gilmrjc | Owner: blighj Type: New feature | Status: closed Component: contrib.staticfiles | Version: dev Severity: Normal | Resolution: fixed Keywords: | Triage Stage: Ready for | checkin Has patch: 1 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Comment (by Michael):
Replying to [comment:16 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. 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. > 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. Enforcing the `;` for "safety" actually breaks import assertions, e.g.: {{{ import l from"/static/skin/skin/x-triangle.min.css"assert{type:"css"}; }}} -- Ticket URL: <https://code.djangoproject.com/ticket/32319#comment:24> 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 django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/0107018ed82349bb-ed9b1a32-cd92-4412-aae2-1fc50d02153f-000000%40eu-central-1.amazonses.com.