Skip to content

Wombat breaks native JavaScript modules #116

Closed
@Rich-Harris

Description

@Rich-Harris

I was looking at archived copies of https://kit.svelte.dev on the Wayback Machine (example), and discovered that the JavaScript errors when it loads:

Uncaught (in promise) SyntaxError: import declarations may only appear at top level of a module

This is happening because the file in question is a JavaScript module (i.e. it uses import/export syntax):

var _____WB$wombat$assign$function_____ = function(name) {return (self._wb_wombat && self._wb_wombat.local_init && self._wb_wombat.local_init(name)) || self[name]; };
if (!self.__WB_pmw) { self.__WB_pmw = function(obj) { this.__WB_source = obj; return this; } }
{
  let window = _____WB$wombat$assign$function_____("window");
  let self = _____WB$wombat$assign$function_____("self");
  let document = _____WB$wombat$assign$function_____("document");
  let location = _____WB$wombat$assign$function_____("location");
  let top = _____WB$wombat$assign$function_____("top");
  let parent = _____WB$wombat$assign$function_____("parent");
  let frames = _____WB$wombat$assign$function_____("frames");
  let opener = _____WB$wombat$assign$function_____("opener");

import{o as Ce,t as se}from"../chunks/index.1cc2e0ff.js"; // rest of the code omitted


}
/*
     FILE ARCHIVED ON 16:57:32 Mar 27, 2023 AND RETRIEVED FROM THE
     INTERNET ARCHIVE ON 00:11:11 Mar 28, 2023.
     JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE.

     ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C.
     SECTION 108(a)(3)).
*/
/*
playback timings (ms):
  captures_list: 89.103
  exclusion.robots: 0.064
  exclusion.robots.policy: 0.055
  RedisCDXSource: 5.547
  esindex: 0.007
  LoadShardBlock: 67.337 (3)
  PetaboxLoader3.datanode: 85.078 (4)
  load_resource: 84.5
  PetaboxLoader3.resolve: 47.163
*/

As the error message says, import and export can only appear at the top level of a module, not inside a function body. Unfortunately this means that Wombat's transformation breaks every site that uses native modules. Today, these sites are in the minority, but since modules are supported in all modern browsers and many tools emit them automatically, the number is growing rapidly.

Afraid I don't have a great suggestion for a solution handy! Perhaps something like this, if import/export is detected in the code...

import { window, self, etc... } from 'data:application/javascript;base64,xyz123';

...where xyz123 is a base64-encoded module containing the relevant overrides?

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