Skip to content

Commit b07551c

Browse files
committed
Add JS rewriting rule to ignore import function rewriting
1 parent aa720ad commit b07551c

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/zimscraperlib/rewriting/js.py

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ def create_js_rules() -> list[TransformationRule]:
186186
# As the rule will match first, it will prevent next rule matching `import` to
187187
# be apply to `async import`.
188188
(re.compile(r"async\s+import\s*\("), m2str(lambda x: x)),
189+
(re.compile(r"[^$.]\bimport\s*\([^)]*\)\s*\{"), m2str(lambda x: x)),
189190
# esm dynamic import, if found, mark as module
190191
(
191192
re.compile(r"[^$.]\bimport\s*\("),

tests/rewriting/test_js_rewriting.py

+13
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ def wrap_import(text: str) -> str:
271271
import { Z } from "../../../path.js";
272272
273273
B = await import(somefile);
274+
275+
class X {
276+
import(a, b, c) {
277+
await import (somefile);
278+
}
279+
}
274280
""",
275281
expected="""
276282
import * from "../../../example.com/file.js"
@@ -282,6 +288,12 @@ def wrap_import(text: str) -> str:
282288
import { Z } from "../../path.js";
283289
284290
B = await ____wb_rewrite_import__(import.meta.url, somefile);
291+
292+
class X {
293+
import(a, b, c) {
294+
await ____wb_rewrite_import__ (import.meta.url, somefile);
295+
}
296+
}
285297
""",
286298
),
287299
ImportTestContent(
@@ -341,6 +353,7 @@ def test_import_rewrite(rewrite_import_content: ImportTestContent):
341353
"a.window.x = 5",
342354
" postMessage({'a': 'b'})",
343355
"simport(5);",
356+
"import(e) {",
344357
"a.import(5);",
345358
"$import(5);",
346359
"async import(val) { ... }",

0 commit comments

Comments
 (0)