mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-25 06:02:13 +00:00
Don't put anything before ES module imports
Because of some incorrect use of `js.push_str(..)`, we could sometimes emit code before the ES modules imports, which is syntactically invalid: const __exports = {}; import { Thing } from '...'; // Syntax error! This has been fixed by making sure that the correct `imports` or `imports_post` string is built up. We now also assert that the `js` string is empty at the location where we add imports if we're using ES modules.
This commit is contained in:
parent
863a8b9cea
commit
dff9b9b1e3
@ -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);
|
||||
|
@ -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),
|
||||
|
Loading…
x
Reference in New Issue
Block a user