[Django] #35553: HashedFilesMixin for ES modules does not work with `import * as ...` syntax

15 views
Skip to first unread message

Django

unread,
Jun 23, 2024, 10:20:01 AM6/23/24
to django-...@googlegroups.com
#35553: HashedFilesMixin for ES modules does not work with `import * as ...` syntax
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: nobody
Type: | Status: new
Uncategorized |
Component: | Version: 5.0
contrib.staticfiles | Keywords: Manifest Static
Severity: Normal | Files Storage, javascript module
Triage Stage: | scripts
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Django's regex does not work with the following:

{{{
import*as l from "/static/jsapp/jsapp/dtmod.min.js";import*as h from
"/static/jsapp/jsapp/nummod.min.js";import*as m from
"/static/leave/jsapp/fetcher.min.js";import {BaseComponent as g} from
"/static/wcapp/jsapp/wc-base.min.425310100bce.js";
}}}

As you can see only the 4th import was correctly altered, the first 3
werent even detected, (below is the same as above but placed the imports
on seprate lines for readability):

{{{
import*as l from "/static/jsapp/jsapp/dtmod.min.js";
import*as h from "/static/jsapp/jsapp/nummod.min.js";
import*as m from "/static/leave/jsapp/fetcher.min.js";
import {BaseComponent as g} from "/static/wcapp/jsapp/wc-
base.min.425310100bce.js";
}}}


