Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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)
- Keep escaped Unicode characters in CSS strings instead of turning them into literal characters when rewriting (#377)
- 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)
- 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)

## [5.5.2] - 2026-11-06

Expand Down
12 changes: 11 additions & 1 deletion src/zimscraperlib/rewriting/js.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@
r"""^\s*?import\s*?[{"'*]""",
)
EXPORT_RX = re.compile(
r"""\s*?export\s*?({([\s\w,$\n]+?)}[\s;]*|default|class)\s+""", re.MULTILINE
r"""\s*?export\s*?("""
r"""{([\s\w,$\n]+?)}[\s;]*"""
r"""|default"""
r"""|\*"""
r"""|class"""
r"""|const"""
r"""|let"""
r"""|var"""
r"""|(?:async\s+)?function"""
r""")\s+""",
re.MULTILINE,
)
IMPORT_EXPORT_MATCH_RX = re.compile(
r"""(^|;)\s*?(?:im|ex)port(?:['"\s]*(?:[\w*${}\s,]+from\s*)?['"\s]?['"\s])(?:.*?)['"\s]""",
Expand Down
63 changes: 63 additions & 0 deletions tests/rewriting/test_js_rewriting.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,69 @@ def wrap_import(text: str) -> str:
expected="""a = _____WB$wombat$check$this$function_____(this).location

export { a };
""",
),
# export const/let/var/function/async function/* are also module markers
# (#387 - EXPORT_RX used to only recognize `export {...}`, `export default`
# and `export class`)
ImportTestContent(
input_="""a = this.location

export const b = 1;
""",
expected="""a = _____WB$wombat$check$this$function_____(this).location

export const b = 1;
""",
),
ImportTestContent(
input_="""a = this.location

export let b = 1;
""",
expected="""a = _____WB$wombat$check$this$function_____(this).location

export let b = 1;
""",
),
ImportTestContent(
input_="""a = this.location

export var b = 1;
""",
expected="""a = _____WB$wombat$check$this$function_____(this).location

export var b = 1;
""",
),
ImportTestContent(
input_="""a = this.location

export function b() {}
""",
expected="""a = _____WB$wombat$check$this$function_____(this).location

export function b() {}
""",
),
ImportTestContent(
input_="""a = this.location

export async function b() {}
""",
expected="""a = _____WB$wombat$check$this$function_____(this).location

export async function b() {}
""",
),
ImportTestContent(
input_="""a = this.location

export * from "./other.js";
""",
expected="""a = _____WB$wombat$check$this$function_____(this).location

export * from "./other.js";
""",
),
# rewrite import same line
Expand Down