Skip to content

ES6 modules with imports broken #964

@rofe

Description

@rofe

Describe the bug

pywb's JavaScript writer inserts initialization code at the beginning of JS files, placing it before ES6 import statements. This breaks ES6 modules since the ECMAScript specification requires imports to appear before any other code.

Steps to reproduce the bug

  1. Archive a web page containing an ES6 module with import statements
  2. Replay the page through pywb
  3. The injected pywb code appears before imports, causing a syntax error

Example file that breaks:

import { foo } from 'module1';
import bar from 'module2';

Becomes:

let window = _init('window');  // ❌ Injected first
import { foo } from 'module1';  // ERROR: imports must be first!

Expected behavior

Import statements should remain at the top of the file, with pywb's initialization code inserted after all imports:

import { foo } from 'module1';  // ✅ Imports at top
import bar from 'module2';
let window = _init('window');   // Injected after imports

Screenshots

N/A

Environment

  • Component: pywb JavaScript rewriter
  • Affected files: Any ES6 module with import statements
  • Version: All versions prior to fix

Additional context

Root cause: The first_buff mechanism in StreamingRewriter always inserts code at the very beginning without checking for ES6 imports.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions