diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index c44cd7cc..ae7b1b20 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -354,7 +354,8 @@ impl<'a> Context<'a> { | OutputMode::Node { experimental_modules: true, } => { - js.push_str(&format!("import * as wasm from './{}_bg';\n", module_name)); + self.imports + .push_str(&format!("import * as wasm from './{}_bg';\n", module_name)); if needs_manual_start { self.footer.push_str("wasm.__wbindgen_start();\n"); } @@ -365,7 +366,7 @@ impl<'a> Context<'a> { // expose the same initialization function as `--target no-modules` // as the default export of the module. OutputMode::Web => { - js.push_str("const __exports = {};\n"); + self.imports_post.push_str("const __exports = {};\n"); self.imports_post.push_str("let wasm;\n"); init = self.gen_init(&module_name, needs_manual_start); self.footer.push_str("export default init;\n"); @@ -377,6 +378,10 @@ impl<'a> Context<'a> { ts.push_str(&init_ts); // Emit all the JS for importing all our functionality + assert!( + !self.config.mode.uses_es_modules() || js.is_empty(), + "ES modules require imports to be at the start of the file" + ); js.push_str(&self.imports); js.push_str("\n"); js.push_str(&self.imports_post); diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index 45186593..5793b83b 100755 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -42,6 +42,19 @@ enum OutputMode { Node { experimental_modules: bool }, } +impl OutputMode { + fn uses_es_modules(&self) -> bool { + match self { + OutputMode::Bundler { .. } + | OutputMode::Web + | OutputMode::Node { + experimental_modules: true, + } => true, + _ => false, + } + } +} + enum Input { Path(PathBuf), Module(Module, String),