From 35dae451bb322f55c3507d530c8e1d545d106561 Mon Sep 17 00:00:00 2001 From: benoit74 Date: Fri, 6 Nov 2026 18:02:00 +0000 Subject: [PATCH] Detect export const/let/var/function/* as module code EXPORT_RX only recognized `export {...}`, `export default` and `export class`, so a script using `export const`/`let`/`var`/ `function`/`async function`/`*` (all common, e.g. webpack/rspack output) was misdetected as non-module code. JsRewriter then wrapped it with the non-module globals prefix, which unconditionally injects `let arguments;` - a syntax error in the module/strict-mode code the script actually runs as. Found while investigating openzim/warc2zim#327: MDN's interactive widgets failed in the browser with "SyntaxError: Unexpected eval or arguments in strict mode" because of this. (#387) --- CHANGELOG.md | 1 + src/zimscraperlib/rewriting/js.py | 12 +++++- tests/rewriting/test_js_rewriting.py | 63 ++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82225d7b..ce781f9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/zimscraperlib/rewriting/js.py b/src/zimscraperlib/rewriting/js.py index 74c74d36..a81c9ab3 100644 --- a/src/zimscraperlib/rewriting/js.py +++ b/src/zimscraperlib/rewriting/js.py @@ -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]""", diff --git a/tests/rewriting/test_js_rewriting.py b/tests/rewriting/test_js_rewriting.py index ccfda24a..9813a8ae 100644 --- a/tests/rewriting/test_js_rewriting.py +++ b/tests/rewriting/test_js_rewriting.py @@ -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