This regex handles the missing case:
{{{
(
r"""(?P<matched>(?P<import_as>import\s*\*as\s\S+)\s+from\s*["'](?P<url>[./].*?)["']\s*;)""",
"""%(import_as)s from "%(url)s";""",
),
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35553>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 24, 2024, 3:42:43 AM6/24/24
to django-...@googlegroups.com
#35553: HashedFilesMixin for ES modules does not work with `import * as ...` syntax
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: nobody
Type: Uncategorized | Status: closed
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: Manifest Static | Triage Stage:
Files Storage, javascript module | Unreviewed
scripts |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* resolution: => invalid
* status: new => closed

Comment:

Replying to [ticket:35553 Michael]:
> {{{
> import*as l from "/static/jsapp/jsapp/dtmod.min.js";
> import*as h from "/static/jsapp/jsapp/nummod.min.js";
> import*as m from "/static/leave/jsapp/fetcher.min.js";
> import {BaseComponent as g} from "/static/wcapp/jsapp/wc-
base.min.425310100bce.js";
> }}}

This doesn't look valid to me, shouldn't it be `import * as` not
`import*as`?
--
Ticket URL: <https://code.djangoproject.com/ticket/35553#comment:1>

Django

unread,
Jun 24, 2024, 5:35:11 AM6/24/24
to django-...@googlegroups.com
#35553: HashedFilesMixin for ES modules does not work with `import * as ...` syntax
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: nobody
Type: Uncategorized | Status: closed
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: Manifest Static | Triage Stage:
Files Storage, javascript module | Unreviewed
scripts |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Michael):

Hi, no it is not invalid, that is the result of minification, removing any
extra whitespace, most production system will serve minified files, so
it's much more likely to not have the space.
--
Ticket URL: <https://code.djangoproject.com/ticket/35553#comment:2>

Django

unread,
Jun 24, 2024, 6:40:03 AM6/24/24
to django-...@googlegroups.com
#35553: HashedFilesMixin for ES modules does not work with `import * as ...` syntax
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: nobody
Type: Bug | Status: new
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Resolution:
Keywords: Manifest Static | Triage Stage: Accepted
Files Storage, javascript module |
scripts |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* resolution: invalid =>
* stage: Unreviewed => Accepted
* status: closed => new
* type: Uncategorized => Bug

Comment:

Replying to [comment:2 Michael]:
> Hi, no it is not invalid, that is the result of minification, removing
any extra whitespace, most production system will serve minified files, so
it's much more likely to not have the space.

Ah, TIL 😁 confirmed that this is the output of many minifiers

Confirmed that your suggested patch works for me, here is also a test:
{{{#!diff
diff --git a/tests/staticfiles_tests/project/documents/cached/module.js
b/tests/staticfiles_tests/project/documents/cached/module.js
index 7764e740d6..30ca25e9b6 100644
--- a/tests/staticfiles_tests/project/documents/cached/module.js
+++ b/tests/staticfiles_tests/project/documents/cached/module.js
@@ -2,6 +2,7 @@
import rootConst from "/static/absolute_root.js";
import testConst from "./module_test.js";
import * as NewModule from "./module_test.js";
+import*as m from"./module_test.js";
import { testConst as alias } from "./module_test.js";
import { firstConst, secondConst } from "./module_test.js";
import {
diff --git a/tests/staticfiles_tests/test_storage.py
b/tests/staticfiles_tests/test_storage.py
index 469d5ec690..956341a858 100644
--- a/tests/staticfiles_tests/test_storage.py
+++ b/tests/staticfiles_tests/test_storage.py
@@ -674,7 +674,7 @@ class
TestCollectionJSModuleImportAggregationManifestStorage(CollectionTestCase)

def test_module_import(self):
relpath = self.hashed_file_path("cached/module.js")
- self.assertEqual(relpath, "cached/module.55fd6938fbc5.js")
+ self.assertEqual(relpath, "cached/module.d16a17156de1.js")
tests = [
# Relative imports.
b'import testConst from "./module_test.477bbebe77f0.js";',
@@ -686,6 +686,8 @@ class
TestCollectionJSModuleImportAggregationManifestStorage(CollectionTestCase)
b'const dynamicModule =
import("./module_test.477bbebe77f0.js");',
# Creating a module object.
b'import * as NewModule from
"./module_test.477bbebe77f0.js";',
+ # Creating a minified module object.
+ b'import*as m from "./module_test.477bbebe77f0.js";',
# Aliases.
b'import { testConst as alias } from
"./module_test.477bbebe77f0.js";',
b"import {\n"
}}}

Would you like to raise a PR?
--
Ticket URL: <https://code.djangoproject.com/ticket/35553#comment:3>

Django

unread,
Jun 24, 2024, 9:24:07 AM6/24/24
to django-...@googlegroups.com
#35553: HashedFilesMixin for ES modules does not work with `import*as ...` syntax
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: nobody
Type: Bug | Status: new
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Resolution:
Keywords: Manifest Static | Triage Stage: Accepted
Files Storage, javascript module |
scripts |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Michael):

Okay great, thanks for handling it. I dont know what "TIL" means, but
cheers!
--
Ticket URL: <https://code.djangoproject.com/ticket/35553#comment:5>

Django

unread,
Jun 24, 2024, 10:30:31 AM6/24/24
to django-...@googlegroups.com
#35553: HashedFilesMixin for ES modules does not work with `import*as ...` syntax
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: nobody
Type: Bug | Status: new
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Resolution:
Keywords: Manifest Static | Triage Stage: Accepted
Files Storage, javascript module |
scripts |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce):

Replying to [comment:5 Michael]:
> Okay great, thanks for handling it. I dont know what "TIL" means, but
cheers!

Sorry it's short for "Today I learned"
--
Ticket URL: <https://code.djangoproject.com/ticket/35553#comment:6>

Django

unread,
Jun 27, 2024, 1:13:31 PM6/27/24
to django-...@googlegroups.com
#35553: HashedFilesMixin for ES modules does not work with `import*as ...` syntax
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: Farhan
| Ali
Type: Bug | Status: assigned
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Resolution:
Keywords: Manifest Static | Triage Stage: Accepted
Files Storage, javascript module |
scripts |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Farhan Ali):

* owner: nobody => Farhan Ali
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/35553#comment:7>

Django

unread,
Jun 27, 2024, 4:11:59 PM6/27/24
to django-...@googlegroups.com
#35553: HashedFilesMixin for ES modules does not work with `import*as ...` syntax
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: Farhan
| Ali
Type: Bug | Status: assigned
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Resolution:
Keywords: Manifest Static | Triage Stage: Accepted
Files Storage, javascript module |
scripts |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Farhan Ali):

output of this produces `import *as m from
"./module_test.477bbebe77f0.477bbebe77f0.js";` for me.
I don't know why.
--
Ticket URL: <https://code.djangoproject.com/ticket/35553#comment:8>

Django

unread,
Jun 27, 2024, 4:38:41 PM6/27/24
to django-...@googlegroups.com
#35553: HashedFilesMixin for ES modules does not work with `import*as ...` syntax
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: Farhan
| Ali
Type: Bug | Status: assigned
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Resolution:
Keywords: Manifest Static | Triage Stage: Accepted
Files Storage, javascript module |
scripts |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Farhan Ali):

{{{#!diff

diff --git a/django/contrib/staticfiles/storage.py
b/django/contrib/staticfiles/storage.py
index 85172ea42d..394975c9de 100644
--- a/django/contrib/staticfiles/storage.py
+++ b/django/contrib/staticfiles/storage.py
@@ -73,6 +73,10 @@ class HashedFilesMixin:
r"""(?P<matched>import\(["'](?P<url>.*?)["']\))""",
"""import("%(url)s")""",
),
+ (
+
r"""(?P<matched>(?P<import_as>import\s*\*as\s\S+)\s+from\s*["'](?P<url>[./].*?)["']\s*;)""",
+ """%(import_as)s from "%(url)s";""",
+ ),
),
)
patterns = (
@@ -287,12 +291,10 @@ class HashedFilesMixin:

# where to store the new paths
hashed_files = {}
-
# build a list of adjustable files
adjustable_paths = [
path for path in paths if matches_patterns(path,
self._patterns)
]
-
# Adjustable files to yield at end, keyed by the original path.
processed_adjustable_paths = {}

diff --git a/tests/staticfiles_tests/project/documents/cached/module.js
b/tests/staticfiles_tests/project/documents/cached/module.js
index 7764e740d6..602561798f 100644
--- a/tests/staticfiles_tests/project/documents/cached/module.js
+++ b/tests/staticfiles_tests/project/documents/cached/module.js
@@ -2,6 +2,7 @@
import rootConst from "/static/absolute_root.js";
import testConst from "./module_test.js";
import * as NewModule from "./module_test.js";
+import *as m from "./module_test.js";
import { testConst as alias } from "./module_test.js";
import { firstConst, secondConst } from "./module_test.js";
import {
diff --git a/tests/staticfiles_tests/test_storage.py
b/tests/staticfiles_tests/test_storage.py
index dc8607a307..6290d9d51a 100644
--- a/tests/staticfiles_tests/test_storage.py
+++ b/tests/staticfiles_tests/test_storage.py
@@ -643,7 +643,7 @@ class
TestCollectionJSModuleImportAggregationManifestStorage(CollectionTestCase)

def test_module_import(self):
relpath = self.hashed_file_path("cached/module.js")
- self.assertEqual(relpath, "cached/module.55fd6938fbc5.js")
+ self.assertEqual(relpath, "cached/module.0415cd43ac63.js")
tests = [
# Relative imports.
b'import testConst from "./module_test.477bbebe77f0.js";',
@@ -655,6 +655,8 @@ class
TestCollectionJSModuleImportAggregationManifestStorage(CollectionTestCase)
b'const dynamicModule =
import("./module_test.477bbebe77f0.js");',
# Creating a module object.
b'import * as NewModule from
"./module_test.477bbebe77f0.js";',
+ # Creating a minified module object.
+ b'import*as m from "./module_test.477bbebe77f0.js";',
# Aliases.
b'import { testConst as alias } from
"./module_test.477bbebe77f0.js";',
b"import {\n"
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35553#comment:9>

Django

unread,
Aug 2, 2024, 4:28:04 PM8/2/24
to django-...@googlegroups.com
#35553: HashedFilesMixin for ES modules does not work with `import*as ...` syntax
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: Farhan
| Ali
Type: Bug | Status: assigned
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Resolution:
Keywords: Manifest Static | Triage Stage: Accepted
Files Storage, javascript module |
scripts |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Farhan Ali):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35553#comment:10>

Django

unread,
Aug 5, 2024, 3:01:02 AM8/5/24
to django-...@googlegroups.com
#35553: HashedFilesMixin for ES modules does not work with `import*as ...` syntax
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: Farhan
| Ali
Type: Bug | Status: assigned
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Resolution:
Keywords: Manifest Static | Triage Stage: Accepted
Files Storage, javascript module |
scripts |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35553#comment:11>

Django

unread,
Aug 7, 2024, 4:07:17 AM8/7/24
to django-...@googlegroups.com
#35553: HashedFilesMixin for ES modules does not work with `import*as ...` syntax
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: Farhan
| Ali
Type: Bug | Status: assigned
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Resolution:
Keywords: Manifest Static | Triage Stage: Ready for
Files Storage, javascript module | checkin
scripts |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/35553#comment:12>

Django

unread,
Aug 7, 2024, 5:02:07 AM8/7/24
to django-...@googlegroups.com
#35553: HashedFilesMixin for ES modules does not work with `import*as ...` syntax
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: Farhan
| Ali
Type: Bug | Status: closed
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Resolution: fixed
Keywords: Manifest Static | Triage Stage: Ready for
Files Storage, javascript module | checkin
scripts |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"6993c9d8c962503cd60a37ad7f9569c06c13516e" 6993c9d8]:
{{{#!CommitTicketReference repository=""
revision="6993c9d8c962503cd60a37ad7f9569c06c13516e"
Fixed #35553 -- Handled import*as in HashedFilesMixin.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35553#comment:13>
Reply all
Reply to author
Forward
0 new messages