Skip to content

JS module detection misses export const/let/var/function, breaking strict-mode rewriting #387

Closed

Description

@benoit74

Context

While investigating warc2zim#327 (fixed upstream by #386), I built a test ZIM of a modern MDN page and found that most of the page's interactive JS (ads, survey widget, search, menus, the "Try it" wrapper) failed to initialize in the browser console with:

SyntaxError: Unexpected eval or arguments in strict mode

Root cause

rewriting/js.py's _detect_module_or_strict() decides whether a script is an ES module by matching EXPORT_RX:

EXPORT_RX = re.compile(
    r"""\s*?export\s*?({([\s\w,$\n]+?)}[\s;]*|default|class)\s+""", re.MULTILINE
)

This only recognizes export {...}, export default and export class. It does not match export const foo = ..., export let foo = ..., export var foo = ... or export function foo() {...} — all common, valid ES module export forms (and the one webpack/rspack commonly emits, e.g. export const __webpack_modules__={...}).

When a module uses one of these unrecognized forms and contains no other module marker (import, export {, export default, export class, no "use strict"; literal), _detect_module_or_strict() returns "lax" instead of "module". JsRewriter.rewrite() then treats the script as a plain non-module script and wraps it with _wrap(), whose prefix (_init_local_declaration()) unconditionally injects:

let arguments;

Declaring arguments (or eval) via let/const is a syntax error in strict-mode/module code (ES spec early error on BoundNames containing eval or arguments). Since the script actually runs as a module (loaded via <script type="module"> or dynamic import()), the browser throws SyntaxError: Unexpected eval or arguments in strict mode at parse time, and the whole chunk silently fails to execute.

Repro

Minimal case (standalone, no HTML/wombat context needed — the bug is purely in the module-detection regex):

export const x = 1;

Rewriting this file with JsRewriter.rewrite() currently produces a script with let arguments; injected at top level, which is invalid wherever the rewritten output is loaded as a module (e.g. via a bare dynamic import() or <script type="module">).

Concretely, this was observed on https://developer.mozilla.org/static/client/7489.*.js, which starts with:

export const __rspack_esm_ids=[7489];export const __webpack_modules__={...}

Suggested fix

Broaden EXPORT_RX (or add a second check) to also match export const|let|var|function|async function|class declarations, e.g. something along the lines of:

EXPORT_RX = re.compile(
    r"""\s*?export\s*?("""
    r"""{([\s\w,$\n]+?)}[\s;]*"""
    r"""|default"""
    r"""|class\s"""
    r"""|(?:const|let|var)\s"""
    r"""|(?:async\s+)?function\b"""
    r""")""",
    re.MULTILINE,
)

(exact wording/anchoring to be refined — want to avoid false positives on e.g. a string literal containing the word export).

I'll attempt a local patch and open a PR against this issue.

Activity

  1. self-assigned this
    on Nov 6, 2026
  2. added theissue type on Nov 6, 2026
  3. added this to the 5.6.0 milestone on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions