Skip to content

Commit ca39112

Browse files
authored
Merge pull request #388 from openzim/benoit74/fix-export-rx-module-detection
Detect export const/let/var/function/* as module code
2 parents c209d81 + 35dae45 commit ca39112

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Patch vendored `wombat.js` so `location.href`/`.search`/`.pathname` (and the rest of the `Location` getters) prefer `wb_info.url` over re-deriving a value via `extractOriginalURL`, fixing their value for any archived page whose captured URL has a query string (#382, webrecorder/wombat#244)
1414
- Keep escaped Unicode characters in CSS strings instead of turning them into literal characters when rewriting (#377)
1515
- Patch vendored `wombat.js` so element properties (`img.src`, `script.src`, `link.href`, `a.href` and its derived `.pathname`/`.search`/..., `Attr.value`) return and forget a server-side-rewriter-recorded original the same way `getAttribute` already does (#386, webrecorder/wombat#240)
16+
- Detect `export const`/`let`/`var`/`function`/`async function`/`*` as module code (`EXPORT_RX` only recognized `export {...}`, `export default` and `export class`), fixing JS rewriting wrongly injecting a non-module `let arguments;` wrapper into ES modules using these common export forms, which is a syntax error in strict/module code (#387)
1617

1718
## [5.5.2] - 2026-11-06
1819

‎src/zimscraperlib/rewriting/js.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,17 @@
3838
r"""^\s*?import\s*?[{"'*]""",
3939
)
4040
EXPORT_RX = re.compile(
41-
r"""\s*?export\s*?({([\s\w,$\n]+?)}[\s;]*|default|class)\s+""", re.MULTILINE
41+
r"""\s*?export\s*?("""
42+
r"""{([\s\w,$\n]+?)}[\s;]*"""
43+
r"""|default"""
44+
r"""|\*"""
45+
r"""|class"""
46+
r"""|const"""
47+
r"""|let"""
48+
r"""|var"""
49+
r"""|(?:async\s+)?function"""
50+
r""")\s+""",
51+
re.MULTILINE,
4252
)
4353
IMPORT_EXPORT_MATCH_RX = re.compile(
4454
r"""(^|;)\s*?(?:im|ex)port(?:['"\s]*(?:[\w*${}\s,]+from\s*)?['"\s]?['"\s])(?:.*?)['"\s]""",

‎tests/rewriting/test_js_rewriting.py‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,69 @@ def wrap_import(text: str) -> str:
368368
expected="""a = _____WB$wombat$check$this$function_____(this).location
369369
370370
export { a };
371+
""",
372+
),
373+
# export const/let/var/function/async function/* are also module markers
374+
# (#387 - EXPORT_RX used to only recognize `export {...}`, `export default`
375+
# and `export class`)
376+
ImportTestContent(
377+
input_="""a = this.location
378+
379+
export const b = 1;
380+
""",
381+
expected="""a = _____WB$wombat$check$this$function_____(this).location
382+
383+
export const b = 1;
384+
""",
385+
),
386+
ImportTestContent(
387+
input_="""a = this.location
388+
389+
export let b = 1;
390+
""",
391+
expected="""a = _____WB$wombat$check$this$function_____(this).location
392+
393+
export let b = 1;
394+
""",
395+
),
396+
ImportTestContent(
397+
input_="""a = this.location
398+
399+
export var b = 1;
400+
""",
401+
expected="""a = _____WB$wombat$check$this$function_____(this).location
402+
403+
export var b = 1;
404+
""",
405+
),
406+
ImportTestContent(
407+
input_="""a = this.location
408+
409+
export function b() {}
410+
""",
411+
expected="""a = _____WB$wombat$check$this$function_____(this).location
412+
413+
export function b() {}
414+
""",
415+
),
416+
ImportTestContent(
417+
input_="""a = this.location
418+
419+
export async function b() {}
420+
""",
421+
expected="""a = _____WB$wombat$check$this$function_____(this).location
422+
423+
export async function b() {}
424+
""",
425+
),
426+
ImportTestContent(
427+
input_="""a = this.location
428+
429+
export * from "./other.js";
430+
""",
431+
expected="""a = _____WB$wombat$check$this$function_____(this).location
432+
433+
export * from "./other.js";
371434
""",
372435
),
373436
# rewrite import same line

0 commit comments

Comments
 (0)